Nginx remove base url

djangonginxrewriteuwsgi

I'm trying to setup Django through UWSGI using Nginx.

I got the UWSGI pass to work using this function

                location / {
                        include        uwsgi_params;
                        uwsgi_pass     127.0.0.1:9001;
                }

Unfortunately when I visit /django/admin. I get an error

Page not found (404)
Request Method: GET
Request URL: http://69.x.x.x/django/admin
Using the URLconf defined in Django.urls, Django tried these URL patterns, in this order:
^admin/

How can I have nginx rewrite the url to not pass the /django part?

Best Answer

There has to be something like the following in the uwsgi_params file:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

These are the params passed to your application. I think you need to change the request_uri to $host in order to remove everything behind the hostname.

You can also set this variable inside the virtual server config as well, it should overwrite it locally. Do this before uwsgi_pass 127.0.0.1:9001;.

ALTERNATIVE METHOD Also, you can create a rewrite rule as the following:

rewrite ^(.*)/(.*)$ $1 last;

and add it into the location / part, but I'm not sure this will work as expected.