How to place a Django project in a subpath?

I am creating an API with a Django REST framework that is served through Gunicorn and Nginx. The project "exampleproject" should be run under the following subclause, for example: https://100.100.100.100/exampleproject (example IP address). I do not have a domain name registered for the IP address.

Currently, the start page is displayed as expected at https://100.100.100.100/exampleproject . However, the resource path for the "products" does not work. Instead of https://100.100.100.100/exampleproject/products, the start page displays https://100.100.100.100/products - which does not work.

I configured a subpath for exampleprojectin /etc/nginx/sites-enabled/defaultas follows:

server {

    # ...

    location /exampleproject/ {
        proxy_pass        http://localhost:8007/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host $host;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
    }

When I manually visit https://100.100.100.100/exampleproject/products Nginx writes the following to /var/log/nginx/access.log:

"GET / products / HTTP / 1.1" 404 151 "-"

+4
source share
1 answer

I found here that I need to add the following parameter to Django's configuration settings.py:

FORCE_SCRIPT_NAME = '/exampleproject'

This seems to rewrite all paths for nested resources.

+4
source

All Articles