Search code examples
djangonginxuwsgisubdirectory

nginx serving Django in a subdirectory through uWSGI


I have already gone through some previous threads: How do I set subdirectory in nginx with Django how to deploy django under a suburl behind nginx Serving flask app on subdirectory nginx + uwsgi

The basic lesson is that you should only need to configure your site(s-available) to achieve this. I have now tried various permutations of

server {
    listen 80;
    server_name www.example.com;

    location = /favicon.ico { 
        access_log off; 
        log_not_found off; 
    }

    location /static/ {
        root /path/to/project;
    }

    location /project/ {
        root            /path/to/project;
        include         /etc/nginx/uwsgi_params;
        uwsgi_param     SCRIPT_NAME /project;
        uwsgi_modifier1 30;
        uwsgi_param PATH_INFO "$1";
        uwsgi_pass      unix:/tmp/project.sock;
    }
}

Everything runs perfectly when I define location to be "/" (and remove SCRIPT_NAME, modifier1, PATH_INFO and root doesn't matter. But trying to use a subdirectory always results in Page not found (404):

Request URL:    http://www.example.com/project/project

(edit) It's ADDING a directory to the request. What am I not figuring out?

(tried forced_script_name - should't have to use this and gives other types of headaches - and uwsgi config setting)

EDIT:

location /project/ {
    root            /path/to/project;
    include         /etc/nginx/uwsgi_params;
    uwsgi_param     SCRIPT_NAME /project;
    uwsgi_pass      unix:/tmp/project.sock;
}

Does not work ... The socket is there and works when I configure for / - I just can't see what I'm missing.

UPDATE:

location ~ /project(?<path_info>/.*|$) {
    include         /etc/nginx/uwsgi_params;
    uwsgi_pass      unix:/tmp/project.sock;
    uwsgi_param     PATH_INFO $path_info;
    uwsgi_param     SCRIPT_NAME /project;
}

This loads up the site but all links point to http://example.com/link/to/something instead of http://example.com/project/link/to/something


Solution

  • Eventually gave up on trying to do this "neatly".

    The final solution was just to make a settings variable that I prefixed to the static_url and projects urls.py file. No SCRIPT_NAME or anything complicated on the nginx side.