Showing posts with label nginx. Show all posts
Showing posts with label nginx. Show all posts

Tuesday, January 1, 2013

deploying static files and django applications with Nginx

Just learned a trick to serve static files as well as django applications with nginx. In the following setup, nginx tries to serve static files first, if the requested file is not found, then it passes request to the (django) application server (gunicorn, if you wish).


    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://127.0.0.1:8080;
    }


If we want to enable user-dir mode, the configuration code should be put before these.

User-based website directory with Nginx

User-dir mode can be done by using regex captures. Here is an example to configure your nginx:

        location ~ ^/~(.+?)(/.*)?$ {
                alias /home/$1/public_html$2;
                index  index.html index.htm;
                autoindex on;
        }