Search code examples
djangoreverse-proxytraefik

Traefik and Django on a Subpath


Following setup I want to achieve inside Docker with Traefik and Django:

  • http://domain/app1
  • http://domain/app2

My docker-compose.yml contains the following labels for the containers:

  • traefik.http.routers.app1.rule=Host(`my.host.de`) && PathPrefix(`/app1`)
  • traefik.http.routers.app1.middlewares=app1
  • traefik.http.middlewares.app1.headers.customresponseheaders.SCRIPT_NAME=/app1

I did the same for app2.

In the settings.py of both apps I set: FORCE_SCRIPT_NAME = env('FORCE_SCRIPT_NAME', default=None) which then should get resolved via the ENV File where I have FORCE_SCRIPT_NAME=/app1.

On Django side I always get a 404 with the message that this path does not exist and I should choose from an existing one.

Django recognizes the URL as http://my.host.de/app1 and tells me The current path, app1, didn't match any of these.

EDIT: Since my setup is thought to be both for dev and prod envs, I am using the Django built in server as well as Gunicorn for Running the Django apps.


Solution

  • If you want to pass a SCRIPT_NAME header to django, you have to use customrequestheaders instead of customresponseheaders when creating the middleware