| 1 | | nginx is a reverse proxy and needs to connect to a server that contains django. This guide is for using [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/ fastcgi], but you could instead use |
| 2 | | [http://bartek.im/blog/2009/04/06/setting-up-nginx-django.html apache+mod_wsgi], |
| 3 | | [http://senko.net/en/django-nginx-gunicorn/ gunicorn], |
| 4 | | [https://www.google.com/search?q=nginx+uwsgi+django uwsgi], or |
| 5 | | [http://www.rkblog.rk.edu.pl/w/p/django-nginx/#3 SCGI+Cherokee] |
| | 1 | This page focused on fastcgi which is now deprecated. |
| 7 | | ---- |
| 8 | | This assumes you are using ubuntu 10.04 or later. |
| 9 | | |
| 10 | | You'll need nginx and flup installed. |
| 11 | | {{{ |
| 12 | | sudo aptitude install nginx python-flup |
| 13 | | }}} |
| 14 | | |
| 15 | | In your django project, start up fastcgi. See the [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/ fastcgi docs] for more info. |
| 16 | | {{{ |
| 17 | | python ./manage.py runfcgi host=127.0.0.1 port=8080 |
| 18 | | }}} |
| 19 | | |
| 20 | | |
| 21 | | Next, we need to create a configuration file in `/etc/nginx/sites-available/sample_project.conf` and symlink it to `/etc/nginx/sites-enabled/sample_project.conf`: |
| 22 | | {{{ |
| 23 | | sudo touch /etc/nginx/sites-available/sample_project.conf |
| 24 | | sudo ln -s /etc/nginx/sites-available/sample_project.conf /etc/nginx/sites-enabled/sample_project.conf |
| 25 | | }}} |
| 26 | | |
| 27 | | Here is what a minimal conf file would look like: |
| 28 | | {{{ |
| 29 | | server { |
| 30 | | listen 80; |
| 31 | | server_name myhostname.com; |
| 32 | | access_log /var/log/nginx/sample_project.access.log; |
| 33 | | error_log /var/log/nginx/sample_project.error.log; |
| 34 | | |
| 35 | | # https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production |
| 36 | | location /static/ { # STATIC_URL |
| 37 | | alias /home/www/myhostname.com/static/; # STATIC_ROOT |
| 38 | | expires 30d; |
| 39 | | } |
| 40 | | |
| 41 | | location /media/ { # MEDIA_URL |
| 42 | | alias /home/www/myhostname/static/; # MEDIA_ROOT |
| 43 | | expires 30d; |
| 44 | | } |
| 45 | | |
| 46 | | location / { |
| 47 | | include fastcgi_params; |
| 48 | | fastcgi_pass 127.0.0.1:8080; |
| 49 | | } |
| 50 | | } |
| 51 | | }}} |
| 52 | | |
| 53 | | Now, reload nginx. |
| 54 | | {{{ |
| 55 | | sudo /etc/init.d/nginx reload |
| 56 | | }}} |
| 57 | | |
| 58 | | If your urls are not working correctly, you may need to add this line to `location /`: |
| 59 | | {{{ |
| 60 | | fastcgi_split_path_info ^()(.*)$; |
| 61 | | }}} |
| 62 | | |
| 63 | | You may use a unix socket instead of a port number like so: `fastcgi_pass unix:/home/www/myhostname/fastcgi.sock` |
| 64 | | |
| 65 | | You'll probably want to keep your fastcgi process running using [http://blog.bashton.com/2011/using-django-fastcgi-with-upstart/ upstart], [https://code.djangoproject.com/wiki/InitdScriptForLinux init.d], or [http://web.archive.org/web/20120113102046/http://just-another.net/byteflowdjango-supervisord-nginx-win supervisor] (archive.org) |
| 66 | | |
| 67 | | See also: |
| 68 | | - [http://serverfault.com/a/370573/84192 serverfault question on django+nginx] |
| 69 | | - [http://wiki.nginx.org/DjangoFastCGI nginx wiki page on django+FastCGI] |
| 70 | | - [http://eth0.pro/post/1 django project at nginx+uwsgi with virtualenv] |
| | 3 | Look at the history if you're interested in the content that used to be there. |