Hosting Django applicaton - Django uWSGI Nginx Server Config

April 16, 2014 5.5k views
Im following this tutorial to the letter but I'm having problems with the application I'm getting 502 Nginx error http://blog.richard.do/index.php/2013/04/setting-up-nginx-django-uwsgi-a-tutorial-that-actually-works/#comment-116 could someone help? I've been trying to rectify this for sometime now but i can see a way of fixing it. so thought id ask here.
4 Answers
If you could provide some more information, it would make it easier for us to know what the issue might be. Could you post any error messages in your nginx and uwsgi logs? Following the tutorial that you linked, they would be in:

/var/www/example.com/logs/error.log
/var/log/uwsgi/example_project.log

The nginx log's default location is /var/log/nginx/error.log
@astarr sure,

************************************************************************************
/var/www/example.com/conf/nginx.conf
************************************************************************************

server {
listen 80;
server_name khcardsonline.co.uk www.khcardsonline.co.uk;
root /var/www/example.com/src/khcards;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;

location /static/ { # STATIC_URL
alias /var/www/example.com/src/static/static/; # STATIC_ROOT
expires 30d;
}

location /media/ { # MEDIA_URL
alias /var/www/example.com/src/media/; # MEDIA_ROOT
expires 30d;
}

location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8889;
}
}


************************************************************************************
/var/www/example.com/conf/uwsgi.ini
************************************************************************************

[uwsgi]
# variables
projectdomain = khcardsonline.co.uk www.khcardsonline.co.uk

uid = www-data
god = www-data
chmod = 775

# config
plugins = python
master = true
pidfile=/tmp/project-master.pid
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=khcards.settings
pythonpath = /var/www/example.com/src/khcards
module = khcards.wsgi:application
socket = 127.0.0.1:8889
logto = /var/www/example.com/src/logs/uwsgi.log
#below line runs it as a daemon in background
daemonize = /var/log/uwsgi/khcards.log

************************************************************************************
/var/www/example.com/logs/uwsgi.log
************************************************************************************

*** Starting uWSGI 1.0.3-debian (64bit) on [Wed Apr 16 08:57:08 2014] ***
compiled with version: 4.6.3 on 17 July 2012 02:26:54
current working directory: /var/www/example.com
detected binary path: /usr/bin/uwsgi-core
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your memory page size is 4096 bytes
uwsgi socket 0 inherited INET address 127.0.0.1:8889 fd 4
Python version: 2.7.3 (default, Feb 27 2014, 20:09:21) [GCC 4.6.3]
Python main interpreter initialized at 0x2457ed0
your server socket listen backlog is limited to 100 connections
*** Operational MODE: single process ***
added /var/www/example.com/src/khcards/ to pythonpath.
ImportError: No module named khcards.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
gracefully (RE)spawned uWSGI master process (pid: 12881)
spawned uWSGI worker 1 (pid: 14856, cores: 1)


************************************************************************************
/var/www/example.com/logs/error.log *sample*
************************************************************************************
2014/04/16 08:59:00 [error] 787#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 200.29.238.180, server: khcardsonline.co.uk, request: "HEAD / HTTP/1.0", upstream: "uwsgi://127.0.0.1:8889"

************************************************************************************
/var/log/uwsgi/logs/example_project.log
************************************************************************************

*** Starting uWSGI 1.0.3-debian (64bit) on [Wed Apr 16 08:32:32 2014] ***
compiled with version: 4.6.3 on 17 July 2012 02:26:54
current working directory: /var/www/example.com
detected binary path: /usr/bin/uwsgi-core
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your memory page size is 4096 bytes
uwsgi socket 0 bound to TCP address 127.0.0.1:8889 fd 4
Python version: 2.7.3 (default, Feb 27 2014, 20:09:21) [GCC 4.6.3]
Python main interpreter initialized at 0x1286f10
your server socket listen backlog is limited to 100 connections
*** Operational MODE: single process ***
added /var/www/example.com/src/khcards to pythonpath.
ImportError: No module named khcards.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 12881)
spawned uWSGI worker 1 (pid: 12882, cores: 1)
[pid: 12882|app: -1|req: -1/1] 81.111.153.236 () {36 vars in 584 bytes} [Wed Apr 16 08:32:47 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/2] 81.111.153.236 () {38 vars in 585 bytes} [Wed Apr 16 08:32:47 2014] GET /favicon.ico => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/3] 81.111.153.236 () {36 vars in 569 bytes} [Wed Apr 16 08:32:49 2014] GET /apple-touch-icon-precomposed.png => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/4] 81.111.153.236 () {36 vars in 545 bytes} [Wed Apr 16 08:32:49 2014] GET /apple-touch-icon.png => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/5] 81.111.153.236 () {36 vars in 584 bytes} [Wed Apr 16 08:33:12 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/6] 81.111.153.236 () {38 vars in 585 bytes} [Wed Apr 16 08:33:12 2014] GET /favicon.ico => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/7] 81.111.153.236 () {36 vars in 569 bytes} [Wed Apr 16 08:33:14 2014] GET /apple-touch-icon-precomposed.png => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/8] 81.111.153.236 () {36 vars in 545 bytes} [Wed Apr 16 08:33:14 2014] GET /apple-touch-icon.png => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/9] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:37:39 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/10] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:02 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/11] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:03 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/12] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:04 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/13] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:04 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/14] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:04 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/15] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:42:05 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/16] 115.239.248.112 () {34 vars in 486 bytes} [Wed Apr 16 08:43:25 2014] GET /manager/html => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/17] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:11 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/18] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:13 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/19] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:13 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/20] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:13 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/21] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:14 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/22] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:14 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/23] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:14 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/24] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:14 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/25] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:14 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/26] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:15 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/27] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:50:15 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/28] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:51:40 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/29] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:51:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/30] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:51:44 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/31] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:51:44 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/32] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:51:44 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/33] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:40 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/34] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:41 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/35] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/36] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/37] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/38] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/39] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:42 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/40] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/41] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/42] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/43] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/44] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/45] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:43 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/46] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:44 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
[pid: 12882|app: -1|req: -1/47] 81.111.153.236 () {38 vars in 651 bytes} [Wed Apr 16 08:56:44 2014] GET / => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
...brutally killing workers...
binary reloading uWSGI...
chdir() to /var/www/example.com
closing all non-uwsgi socket fds > 2 (_SC_OPEN_MAX = 1024)...
found fd 4 mapped to socket 0 (127.0.0.1:8889)
running /usr/bin/uwsgi-core
[uWSGI] getting INI configuration from /var/www/example.com/conf/uwsgi.ini

Let me know if you would like anything else
Is there a wsgi.py file in you Django project? It doesn't seem to be getting imported:


added /var/www/example.com/src/khcards to pythonpath.
ImportError: No module named khcards.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
Yeah its within

/var/www/example.com/src/khcards

so full path is

/var/www/example.com/src/khcards/wsgi.py
Have another answer? Share your knowledge.