在数字海洋上托管 django 应用程序时遇到问题

时间:2021-06-10 16:49:05

标签: django nginx gunicorn digital-ocean

当我访问 http://[ip address] 时,我收到 502 错误请求错误。

/etc/systemd/system/gunicorn.socket 文件:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service 文件:

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

[Service]
User=mainuser
Group=www-data
WorkingDirectory=/home/mainuser/Personal_Web
ExecStart=/home/mainuser/Personal_Web/vr/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myapp.wsgi:application

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/myapp 文件:

server {
    listen 80;
    server_name [ip address];

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/mainuser/Personal_Web;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

当我运行 sudo tail -F /var/log/nginx/error.log 时,出现 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream 错误。我不知道为什么我会得到这个或如何解决它?

1 个答案:

答案 0 :(得分:0)

我认为您配置此行错误。

ExecStart=/home/mainuser/Personal_Web/vr/bin/gunicorn \
      --access-logfile - \
      --workers 3 \
      --bind unix:/run/gunicorn.sock \
      myapp.wsgi:application

您确定 myapp.wsgi:application 中的 myapp 是您在 Django 项目中的主文件夹吗?您的主文件夹是包含 settings.py 和 wsgi.py 的文件夹。

您可以尝试将其更改为 name_main_folder.wsgi:application 吗?