我得到了这个Flask应用程序,我在这个共享服务器上用127.0.0.1:5002上的gunicorn运行。 我有一个自己的nginx实例,我可以添加自定义configuration。 nginx目录的结构是这样的:
~/.nginx | conf.d | nginx.conf | [...]
其中nginx.conf包含以下内容:
pid /media/sdl1/home/USERNAME/.nginx/pid; error_log /media/sdl1/home/USERNAME/.nginx/error.log; worker_processes 1; worker_rlimit_nofile 65535; events { worker_connections 8192; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log off; sendfile on; tcp_nopush on; real_ip_header X-Forwarded-For; client_max_body_size 1g; client_body_temp_path /media/sdl1/home/USERNAME/.nginx/client; fastcgi_temp_path /media/sdl1/home/USERNAME/.nginx/fastcgi; uwsgi_temp_path /media/sdl1/home/USERNAME/.nginx/uwscgi; scgi_temp_path /media/sdl1/home/USERNAME/.nginx/scgi; include conf.d/*.conf; }
现在指向conf.d目录,其中包含一个名为000-default-server.conf的文件和一个名为000-default-server.d的空目录
000-default-server.conf包含这个:
server { listen 8080; server_name EXAMPLE.com; root /media/sdl1/home/USERNAME/www/$host/public_html; index index.html index.php; autoindex on; autoindex_exact_size off; autoindex_localtime on; error_log /media/sdl1/home/USERNAME/.nginx/error.log debug; # Pass files that end in .php to PHP location ~ \.php$ { fastcgi_read_timeout 1h; fastcgi_send_timeout 10m; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/media/sdl1/home/USERNAME/.nginx/php/socket; } location /apps/whatsmyuserinfo { proxy_redirect off; proxy_pass_header Server; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k; proxy_pass http://127.0.0.1:5002/; } # Deny access to anything starting with .ht location ~ /\.ht { deny all; } include conf.d/000-default-server.d/*.conf; }
用较less的揭示价值代替个人信息,那些价值就是全部上限。
现在这是nginx的一般设置。 这是我的问题出现的地方。 如果我使用
curl -i 127.0.0.1:5002
通过服务器上的SSH来查看gunicorn是否正确地为应用程序提供服务,该应用程序已正确投放。 如果我在外部通过EXAMPLE.com/apps/whatsmyuserinfo获得502错误网关错误。 nginx错误日志可能会说明一些问题,下面是在debugging模式下对EXAMPLE.com/apps/whatsmyuserinfo的单个请求。
"GET / HTTP/1.0 Host: EXAMPLE.com X-Real-IP: 10.0.0.1 X-Scheme: http X-Forwarded-For: 188.176.255.124, 10.0.0.1 Connection: close X-Host: EXAMPLE.com X-Forwarded-Proto: http User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Cookie: __utma=18102207.768668361.1377199324.1377199324.1377199324.1; __utmz=18102207.1377199324.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) Cache-Control: max-age=0 " 2013/08/23 19:00:01 [debug] 31195#0: *14 http cleanup add: 0000000001F44D30 2013/08/23 19:00:01 [debug] 31195#0: *14 get rr peer, try: 1 2013/08/23 19:00:01 [debug] 31195#0: *14 socket 10 2013/08/23 19:00:01 [debug] 31195#0: *14 epoll add connection: fd:10 ev:80000005 2013/08/23 19:00:01 [debug] 31195#0: *14 connect to 127.0.0.1:5002, fd:10 #15 2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream connect: -2 2013/08/23 19:00:01 [debug] 31195#0: *14 posix_memalign: 0000000001F3D850:128 @16 2013/08/23 19:00:01 [debug] 31195#0: *14 event timer add: 10: 90000:1377284491613 2013/08/23 19:00:01 [debug] 31195#0: *14 http finalize request: -4, "/apps/whatsmyuserinfo?" a:1, c:2 2013/08/23 19:00:01 [debug] 31195#0: *14 http request count:2 blk:0 2013/08/23 19:00:01 [debug] 31195#0: *14 http run request: "/apps/whatsmyuserinfo?" 2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream check client, write event:1, "/apps/whatsmyuserinfo" 2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream recv(): -1 (11: Resource temporarily unavailable) 2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream request: "/apps/whatsmyuserinfo?" 2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream process header 2013/08/23 19:00:01 [error] 31195#0: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.0.1, server: EXAMPLE.com, request: "GET /apps/whatsmyuserinfo HTTP/1.0", upstream: "http://127.0.0.1:5002/", host: "EXAMPLE.com" 2013/08/23 19:00:01 [debug] 31195#0: *14 http next upstream, 2 2013/08/23 19:00:01 [debug] 31195#0: *14 free rr peer 1 4 2013/08/23 19:00:01 [debug] 31195#0: *14 finalize http upstream request: 502 2013/08/23 19:00:01 [debug] 31195#0: *14 finalize http proxy request 2013/08/23 19:00:01 [debug] 31195#0: *14 free rr peer 0 0 2013/08/23 19:00:01 [debug] 31195#0: *14 close http upstream connection: 10 2013/08/23 19:00:01 [debug] 31195#0: *14 free: 0000000001F3D850, unused: 48 2013/08/23 19:00:01 [debug] 31195#0: *14 event timer del: 10: 1377284491613 2013/08/23 19:00:01 [debug] 31195#0: *14 reusable connection: 0 2013/08/23 19:00:01 [debug] 31195#0: *14 http finalize request: 502, "/apps/whatsmyuserinfo?" a:1, c:1 2013/08/23 19:00:01 [debug] 31195#0: *14 http special response: 502, "/apps/whatsmyuserinfo?" 2013/08/23 19:00:01 [debug] 31195#0: *14 http set discard body 2013/08/23 19:00:01 [debug] 31195#0: *14 xslt filter header 2013/08/23 19:00:01 [debug] 31195#0: *14 HTTP/1.1 502 Bad Gateway
最重要的部分是:
2013/08/23 19:00:01 [debug] 31195#0: *14 http upstream recv(): -1 (11: Resource temporarily unavailable) 2013/08/23 19:00:01 [error] 31195#0: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.0.1, server: EXAMPLE.com, request: "GET /apps/whatsmyuserinfo HTTP/1.0", upstream: "http://127.0.0.1:5002/", host: "EXAMPLE.com" 2013/08/23 19:00:01 [debug] 31195#0: *14 HTTP/1.1 502 Bad Gateway
任何人都可以看到这里发生了什么,因为我不明白为什么拒绝工作。 我已经尝试在专门为应用程序的nginxconfiguration中创build一个新的服务器,并有位置/,我试图摆脱大部分的代理设置,只是留下一些像proxy_pass,我已经尝试创build一个上游在configuration等。
充分的披露:我也发布了这个在stackoverflow,但被告知张贴在这里,因为它更有可能我会在这里得到帮助。