我正在尝试设置 Django、Gunicorn 和 nginx。
\n\n我已经配置了 Gunicorn 以便运行,但似乎我在 nginx 配置使用 Gunicorn 时遇到了问题。
\n\n这是我的配置:
\n\n[Unit]\nDescription=gunicorn daemon\nRequires=socket\nAfter=network.target\n\n[Service]\nPIDFile=/run/gunicorn/pid\nUser=root\nGroup=root\nRuntimeDirectory=gunicorn\nWorkingDirectory=/srv/myproject/current\nExecStart=/srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid \\\n --bind unix:/run/gunicorn/socket myapp.wsgi:application\nExecReload=/bin/kill -s HUP $MAINPID\nExecStop=/bin/kill -s TERM $MAINPID\nPrivateTmp=true\n\n[Install]\nWantedBy=multi-user.target\n
Run Code Online (Sandbox Code Playgroud)\n\nGunicorn 状态看起来像这样
\n\ngunicorn.service - gunicorn daemon\n Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)\n Active: active (running) since Thu 2018-01-18 23:32:11 UTC; 3min 23s ago\n Process: 6347 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)\n Main PID: 6355 (gunicorn)\n Tasks: 2\n Memory: 195.7M\n CPU: 1.426s\n CGroup: /system.slice/gunicorn.service\n \xe2\x94\x9c\xe2\x94\x806355 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:application\n \xe2\x94\x94\xe2\x94\x806360 /srv/venvs/myenv/bin/python3.6 /srv/venvs/myenv/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket myapp.wsgi:application\n\nJan 18 23:32:11 python-server systemd[1]: Stopped gunicorn daemon.\nJan 18 23:32:11 python-server systemd[1]: Started gunicorn daemon.\nJan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Starting gunicorn 19.7.1\nJan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Listening at: unix:/run/gunicorn/socket (6355)\nJan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6355] [INFO] Using worker: sync\nJan 18 23:32:11 python-server gunicorn[6355]: [2018-01-18 23:32:11 +0000] [6360] [INFO] Booting worker with pid: 6360\n
Run Code Online (Sandbox Code Playgroud)\n\n我的 nginx 配置
\n\nserver {\n server_tokens off;\n listen 443 ssl;\n server_name myserver.com;\n keepalive_timeout 70;\n\n ssl_certificate /etc/ssl/certs/myserver.com.merged.crt;\n ssl_certificate_key /etc/ssl/private/myserver.com.key;\n\n ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n ssl_ciphers HIGH:!aNULL:!MD5;\n\n #access_log /var/log/nginx/access.myserver.log;\n access_log /var/log/nginx/access.myserver.log;\n #error_log /var/log/nginx/error.myserver.log;\n error_log /var/log/nginx/error.myserver.log;\n\n location / {\n include proxy_params;\n proxy_pass http://unix:/run/gunicorn/socket;\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n在路径上/run/gunicorn
我只能看到pid
文件。文件socket
从未创建过。
这是错误日志的样子
\n\n2018/01/18 23:09:00 [crit] 5764#5764: *1 connect() to unix:/run/gunicorn/socket failed (2: No such file or directory) while connecting to upstream, client: 212.251.167.250, server: my-server.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn/socket:/",, host: "myserver.com"\n
Run Code Online (Sandbox Code Playgroud)\n\n谁能看出这里出了什么问题吗?为什么socket
不建文件而pid
建文件?
正如日志条目中所示,gunicorn实际打开的套接字与您的 systemd 单元中配置的套接字不同。
该日志展示了 Gunicorn 实际做了什么:
Jan 18 23:08:49 python-server gunicorn[5858]: [2018-01-18 23:08:49 +0000] [5858] [INFO] Listening at: unix:/run/gunicorn/socket (5858)
Run Code Online (Sandbox Code Playgroud)
请注意,它确实打开了/run/gunicorn/socket
。但是您的 nginx 配置和 systemd 单元指定为 open /run/gunicorn/gunicorn.socket
,这是一个不同的路径。
我的第一个想法是您可能更改了 systemd 单元中的路径,但尚未运行systemctl daemon-reload
. 在运行此命令(或重新启动)之前,systemd 单元中的任何更改都不会生效。
因此,我会重新加载 systemd,然后尝试重新启动 Gunicorn。
systemctl daemon-reload
systemctl restart gunicorn.service
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34250 次 |
最近记录: |