使用nginx和gunicorn设置django我已经能够为它提供静态文件但在django上带来了502。该网站运行正常runserver 0.0.0.0:8000
我之前也曾在uwsgi上工作过,我不认为这两个(gunicorn和uwsgi)是冲突的。
当我运行gunicorn状态检查时,我得到了
ubuntu@ip-172-31-16-133:~$ sudo systemctl status gunicorn
● gunicorn.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2017-01-31 10:36:23 UTC; 4min 20s ago
Process: 32261 ExecStart=/home/ubuntu/djangoenv/bin/gunicorn --workers 10 --bind unix:/home/ubu
Main PID: 32261 (code=exited, status=203/EXEC)
Jan 31 10:36:23 ip-172-31-16-133 systemd[1]: Started gunicorn daemon.
Jan 31 10:36:23 ip-172-31-16-133 systemd[1]: gunicorn.service: Main process exited, code=exited,
Jan 31 10:36:23 ip-172-31-16-133 systemd[1]: gunicorn.service: Unit entered failed state.
Jan 31 10:36:23 ip-172-31-16-133 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
,错误日志读取
2017/01/31 10:36:31 [crit] 32205#32205: *7 connect() to unix:/home/ubuntu/webapps/kenyabuzz/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 105.231.127.174, server: kenyabuzz.nation.news, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/webapps/kenyabuzz/gunicorn.socket:/", host: "kenyabuzz.nation.news"
这是conf文件
#kb gunicorn nginx settings
server {
listen 80;
server_name kenyabuzz.nation.news;
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/webapps/kenyabuzz/kb/media; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/webapps/kenyabuzz/kb/static; # your Django project's static files - amend as required
}
location /favicon.ico {
alias /home/ubuntu/webapps/kenyabuzz/kb/static/kb/favicon.ico; # favicon
}
#location / {
# include proxy_params;
# proxy_pass http://unix:/home/ubuntu/webapps/kenyabuzz/kb.sock;
#}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
upstream app_server {
server unix:/home/ubuntu/webapps/kenyabuzz/gunicorn.socket fail_timeout=0;
}
更新
Gunicorn服务设置
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/webapps/kenyabuzz
ExecStart=/home/ubuntu/djangoenv/bin/gunicorn --workers 10 --bind unix:/home/ubuntu/kenyabuzz/kb.sock kb.wsgi:application
[Install]
WantedBy=multi-user.target
更新
使用gunicorn kb.wsgi:application --bind 0.0.0.0:8001
运行gunicorn返回
WSGI without exception
kb.wsgi文件不存在也可能是问题
答案 0 :(得分:2)
您的/etc/systemd/system/gunicorn.service ExecStart命令似乎是一个问题,请尝试从命令行运行相同的命令对其进行故障排除。
ExecStart示例:
ExecStart=/home/user/Virtualenvs/app_virtualenv/bin/gunicorn --workers 3 --bind unix:/var/local/django/app.sock app.wsgi:application
您可能还需要为服务设置用户,组和工作目录:
[Service]
User=yourAppUser
Group=yourAppGroup
WorkingDirectory=YourAppWD(where manage.py is located)
ExecStart=...