Django + Guncorn + nginx:111:连接被拒绝;网关错误 502

Django + Guncorn + nginx:111:连接被拒绝;网关错误 502

由于某种原因,它不起作用。你看到这段代码有什么问题吗?提前感谢您的审核!

错误日志:

2019/11/10 18:02:02 [error] 8761#8761: *1 connect() to unix:/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: demid.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "127.0.0.1"

终端:

demid@demid-Aspire-7736:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
demid@demid-Aspire-7736:~$ systemctl daemon-reload
demid@demid-Aspire-7736:~$ systemctl restart gunicorn.socket gunicorn.service nginx.service; systemctl status gunicorn.socket gunicorn.service nginx.service
Failed to dump process list, ignoring: No such file or directory
● gunicorn.socket - gunicorn socket
   Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-11-10 17:59:39 EET; 288ms ago
   Listen: /run/gunicorn.sock (Stream)
   CGroup: /system.slice/gunicorn.socket

lapkr. 10 17:59:39 demid-Aspire-7736 systemd[1]: Listening on gunicorn socket.

● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-11-10 17:59:39 EET; 213ms ago
 Main PID: 8756 (gunicorn)
    Tasks: 1 (limit: 4669)
   CGroup: /system.slice/gunicorn.service
           └─8756 /home/demid/myprojectdir/myprojectenv/bin/python3 /home/demid/myprojectdir/myprojectenv/bin/gunicorn

lapkr. 10 17:59:39 demid-Aspire-7736 systemd[1]: Started gunicorn daemon.

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-11-10 17:59:39 EET; 70ms ago
     Docs: man:nginx(8)
Process: 8757 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, 
  Process: 8759 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 8758 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 8760 (nginx)
    Tasks: 3 (limit: 4669)
   CGroup: /system.slice/nginx.service
           ├─8760 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─8761 nginx: worker process
           └─8762 nginx: worker process

lapkr. 10 17:59:39 demid-Aspire-7736 systemd[1]: Starting A high performance web server and a reverse proxy server...
lapkr. 10 17:59:39 demid-Aspire-7736 systemd[1]: Started A high performance web server and a reverse proxy server.

/etc/nginx/conf.d/demid.com.conf

server {
    listen         80 default_server;
    listen         [::]:80 default_server;
    server_name    demid.com;
    location = /favicon.ico {access_log off; log_not_found off;}
    location /static/ {
root /demid/myprojectdir;
    }

    location / {
include proxy_params;
#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:/home/sammy/myproject/myproject.sock;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

/etc/systemd/system/gunicorn.service

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

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

[Install]
WantedBy=multi-user.target

/etc/nginx/nginx.conf

worker_processes auto;
worker_rlimit_nofile 50000;

events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout  65;
    keepalive_requests 256;
    reset_timedout_connection on;

    gzip  on;
    gzip_vary on;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_types text/plain text/xml text/css text/javascript application/x-javascript application/json application/xml application/xml+rss image/png image/gif image/jpeg image/jpg;

    open_file_cache max=50000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;

    client_max_body_size 512m;

    server_tokens off;

    include /etc/nginx/conf.d/*.conf;
}

答案1

总而言之 -尝试通过位于 的 UNIX 套接字连接后nginx返回。111: Connection refusedgunicorn/run/gunicorn.sock

首先我们确定这不是权限问题:

# ls -l /run/gunicorn.sock
srw-rw-rw- 1 root root 0 lapkr 10 20:34 /run/gunicorn.sock

rw-rw-rw-意味着每个用户都可以读取和写入该文件。

(旁注-这些权限太广泛,更好的做法可能是确保nginx工作人员由同一用户运行,并gunicorn.service仅向该用户授予 RW 权限,但这个答案主要集中于使连接正常工作,而这实际上不是我的领域。)

下一步操作是列出在套接字上监听的进程:

# netstat --protocol=unix -nlp | grep 'gunicorn'

gunicorn您也可以使用 grep 完整套接字路径,但是这里就足够了)。

输出为空,gunicorn没有监听。

查看 gunicorn 服务日志后:

# systemctl status gunicorn.service
# journalctl -u gunicorn.service

我们发现套接字绑定失败并显示以下消息:

ModuleNotFoundError: No module named 'myproject.wsgi' 

这是由于在 Python 应用程序中使用错误的路径进行套接字绑定而导致的/etc/systemd/system/gunicorn.service

该文件包含两个影响此点的参数-WorkingDirectoryExecStart

必须WorkingDirectory指向包含主gunicorn配置文件的目录 - settings.py

必须ExecStart包含运行gunicornvirtualenv 二进制文件的主命令。您需要使用选项来运行它--bind /run/gunicorn.sock myproject.wsgi:application,其中/run/gunicorn.sock是套接字文件路径,myproject.wsgi:application是 Python 模块入口点的相对路径(目录用点分隔,.py省略扩展名,模块名称后跟冒号和可调用变量application)。

相关内容