Reputation: 421
I'm trying to deploy a django project on VPS server via nginx and gunicorn, but I got an nginx error '502 bad gateway' when I try to open my page rollservice-rostov.ru
Something is wrong with my Nginx configuration, but I can't figure out what it is.
/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/
Although, my page opens successfully via
lynx http://127.0.0.1:8010
This is last error logs:
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"
Upvotes: 1
Views: 7040
Reputation: 3958
The problem looks to be with the proxy_pass
is passing to a FQDN due to the naming of your up stream.
if (!-f $request_filename) {
proxy_pass http://rollservice-rostov.ru;
break;
}
It is passing it straight to that. Change the name of the upstream and you should be okay.
Joe
Upvotes: 1
Reputation: 844
i think it's Django configuration related: if you have set DEBUG to False , ALLOWED_HOSTS is used
To allow any domain name
ALLOWED_HOSTS = ['*',]
To allow only your domain name
ALLOWED_HOSTS = ['rollservice-rostov.ru',]
Config file settings.py may look :
...
DEBUG = False
ALLOWED_HOSTS = ['rollservice-rostov.ru',]
...
To include subdomains , add "." at start
ALLOWED_HOSTS = ['.rollservice-rostov.ru',]
Also , in Django 1.7 and more
ALLOWED_HOSTS = [
'.rollservice-rostov.ru', # Allow domain and subdomains
'.rollservice-rostov.ru.', # Also allow FQDN and subdomains
]
Check Django docs https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
Upvotes: 1