502 错误网关:Nginx、gunicorn、Django
502 bad gateway: Nginx, gunicorn, Django
这是我的nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 0.0.0.0;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /myproject;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/root/myproject/myproject.sock;
}
}
}
我的项目有这样的结构:
这是我的 gunicorn.service
文件
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/root/myproject/myproject
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
error.log
2022/04/13 12:45:30 [crit] 20474#20474: *8 connect() to unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.81.234.10, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/", host: "10.10.89.8"
我的 gunicorn 服务工作正常,nginx.conf
也工作正常,没有错误。
我的项目目录已获得所有权限
我仍然收到 502 Bad Gateway 错误。
如果需要任何其他信息,请告诉我
问题是您在不同的位置创建套接字文件并将 nginx 指向不同的位置。您的套接字文件创建于
/run/gunicorn.sock
正如你从这个命令中看到的那样
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
并尽可能在 nginx 配置中的不同位置 (/root/myproject/myproject.sock
) 引用它
看这里
proxy_pass http://unix:/root/myproject/myproject.sock;
您可以通过两种方式解决此问题
- 通过从
更改 gunicorn conf
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
到
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application
- 通过从
更改 nginx conf
proxy_pass http://unix:/root/myproject/myproject.sock;
到
proxy_pass http://unix:/run/gunicorn.sock;
我推荐使用第一种方法
这些更改后重新启动 gunicorn 和 nginx
这是我的nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 0.0.0.0;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /myproject;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/root/myproject/myproject.sock;
}
}
}
我的项目有这样的结构:
这是我的 gunicorn.service
文件
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/root/myproject/myproject
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
error.log
2022/04/13 12:45:30 [crit] 20474#20474: *8 connect() to unix:/root/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.81.234.10, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream: "http://unix:/root/myproject/myproject.sock:/", host: "10.10.89.8"
我的 gunicorn 服务工作正常,nginx.conf
也工作正常,没有错误。
我的项目目录已获得所有权限 我仍然收到 502 Bad Gateway 错误。
如果需要任何其他信息,请告诉我
问题是您在不同的位置创建套接字文件并将 nginx 指向不同的位置。您的套接字文件创建于
/run/gunicorn.sock
正如你从这个命令中看到的那样
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
并尽可能在 nginx 配置中的不同位置 (/root/myproject/myproject.sock
) 引用它
看这里
proxy_pass http://unix:/root/myproject/myproject.sock;
您可以通过两种方式解决此问题
- 通过从 更改 gunicorn conf
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock myproject.wsgi:application
到
ExecStart=/root/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/root/myproject/myproject.sock myproject.wsgi:application
- 通过从 更改 nginx conf
proxy_pass http://unix:/root/myproject/myproject.sock;
到
proxy_pass http://unix:/run/gunicorn.sock;
我推荐使用第一种方法
这些更改后重新启动 gunicorn 和 nginx