Aja*_*mar 10 django nginx gunicorn
一个Django应用程序在AWS实例上运行,通过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"
Run Code Online (Sandbox Code Playgroud)
我的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
之后我通过以下命令重新启动了 nginx
sudo service nginx restart
Run Code Online (Sandbox Code Playgroud)
重新启动后,应用程序运行良好,我找不到此错误背后的具体原因,我为此进行了谷歌搜索,但我得到了不同类型的答案,但没有适合我的答案,你们可以帮我吗因为,为什么会发生这种情况,我的配置中是否缺少任何内容,或者此行为背后的常见/一般原因是什么。这对我来说非常有帮助,提前致谢。
Jos*_*osh 11
这是“突然”引起的,不是因为 nginx 错误,而是由于 Gunicorn 或您的应用程序的错误(代码错误、未安装软件包等)。不过,记录和修复应该相对容易。
首先尝试从服务器运行您的应用程序python manage.py runserver,看看是否遇到任何问题。对于 也一样... migrate。通常,生产无法工作但本地可以工作的问题是因为缺少软件包或缺少迁移。在本地创建一个requirements.txt 文件并将其安装在生产环境中。
如果错误仍然存在,请检查gunicorn 日志gunicorn --log-file=- YourApp.wsgi:application。纠正所有这些错误后,运行
sudo systemctl status gunicorn.socket
sudo systemctl status gunicorn
Run Code Online (Sandbox Code Playgroud)
你想要既活跃又跑步。如果您开始收到 400 错误,这是一个好兆头,因为它现在是 Django 错误(通常是允许的主机)。打开 debug=True 以查看 django 的确切错误。
记住每当对代码运行进行任何更改时
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
Run Code Online (Sandbox Code Playgroud)
仅供参考,如果上述方法都不起作用,那么您可以随时检查您的 nginx 日志
sudo tail -30 /var/log/nginx/error.log
Run Code Online (Sandbox Code Playgroud)
http://尝试从 nginx 配置中的 proxy_pass 中删除:
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 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;
}
}
Run Code Online (Sandbox Code Playgroud)
原因是gunicorn正在监听 unix 套接字(--bind参数)。然后nginx应该将流量转发到该套接字。http://代表常规 IP:PORT 中的 TCP 套接字,这不是您的情况。
| 归档时间: |
|
| 查看次数: |
23088 次 |
| 最近记录: |