Question

Nginx for django app and pelican website?

Following this great tutorial I was able to configure my website to run the django app I’m developing. Now what I’d like to do would be to set up NGINX and gunicorn to run my server like this:

www.mydomain.com -> static webpage (pelican, bootstrap) www.mydomain.com/pmapp -> Django app I’m developing

I did try this with no luck.

Any help, idea?

thanks a lot

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
February 7, 2018
Accepted Answer

Using two different Nginx server blocks would be the right approach if you are serving one of the sites from a different domain or sub-domain. If you’d like to serve the Django app from a sub-folder, you will need to make use of a location directive in the same server block.

The details may differ for your exact setup, but here is a basic example of serving a static site from the main domain and a Django project from a subfolder:

upstream app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com;

    root /path/to/pelican/files;
    index index.html index.htm;

    client_max_body_size 4G;
    keepalive_timeout 5;

    location /app/static {
        alias /path/to/django/static/files;
    }

    location /app/ {
            rewrite ^/app/(.*)$ /$1 break;
            proxy_set_header X-Script-Name /app;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://app_server;
    }
}

Two very important parts in the location directive that are specific to hosting from the subfolder are:

            rewrite ^/app/(.*)$ /$1 break;
            proxy_set_header X-Script-Name /app;

This ensures that the Django app will generate URLs with the correct prefix in the path (e.g. example.com/app rather than example.com).

Hope that helps point you in the right direction!

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.