1

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 http://127.0.0.1 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; 
                uwsgi_pass    127.0.0.1:9001;
            } 

            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 0.0.0.0:80, ignored" error.
  3. Read a bunch more guides on Nginx + uWSGI setups without getting any further in solving the issue.
vjk2005
  • 175
  • 1
  • 2
  • 8
  • 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 05 '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 05 '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 05 '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 05 '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 05 '11 at 13:33
  • Still nothing in the error log. How do I move the log to a higher block? – vjk2005 Jun 05 '11 at 13:37
  • Move the line to below the `worker_processes` line. Blocks are the named entities - location is the lowest, then server, http, or events. Being outside any is the highest-level block. – Cyclops Jun 05 '11 at 13:41
  • Still no luck. Thanks for the tip about the blocks. – vjk2005 Jun 05 '11 at 13:46
  • Sorry it didn't help, no clue what's wrong. Only thing I can suggest is simplifying - cut out everything unnecessary, like both fastcgi and static files, just get the localhost working, then add things back one at a time. (My config was also a major pain to get working, so you're not the only one. :) – Cyclops Jun 05 '11 at 13:51
  • One last thing :) I'm presuming you didn't see any useful error messages in the `nginx` log. If so, have you tried logging `uwsgi`? Command line would be `uwsgi --logto foo.log`. – Cyclops Jun 05 '11 at 14:18
  • I just did another round of the internet to see if I can find something useful but haven't turned up anything yet. The basic localhost works, like I mentioned in the post - it says "welcome to nginx". But when I add the uWSGI stuff (I'm not using fastcgi) to the nginx.conf, things turn bad. I logged the uWSGI, made an error on purpose to see if the error was getting logged (which it did) & then ran uWSGI as usual after cleaning up the log - still nothing. Can't wait to see my apps running on my own server. Gonna run out into the webs & search some more. Thanks for all your suggestions & tips. – vjk2005 Jun 05 '11 at 14:36
  • Dunno if you've found the problem. If not, here's a few more thoughts. First, you didn't mention `virtualenv`, so I presume you're not running it? Second, all of the "502 bad gateway" problems I've seen, mention the backend (usually Apache) as being the problem - so I think `nginx` is fine, and it's something in the configuration of `uWSGI`. I looked at the .sh at that link, didn't see anything jump out at me :) . What was the error you saw in the log? – Cyclops Jun 08 '11 at 01:14

1 Answers1

2

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:

sys.path.append('/var/www/myproject/')

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