Tuesday, November 29, 2011

Re: Nginx - How to configure the virtual.conf to serve a Django application

On Tue, Nov 29, 2011 at 3:05 PM, Andre Lopes <lopes80andre@gmail.com> wrote:
> I'm trying to serve a Django application to production. I have
> installed Gunicorn and now I need to setup a Nginx Virtual Host. I'm
> kind of lost on how to configure the "virtual.conf" from Nginx. I just
> want to use Nginx as proxy, I don't want to configure deplyment
> scripts for now.
>
> I have this layout taken from here(http://senko.net/en/django-nginx-gunicorn/):
>
> [code]
>        server {
>            listen   80;
>            server_name example.com;
>            # no security problem here, since / is alway passed to upstream
>            root /path/to/test/hello;
>            # serve directly - analogous for static/staticfiles
>            location /media/ {
>                # if asset versioning is used
>                if ($query_string) {
>                    expires max;
>                }
>            }
>            location /admin/media/ {
>                # this changes depending on your python version
>                root /path/to/test/lib/python2.6/site-packages/django/contrib;
>            }
>            location / {
>                proxy_pass_header Server;
>                proxy_set_header Host $http_host;
>                proxy_redirect off;
>                proxy_set_header X-Real-IP $remote_addr;
>                proxy_set_header X-Scheme $scheme;
>                proxy_connect_timeout 10;
>                proxy_read_timeout 10;
>                proxy_pass http://localhost:8000/;
>            }
>            # what to serve if upstream is not available or crashes
>            error_page 500 502 503 504 /media/50x.html;
>        }
> [/code]
>
> To put things working I should replace:
>
> [code]
>    root /path/to/test/hello;
> [/code]
>
> For the path where I have my Django Project?
>
> I have Django and all other Python scripts installed in a virtualenv.
>
> Can someone give me some gidelines to get Django working with Gunicorn
> with Nginx as proxy?
>
> Best Regards,


I use uwsgi but the process is the same. Basically gunicorn will be
serving your application up on localhost:PORT (in the example
http://localhost:8000). So first you need to configure gunicorn to do
that. They have a 'deploy' section on their site that has a sample
nginx config. I would base my files off that. But the basic scenario
is gunicorn serving your django app (Nginx knows nothing about django)
+ supervisord (to make sure gunicorn stays running) + Nginx who is
listening for requests with your django url ('/' in your example file)
to proxy that request back to gunicorn. All of the django-specific
stuff is going to be handled by gunicorn (ie. completely different
than serving php out of Apache via mod_php).

Basically install gunicorn (pip install gunicorn). Copy the
gunicorn_django script out of the install path for gunicorn and plop
it into your django project's root (ie. where your settings.py file)
is and run that script (./gunicorn_django -b localhost:8000 for your
example). Once you confirm that it is working and serving requests
proxied back from nginx then you can worry about getting supervisord
to monitor your process and restart it if it stops.

Gunicorn is probably slightly easier than getting uwsgi going so I'd
stay with that before you switch.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment