【发布时间】:2017-10-18 04:48:44
【问题描述】:
AWS 实例上运行一个 Django 应用,通过 gunicorn 和 nginx 配置,运行了一年多,但突然出现 502 bad gateway 错误,然后我在 nginx 错误中看到了下面提到的消息记录,
2017/05/17 16:18:35 [error] 1040#0: *7460 connect() to unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock failed (111: Connection refused) while connecting to upstream, client: xx.xxxx.xx.xxx, server: xx.xx.xx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock:/", host: "xx.xx.xx.xxx", referrer: "http://xx.xx.xx.xxx"
我的 nginx 配置:
server {
client_max_body_size 200M;
listen 80;
listen [::]:80 ipv6only=on;
server_name xx.xx.xx.xxx;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/myserver.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/webapps/myproject/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock;
proxy_set_header X-Forwarded-Protocol $scheme;
}
if ($scheme = http){
return 301 https://xx.xx.xx.xxx$request_uri;
}
if ($http_host = pas-cash.com){
return 303 https://xx.xx.xx.xxx$request_uri;
}
}
我的 gunicorn.conf
description "Gunicorn application server handling myproject"
start on runlevel [6789]
stop on runlevel [!6789]
respawn
setuid ubuntu
setgid www-data
chdir /home/ubuntu/webapps/myproject/myproject
exec /home/ubuntu/webapps/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock myproject.wsgi:application
之后我通过以下命令重新启动了 nginx
sudo service nginx restart
重新启动后,应用程序运行良好,我找不到这个错误背后的具体原因,我为此搜索了它,但我得到了不同类型的答案,但没有适合我的答案,请你们帮帮我,为什么会发生这种情况,我的配置中是否缺少任何东西,或者这种行为背后的常见/一般原因是什么。这对我很有帮助,在此先感谢。
【问题讨论】:
-
尝试从nginx配置中的
proxy_pass中删除http:// -
@alfonso.kim 感谢您的评论,您能否解释一下为什么会发生这种情况,以及从“proxy_pass”中删除“http://”的原因是什么,请给我一个清晰的解释。
-
当然。我不确定这是否是您问题的答案,所以我没有发布它。成功了吗?
-
@alfonso.kim,实际上应用程序在我重新启动 nginx 后运行良好,我需要知道它为什么会发生以及这背后的原因是什么。就是这样。
-
你是怎么让它工作的? Nginx 不喜欢我。
nginx: [emerg] invalid URL prefix