我在这个共享服务器上的 127.0.0.1:5002 上用 gunicorn 运行了这个 Flask 应用。我为自己创建了一个 nginx 实例,可以向其中添加自定义配置。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;
}
将个人信息替换为不太明显的值,这些值全部大写。
这就是 nginx 的一般设置。这就是我的问题所在。如果我使用
curl -i 127.0.0.1:5002
通过服务器上的 SSH 查看 gunicorn 是否正确为应用程序提供服务,应用程序已正确提供服务。如果我从外部执行此操作,通过 EXAMPLE.com/apps/whatsmyuserinfo,我会收到 502 Bad Gateway 错误。nginx 错误日志可能会对此有所说明,以下是在调试模式下对 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
有人能看出这里发生了什么吗?因为我不明白为什么它拒绝工作。我尝试在 nginx 配置中专门为应用程序创建一个新服务器,并将位置设置为 /,我尝试删除大多数代理设置,只留下几个,如 proxy_pass,我尝试在配置中创建一个上游,等等。
完全披露:我也在 stackoverflow 上发布了此信息,但被告知也在这里发布,因为这样我更有可能在这里获得帮助。