어떤 이유로 작동하지 않습니다. 이 코드에 문제가 있습니까? 미리 검토해 주셔서 감사합니다!
오류 기록:
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;
}
요약하면- nginx
에 위치한 UNIX 소켓 111: Connection refused
을 gunicorn
통해 연결을 시도한 후 돌아 왔습니다 /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'
이는 .NET에서 소켓 바인딩을 위해 Python 애플리케이션에 대한 잘못된 경로를 사용하여 발생했습니다 /etc/systemd/system/gunicorn.service
.
파일이 영향을 미치는 두 PARAMS을 포함 - WorkingDirectory
와 ExecStart
.
는 WorkingDirectory
주요 들어있는 디렉토리를 지정해야합니다 gunicorn
config 파일을 - settings.py
.
는 ExecStart
실행하는 기본 명령이 포함되어야합니다 gunicorn
VIRTUALENV 바이너리를. 옵션을 사용하여 실행해야합니다 --bind /run/gunicorn.sock myproject.wsgi:application
. 여기서는 /run/gunicorn.sock
소켓 파일 경로이고 myproject.wsgi:application
Python 모듈 진입 점에 대한 상대 경로입니다 (디렉터리는 점으로 구분되고 확장자 .py
는 생략되고 모듈 이름은 콜론과 호출 가능한 application
변수 가 뒤 따름 ).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다