fr3d-5
fr3d-5

Reputation: 812

Configure Django App as nginx location

I am trying to deploy a Django app along a static html website. The general www.example.com will provide the basic website. The URI /app should then proxy to a Gunicorn WSGI server. The nginx configuration in sites-enabled looks as follows:

upstream gunicorn {
server 127.0.0.1:8000;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/website;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    location /app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://gunicorn;
    }

}

I am getting a 404 error when trying www.example.com/app. It seems the URI gets messed up somehow. The complete URL that is created is http://www.example.com/accounts/login/?next=/app/

Setting up the App alone exactly following this tutorial http://hackedexistence.com/project/raspi/django-on-raspberry-pi.html does work.

Is the setup I am aiming at even possible? And if yes, where is my misunderstanding of the location concept?

Upvotes: 0

Views: 1623

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

You need to set the SCRIPT_NAME header in your location directive.

proxy_set_header SCRIPT_NAME /app;

See this blog post for an example.

Upvotes: 2

Related Questions