Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a django installation on my EC2 server which sometimes is working fine. But some other times i keep getting those 502 Bad Gateway errors which don't seem to be a fault in the code because when doing the same action using another browser or with a new session, the error goes away.

Here's what i have configured: in /etc/nginx/nginx.conf i have:

include /opt/nginx/sites-enabled/*;

in /opt/nginx/sites-enabled/mysite.conf

        server_name mysite.com;
        access_log /var/log/nginx/mysite.com.log;
        error_log /var/log/nginx/mysite.com.log;

        location /media {
            alias /opt/python/apps/mysite/media;

        location /static {
                alias /opt/python/apps/mysite/static;

        location /favicon.ico {
                alias /opt/python/apps/mysite/favicon.ico;

        location = /robots.txt {
                alias /opt/python/apps/mysite/robots.txt;

        location / {
            root /opt/python/apps/mysite/;
            uwsgi_pass   unix:///tmp/uwsgi_mysite.sock;
            include        uwsgi_params;


And finally, in my /etc/init/uwsgi_mysite.conf

# file: /etc/init/uwsgi.conf 
description "uWSGI server"

start on runlevel [2345]
stop on runlevel [!2345]

uwsgi --chdir=/opt/python/apps/mysite --module=wsgi:application --env DJANGO_SETTINGS_MODULE=settings --master --pidfile=/tmp/mysite-master.pid --socket= --processes=20 --uid=1000 --gid=2000 --harakiri=20 --limit-as=1024 --max-requests=5000 --vacuum --home=/opt/python/apps/mysite --daemonize=/var/log/uwsgi/mysite.log                     

Edit Checking the logs Sometimes showed the following:

"GET /admin/jsi18n/ HTTP/1.1" 502 574 "http://mysite.com/node/" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)"
2012/12/27 13:56:35 [error] 30773#0: *93 upstream prematurely closed connection while reading response header from upstream, client:, server: mysite.com, request: "GET /api/nodes/639/?page=3&format=json& HTTP/1.1", upstream: "uwsgi://unix:///tmp/uwsgi_mysite.sock:", host: "mysite.com"

Some other times:

"GET /admin/content_curation/node/?category=2&source=912 HTTP/1.1" 502 574 "http://mysite.com/admin/node/?category=2&source=912" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11"

Could someone tell me what am i doing wrong here? how can i fix this nginx 502 errors?

Thanks in advance

share|improve this question
did you bother to check your uwsgi and nginx logs? –  Mike Dec 27 '12 at 14:44
Yes, just added the logs :) –  Maverick Dec 27 '12 at 14:56
you need the uwsgi logs /var/log/uwsgi/mysite.log –  Mike Dec 27 '12 at 16:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.