1.504 Gateway Time-outnginx/1.18.0 (Ubuntu)

日志报错:

2024/02/11 04:38:54 [error] 564#564: *29 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *******, server: *******, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/*******/myproject/app.sock/favicon.ico", host: "*******", referrer: "http://*******/"

应用服务器(通过 app.sock Unix 套接字)获取 /favicon.ico 时遇到了超时问题。

这通常表明请求没有被正确地路由到静态文件处理逻辑,而是被发送到了后端应用,

后端应用没有及时响应或无法处理该请求

解决方法:说明favicon.ico的位置

location = /favicon.ico {alias /home/*****/myproject/static/favicon.ico;access_log off;log_not_found off;}

2.502 Bad Gateway nginx/1.18.0 (Ubuntu):

日志报错:

2024/02/11 05:08:04 [crit] 12048#12048: *4 connect() to unix:/home/********/myproject/app.sock failed (2: No such file or directory) while connecting to upstream, client: ********, server: ********,request: "GET / HTTP/1.1", upstream: "http://unix:/home/********/myproject/app.sock:/", host: "********"

这个错误表明 Nginx 无法通过 Unix 套接字 app.sock 连接到您的应用程序,因为它找不到指定的文件或目录。可能的原因包括:

  • 1.应用程序未运行:如果您的应用程序没有启动,那么 .sock 文件也不会存在。

  • 2.路径错误:配置中的路径不正确,或 .sock 文件被移动或删除。

  • 3.权限问题:Nginx 没有足够的权限访问 .sock 文件。

解决方法:

原因一:

查看gunicorn的状态:
sudo systemctl status myproject
如下状态(截取网上):
gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-01-18 23:32:11 UTC; 3min 23s agoProcess: 6347 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS) Main PID: 6355 (gunicorn)Tasks: 2 Memory: 195.7MCPU: 1.426s CGroup: /system.slice/gunicorn.service ├─6355 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:application └─6360 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:applicationJan 18 23:32:11 python-server systemd[1]: Stopped gunicorn daemon.Jan 18 23:32:11 python-server systemd[1]: Started gunicorn daemon.Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Starting gunicorn 19.7.1Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Listening at: unix:/run/gunicorn/socket (6355)Jan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Using worker: syncJan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6360] [INFO] Booting worker with pid: 6360
若出现任何error或者warning,请修复他之后运行以下命令
(重新加载gunicorn,并查看状态):
pkill gunicornsudo systemctl daemon-reloadsudo systemctl start myprojectsudo systemctl enable myprojectsudo systemctl status myproject

原因二:修改权限(前面为用户名,后面为路径)

sudo chown ******:www-data /home/****/*****/