Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-gunicorn-nginx: 502 bad gateway

I'm trying to ship my web application to the server and this is my first time configuring the server. I'm using django-gunicorn-nginx setup by following this tutorial http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup/ First everything was perfect and I got the django welcome page. Then I loaded the apps in the django project and setup the static root and Now I'm getting 502 bad gateway You can check out in http://qlimp.com

Everything upto the gunicorn and supervisor setup is the same as shown in that tutorial. But I modified some nginx conf. Here it is:

upstream app_server_djangoapp {
    server localhost:8001 fail_timeout=0;
}

server {
    listen 80;
    server_name qlimp.com;

    access_log  /var/log/nginx/guni-access.log;
    error_log  /var/log/nginx/guni-error.log info;

    keepalive_timeout 5;

    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://app_server_djangoapp;
            break;
        }
    }
    location /files/ {
        autoindex on;
        root /home/nirmal/qlimp/qlimp/files/;
    }
}

Here is my Media url:

MEDIA_URL = '/files/'

Files is the folder where I'm having all the static files. How can I get my project to work in the server? Could anyone guide me?

UPDATE

Errors.log https://gist.github.com/2768425

Thanks!

like image 390
rnk Avatar asked May 22 '12 10:05

rnk


2 Answers

First. Don't use if in an nginx conf. It's bad. Like really, really horrible. Use the following instead:

location / {
    try_files $uri @proxy;
}

location @proxy {
    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_djangoapp;
}

See: http://wiki.nginx.org/IfIsEvil and http://wiki.nginx.org/Pitfalls

Now, as far as debugging goes. Your gunicorn workers are booting because there's some fatal error. Try shutting down gunicorn. If you're using supervisor:

sudo supervisorctl stop [gunicorn process name]

Then, from your project root run:

python manage.py run_gunicorn -c path/to/gunicorn.conf

Note any startup errors or if it actually boots, test your site in the browser. If you're still not getting any meaningful info try just running the standard runserver

python manage.py runserver

Again, note any errors and if it loads fine, test your site in the browser. I suggest testing on localhost:8000 like you would in development. One of these should give you something to work with.

UPDATE

The error you're getting says it can't connect to "ind=127.0.0.1". Then, looking at the command you're running, gunicorn_django -bind=127.0.0.1:8001, it's easy to see the problem. You can specify the IP and port to bind to with either -b or --bind. Since you only used one - it's interpreting the IP as ind=127.0.0.1, which is obviously not correct. You need to use:

gunicorn_django --bind=127.0.0.1:8001

Or

gunicorn_django -b 127.0.0.1:8001
like image 87
Chris Pratt Avatar answered Nov 19 '22 19:11

Chris Pratt


You need to understand directives properly. "server_name" directive holds the IP address and "proxy_pass" will connect to port where your server is hosted. In your case:

server_name 127.0.0.1;
proxy_pass http://127.0.0.1:8001;

There is no reason for this not to work but still if it does not then try "python manage.py runserver" command to make sure that your site runs with no error because in case site is not able to present data to wsgi.py that likely to show same error.

like image 2
Mudassar Hashmi Avatar answered Nov 19 '22 17:11

Mudassar Hashmi