我试图通过nginx和gunicorn在VPS服务器上部署django项目,但是我得到了一个nginx错误' 502坏网关'当我尝试打开我的页面rollservice-rostov.ru
我的Nginx配置出了问题,但我无法弄清楚它是什么。
/etc/nginx/sites-enabled/rollservice.conf:
upstream rollservice-rostov.ru {
server 127.0.0.1:8010 fail_timeout=0;
}
server {
listen 80;
server_name www.rollservice-rostov.ru;
rewrite ^/(.*) http://rollservice-rostov.ru/$1 permanent;
client_max_body_size 4G;
access_log /home/django/logs/rollservice/nginx.access.log;
error_log /home/django/logs/rollservice/nginx.error.log;
location /static/ {
alias /home/django/projects/rollservice/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://rollservice-rostov.ru;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/django/projects/rollservice/templates/;
}
}
/etc/supervisor/conf.d/rollservice.conf:
[program:rollservice]
command=/home/django/projects/rollservice/run/run.sh
directory=/home/django/projects/rollservice
user=django
autostart=true
autorestart=true
stderr_logfile=/home/django/logs/rollservice/gunicorn_errors.log
stdout_logfile=/home/django/logs/rollservice/gunicorn_access.log
/home/django/projects/rollservice/run/run.sh:
#!/bin/bash
NAME="rollservice"
SRCDIR=rollservice
HOMEDIR=/home/django
PROJECTDIR=${HOMEDIR}/projects/${NAME}
VIRTUALENV=${HOMEDIR}/.envs/${NAME}
SOCKFILE=${HOMEDIR}/tmp/${NAME}.sock
USER=django
GROUP=django
NUM_WORKERS=3
DJANGO_SETTINGS=rollservice.settings
DJANGO_WSGI_MODULE=rollservice.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $PROJECTDIR
source ${VIRTUALENV}/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS
export PYTHONPATH=$PROJECTDIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ${VIRTUALENV}/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=127.0.0.1:8010 \
--log-level=debug \
--log-file=$HOMEDIR/logs/
虽然,我的网页已成功通过
打开lynx http://127.0.0.1:8010
这是最后一次错误记录:
rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
[vps]rollservice> sudo tail /var/log/nginx/error.log
2014/10/12 23:13:27 [error] 11106#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:16:46 [error] 11295#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:35:22 [error] 11905#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET / HTTP/1.1", upstream: "http://185.20.226.216:81/", host: "185.20.226.216"
2014/10/12 23:35:22 [error] 11905#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"
2014/10/12 23:35:22 [error] 11905#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"
答案 0 :(得分:1)
我认为它的Django配置相关: 如果您将DEBUG设置为False,则使用ALLOWED_HOSTS
允许任何域名
ALLOWED_HOSTS = ['*',]
仅允许您的域名
ALLOWED_HOSTS = ['rollservice-rostov.ru',]
配置文件settings.py可能会显示:
...
DEBUG = False
ALLOWED_HOSTS = ['rollservice-rostov.ru',]
...
要包含子域名,请添加"。"在开始
ALLOWED_HOSTS = ['.rollservice-rostov.ru',]
此外,在Django 1.7及更多
中ALLOWED_HOSTS = [
'.rollservice-rostov.ru', # Allow domain and subdomains
'.rollservice-rostov.ru.', # Also allow FQDN and subdomains
]
检查Django文档 https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
答案 1 :(得分:1)
由于上游命名,proxy_pass
传递给FQDN时出现问题。
if (!-f $request_filename) {
proxy_pass http://rollservice-rostov.ru;
break;
}
它直接传递给那个。更改上游的名称,你应该没问题。
乔