由于某种原因,它不起作用。您在此代码中看到任何问题吗?提前感谢您的评论!
错误日志:
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;
}
总而言之 -在尝试通过位于 的 UNIX 套接字连接
nginx
后返回。111: Connection refused
gunicorn
/run/gunicorn.sock
起初,我们确保这不是权限问题:
这
rw-rw-rw-
意味着每个用户都可以读取和写入文件。(旁注 - 这些权限太宽泛了,更好的做法可能是确保
nginx
工作人员由与该用户相同的用户运行gunicorn.service
并仅向该用户授予 RW 权限,但这个答案主要集中在使连接工作上,这真的反正不是我的域。)下一个动作是列出侦听套接字的进程:
(代替
gunicorn
你也可以 grep 完整的套接字路径,但在这里就足够了)。输出是空的,
gunicorn
没有在听。查看 gunicorn 服务日志后:
我们发现套接字绑定失败并显示消息:
这是由于在
/etc/systemd/system/gunicorn.service
.该文件包含两个影响此参数的参数 -
WorkingDirectory
和ExecStart
.WorkingDirectory
必须指向包含主gunicorn
配置文件的目录 -settings.py
.ExecStart
必须包含运行gunicorn
virtualenv 二进制文件的主命令。您需要使用选项运行它--bind /run/gunicorn.sock myproject.wsgi:application
,其中/run/gunicorn.sock
是套接字文件路径,并且myproject.wsgi:application
是 Python 模块入口点的相对路径(目录由点分隔,扩展名.py
被省略,模块名称后跟冒号和可调用application
变量)。