解决Django项目的502错误网关Nginx - gunicorn - Debian

bjg7j2ky  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(228)

嗨,我正在尝试第一次使用Nginx和gunicorn在linode上部署我的Django项目。
目前我没有设置venv文件,我只是下载了整个系统中的所有需求。我使用Linode服务器,并从市场上选择了Django,设置的系统是Debian。该项目在portal 8000上运行得非常好,但现在我试图将其带入开发。
我采取了以下步骤:sudo nano/etc/系统管理/系统/gunicorn.服务

[Unit]
Description=Gunicorn service for project
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/DjangoApp/
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/var/www/DjangoApp/project.sock project.wsgi:application

[Install]
WantedBy=multi-user.target

我在项目中找不到sock文件,但这是项目的树,如果有帮助的话cd/var/www/DjangoApp:

api  db.sqlite3  project  manage.py  media  README.md  requirements.txt  static  tac  users

在/var/www/DjangoApp/项目中

asgi.py  __init__.py  __pycache__  settings.py  urls.py  wsgi.py

在我的sudo nano/etc/nginx/可用站点/项目中

server {
        listen 80;
        server_name 111.111.111.11;
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /var/www/DjangoApp/;
        }
        location / {
                include proxy_params;
            proxy_pass http://unix:/var/www/DjangoApp/project.sock;
        }
}

下面是日志错误

root@139-177-193-82:~# sudo tail -50 /var/log/nginx/error.log
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * open socket #11 left in connection 4                                                                                  ...........
......................: * open socket #13 left in connection 6                                                                                  ...........
......................: aborting                                                                                                                ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/run/gunicorn.sock failed (2: No such file or directory) while connecting to upstream,.........................
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........
......................: * connect() to unix:/var/www/DjangoApp/project.sock failed (2: No such file or directory) while connecting to upstream, ...........

我的问题:我在哪里可以找到. sock文件连接到它来修复这个错误。任何建议将不胜感激,以找到解决方案来修复502坏网关

bybem2ql

bybem2ql1#

在创建服务文件之前,您需要创建systemd Socket,然后创建sock文件,为此,您需要在/etc/systemd/system/<file name you want>下创建一个文件,如下所示。

sudo nano /etc/systemd/system/gunicorn_django_app.socket

在该文件中,您需要提供以下详细信息

[Unit]
Description=Gunicorn service for project

[Socket]
ListenStream=/run/gunicorn_django_app.sock

[Install]
WantedBy=sockets.target

这将在**/run/gunicorn_django_app.sock下创建一个sock文件
然后,您需要在路径/etc/systemd/system/<file name you like>中创建一个
systemd服务文件**,如下所示

sudo nano /etc/systemd/system/gunicorn_django_app.service

在文件中您需要提供服务相关的详细信息

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

[Service]
  User=username # user that can access those files
  Group=www-data # group that can access those files 
  WorkingDirectory=<projectpath>
  ExecStart=/<guincorn path>\
      --access-logfile - \
      --workers 3 \
      --bind unix:/run/gunicorn_django_app \
      DjangoApp.wsgi:application

那么你必须启动服务

sudo systemctl start gunicorn_django_app.socket
sudo systemctl enable gunicorn_django_app.socket

要知道更多使用下面的链接这将帮助你了解更多关于这个概念.它是在ubuntu linux但debain基于linux(它将工作在Linode太)digitalocean

相关问题