After a long time on shared hosting, I'm moving my stuff to a VPS and it has become necessary to learn about Nginx + uWSGI to deploy my apps (python). After spending a couple of weeks learning the basics, I'm in the process of setting up my local machine (ubuntu 11.04) to run my apps on Nginx + uWSGI. I'm using the "Hello world" Ubuntu 10.10 linode guide.

The setup was simple but when I run http://localhost/ or I get a 502 Bad Gateway everytime. Appreciate pointers on how to get the setup working.

My nginx.conf: [ I backed up the default nginx conf (which works fine and shows "Welcome to Nginx" when I hit http://localhost/) and replaced it with this custom nginx conf from the linode guide that links nginx to the uWSGI server. ]

    worker_processes  1; 
    events { worker_connections  1024; } 
    http { 
        server { 
            listen 80;
            server_name localhost; 
            access_log /srv/www/myHostname/logs/access.log; 
            error_log /srv/www/myHostname/logs/error.log; 

            location / { 
                include  uwsgi_params; 

            location /static { 
                root   /srv/www/myHostname/public_html/static/;
                index  index.html index.htm;

My uWSGI conf is exactly the same as detailed in the linode guide I linked above, with the one change that "duckington.org" in their example replaced with "myHostname" in my setup. No errors in my nginx error log. Nginx installed at /opt/nginx and uWSGI is at /opt/uwsgi as laid out in the guide linked above. I haven't touched any files the guide doesn't talk about.

What I have tried to solve this, so far:

  1. Starting, stopping and restarting nginx and uWSGI services after modifying their configs.
  2. Tried the 'debian layout' with sites-enabled etc to add my custom vhost listed above while leaving the default nginx.conf untouched (except for the include statement to point to the vhost in sites-enabled). Nginx did not even start and the error log reported a "conflicting server name "localhost" on, ignored" error.
  3. Read a bunch more guides on Nginx + uWSGI setups without getting any further in solving the issue.
  • I don't know if it's a typo, but your post first shows http:/localhost, with only one slash, and the default version shows two slashes. Also there should be a trailing slash, http://localhost/ . Also, nginx -t will test your config without actually starting.
    – Cyclops
    Jun 5 '11 at 13:18
  • That's on purpose as I recall from SO that new users can't post more than 1 link. Deleted a / so SF didn't auto-detect and add a second link, and then prompt me that new users can't post more than 1 link.
    – vjk2005
    Jun 5 '11 at 13:20
  • Ah - then try putting apostrophes ` around it to block it, like I did to make http://localhost/ not becoming a link. Also, it still needs a trailing slash, did you also remove that?
    – Cyclops
    Jun 5 '11 at 13:22
  • Ah, thanks for the tip. Left the trailing slash out as browsers auto-add the slash in when going to localhost. Added it back in now.
    – vjk2005
    Jun 5 '11 at 13:28
  • I'm guessing it didn't work :) Well, try changing the error log level - error_log foo.log warn;, to get more information - I think the default level is crit, which may not be showing the problem. Then see what's in the error log when you hit the page. (It might also be worth moving the error_log to a higher block - where it is only gets server errors).
    – Cyclops
    Jun 5 '11 at 13:33

I saw your post yesterday, just searching for an answer for exactly the same issue. Finally, today I reached the root of the problem.

I suppose you specify the chdir in your .ini config file as the directory where your project is located. I mean that if for example your project is 'myproject' and you have it in '/var/www/myproject' directory, you specify '/var/www' as the chdir.

So, the path for all internal resources is not well defined, and Python interpereter (may be you are using Django?) does not reach them. Ok, I will explain how a solution works in a Django project. For example, suppose that you have an app inside your project called 'app1'; in your views module you are calling for a forms defined in your forms module; you will be doing this like that:

from app1.forms import *

okay? Well, the thing is that the path is not well defined for uwsgi. You should now define this like that:

from myproject.app.forms import *

and you will see that everything is working now. No more 502 Bad Gateway errors will appear for you :)

Yes, I know that is not very elegant to add 'myproject.' to every internal resource calling. So, you can simply add this to your 'settings.py' file:


Substitute '/var/www/' for the path where your project is located on your machine. With this tiny solution, every started working for me :)

I hope my comment helps you.

Cheers, Jose

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.