Skip to content

Fix Traefik deployment: Replace SCRIPT_NAME with URL_PREFIX to avoid WSGI conflicts - #52

Merged
tgangte merged 2 commits into
mainfrom
copilot/fix-traefik-configuration-issue
Oct 16, 2025
Merged

Fix Traefik deployment: Replace SCRIPT_NAME with URL_PREFIX to avoid WSGI conflicts#52
tgangte merged 2 commits into
mainfrom
copilot/fix-traefik-configuration-issue

Conversation

Copilot AI commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

Problem

When deploying LocalCA behind Traefik with path prefix stripping (e.g., serving at /localca), users encounter this error:

Configuration problem: Request path '/' does not start with SCRIPT_NAME '/localca'

This prevents the application from working when deployed behind Traefik with the provided docker-compose-traefik.yml configuration.

Root Cause

The issue stems from a conflict between Django's URL generation and WSGI's request path validation:

  1. The docker-compose-traefik.yml sets SCRIPT_NAME=/localca as an environment variable
  2. Traefik strips the /localca prefix before forwarding requests to Django (as configured)
  3. Django receives the request with path / (after stripping)
  4. However, WSGI servers read SCRIPT_NAME from the environment and use it to validate incoming request paths
  5. Django's CommonMiddleware checks if the request path starts with SCRIPT_NAME
  6. The validation fails because '/' does not start with '/localca'

This is a fundamental incompatibility: you cannot use SCRIPT_NAME as an environment variable when the reverse proxy strips the prefix before forwarding requests.

Solution

This PR introduces a new environment variable URL_PREFIX to replace SCRIPT_NAME:

  • URL_PREFIX is a custom variable that doesn't interfere with WSGI's path validation
  • Django reads it only for the FORCE_SCRIPT_NAME setting, which controls URL generation
  • Traefik can strip the prefix without causing validation errors
  • Generated URLs still include the correct /localca prefix

Changes Made

  1. settings.py: Changed from os.environ.get('SCRIPT_NAME') to os.environ.get('URL_PREFIX') with detailed comments explaining the change
  2. docker-compose-traefik.yml: Updated environment variable from SCRIPT_NAME=/localca to URL_PREFIX=/localca
  3. TRAEFIK_DEPLOYMENT.md: Updated all references and added a new section explaining the technical rationale
  4. README.md: Updated reverse proxy deployment instructions

Migration Guide

Users deploying with Traefik should update their docker-compose-traefik.yml:

# Before (broken):
environment:
  - SCRIPT_NAME=/localca

# After (fixed):
environment:
  - URL_PREFIX=/localca

Then restart the container. No other changes are required.

Testing

  • ✅ All 19 existing unit tests pass
  • ✅ Integration testing confirms URL generation includes correct prefix
  • ✅ Verified Django accepts requests without prefix (simulating Traefik stripping)
  • ✅ Code passes pylint validation (10.00/10)
  • ✅ docker-compose-traefik.yml syntax validated

Backward Compatibility

This change is fully backward compatible for:

  • Deployments without a reverse proxy
  • Deployments without path prefix requirements
  • Any deployment not using docker-compose-traefik.yml

The only users affected are those already using docker-compose-traefik.yml with Traefik, who will need to update their configuration file as shown above.

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Docker compose and Traefik</issue_title>
<issue_description>If you use the docker compose configuration from the repository together with traefik, when you request the home page from the browser, the following error appears in the logs:
Configuration problem: Request path '/' does not start with SCRIPT_NAME '/localca'
The response to the browser is the same.

Traefik version 3.5.3</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #51


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: tgangte <8805013+tgangte@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Traefik configuration issue with Docker compose Fix Traefik deployment: Replace SCRIPT_NAME with URL_PREFIX to avoid WSGI conflicts Oct 16, 2025
Copilot AI requested a review from tgangte October 16, 2025 17:00
@tgangte
tgangte marked this pull request as ready for review October 16, 2025 17:06
@tgangte
tgangte merged commit 862e0d8 into main Oct 16, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docker compose and Traefik

2 participants