遇到502错误的网关-Nginx和Gunicorn

时间:2020-05-15 12:13:57

标签: django nginx gunicorn

我正在尝试使用Gunicorn和Django设置Nginx,但收到502错误的网关错误。以下是我的文件。

gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/apio/apio/
ExecStart=/root/sweetapi_env/bin/gunicorn \
         --access-logfile - \
         --workers 3 \
         --bind unix:/run/gunicorn.sock \
        apio.wsgi:application

[Install]
WantedBy=multi-user.target

我的nginx.conf文件

server {
        listen 80;
        server_name MY_IP_ADDRESS;
        location / {
                include proxy_params;
                proxy_pass http://unix:/run/gunicorn.sock;
        }

}

我的文件夹结构

apio
    apio
        db.sqlite3
        manage.py
        static
        apio
            settings.py
            wsgi.py
            urls.py
            __init__.py

在点击我的IP地址后,我进入/var/log/nginx/error.log

2020/05/15 12:08:15 [error] 31068#31068: *1 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while 
connecting to upstream, client: 117.203.***.***, server: ***.**.**.**, 
request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/"

我的gunicorn.socket测试也失败了

gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: failed (Result: service-start-limit-hit) since Fri 2020-05-15 11:58:41 UTC; 12min ago
   Listen: /run/gunicorn.sock (Stream)

请提供一些提示。我该如何调试?

1 个答案:

答案 0 :(得分:0)

猜猜Gunicorn没有在虚拟环境中正确安装。我的Gunicorn.socket日志给了我线索。

sudo journalctl -u gunicorn
May 15 11:19:34 ubuntu-s-1vcpu-1gb-blr1-01 systemd[29716]: gunicorn.service: Failed to execute command: No such file or directory
May 15 11:19:34 ubuntu-s-1vcpu-1gb-blr1-01 systemd[29716]: gunicorn.service: Failed at step EXEC spawning /root/sweetapi_env/bin/gunicorn: No such file or directory