Hamedio
Hamedio

Reputation: 117

502 Bad Gateway in django app on vps why?

I am trying to deploy a Django project but I get 502 Bad Gateway I used this tutorial

I used supervisor, Gunciorn, and Nginx

./virtualenvs/legaland_env/bin/gunicorn

#!/bin/bash
NAME="django_project"
DIR=/home/django/django_project
USER=django
GROUP=django
WORKERS=3
BIND=unix:/home/django/run/gunicorn.sock
DJANGO_SETTINGS_MODULE=django_project.settings
DJANGO_WSGI_MODULE=django_project.wsgi
LOG_LEVEL=error

cd $DIR
source ../bin/activate

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DIR:$PYTHONPATH

exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $WORKERS \
  --user=$USER \
  --group=$GROUP \
  --bind=$BIND \
  --log-level=$LOG_LEVEL \
  --log-file=-

/etc/supervisor/conf.d/sqh.conf

[program:sqh]

startsecs=0
command=/home/admin/legaland/virtualenvs/legaland_env/bin/gunicorn
user=admin
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/admin/legaland/gunicorn-error.log

/etc/nginx/sites-available/sqh

upstream app_server {
    server unix:/home/admin/legaland/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;

    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    server_name ;

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /home/admin/legaland/logs/nginx-access.log;
    error_log /home/admin/legaland/logs/nginx-error.log;

    location /static/ {
        alias /home/admin/legaland/Legaland/src/static_root/;
    }

    # checks for static file, if not found proxy to app
    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}

Upvotes: 0

Views: 1521

Answers (1)

perly
perly

Reputation: 11

Had a similar issue using the hosted platform heroku. I also got 502 bad gateway and the fix was to run the web application locally. You have to specify in the nginx config file these settings such as:

Using TCP/IP Connection:

upstream localhost {
    # server unix:/tmp/nginx.socket fail_timeout=0;
    server 127.0.0.1:8000 
}

location / {
        # Uncomment this if statement to force SSL/redirect http -> https
        # if ($http_x_forwarded_proto != "https") {
        #   return 301 https://$host$request_uri;
        # }

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host 127.0.0.1:8000;
        proxy_redirect off
        proxy_pass http://localhost;
    }

Then in your gunicorn config file:

    command='/myapp/django_env/bin/gunicorn'
    pythonpath='/myapp'
    bind='localhost'
    workers=1

The Procfile:

web: gunicorn -c conf/gunicorn_config.py myproject.wsgi
worker: service nginx start

Last, type the following command in bash to run the heroku app:

heroku local

Upvotes: 1

Related Questions