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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Andre Lopes-2
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,

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

Reply | Threaded
Open this post in threaded view
|

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

DrBloodmoney
On Tue, Nov 29, 2011 at 3:05 PM, Andre Lopes <[hidden email]> 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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

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

Reinout van Rees
In reply to this post by Andre Lopes-2
On 29-11-11 21:05, Andre Lopes wrote:
> To put things working I should replace:
>
> [code]
>      root /path/to/test/hello;
> [/code]
>
> For the path where I have my Django Project?

You won't even use that root. That's for serving regular static files
and that's only needed for the /static_media and /media urls in Django
(depending on how you set them).

So you need "locations" for your static stuff and a "/" location that
proxies to gunicorn.

I've got an example below. I start gunicorn on port 11000 in this case:


server {
     listen 80;
     client_max_body_size 1G;
     server_name wgs.lizard.net;

     keepalive_timeout 5;
     access_log /Users/reinout/git/nens/wgs/var/log/access.log;
     error_log /Users/reinout/git/nens/wgs/var/log/error.log;

     location /static_media/ {
        # django-staticfiles
        alias /Users/reinout/git/nens/wgs/var/static/;
        expires max;
        # ^^^ Now that I give this example, I see that this 'max'
        # probably only has to be on /static_media/CACHE/ ...
     }

     location /media/ {
        alias /Users/reinout/git/nens/wgs/var/media/;
        expires 24h;
     }

     location / {
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_redirect off;
         proxy_pass <a href="http://localhost:11000;">http://localhost:11000;
     }
}



Reinout

--
Reinout van Rees                    http://reinout.vanrees.org/
[hidden email]             http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

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