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:
- Starting, stopping and restarting nginx and uWSGI services after modifying their configs.
- 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. - Read a bunch more guides on Nginx + uWSGI setups without getting any further in solving the issue.
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:
okay? Well, the thing is that the path is not well defined for uwsgi. You should now define this like that:
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