1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-23 14:59:36 +02:00

feat: add OverrideHostMiddleware to set HTTP_HOST from PUBLIC_URL environment variable and update nginx configuration

This commit is contained in:
Sean Morley 2025-01-06 20:44:37 -05:00
parent e19781d7ac
commit a5aa09ed7b
3 changed files with 31 additions and 11 deletions

View file

@ -17,24 +17,24 @@ http {
} }
server { server {
listen 80; # NGINX always listens on port 80 inside the container listen 80;
server_name localhost; server_name localhost;
location / { location / {
proxy_pass http://server:8000; # Explicitly forward to Django service proxy_pass http://server:8000; # Forward to internal Gunicorn server
proxy_set_header Host $host; proxy_set_header Host $host; # Forward Host header from the request
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr; # Forward real IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Forward original IP
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme; # Forward the protocol
proxy_set_header X-Forwarded-Host $host; # Forward the Host header
} }
location /static/ { location /static/ {
alias /code/staticfiles/; # Serve static files directly alias /code/staticfiles/;
} }
location /media/ { location /media/ {
alias /code/media/; # Serve media files directly alias /code/media/;
} }
} }
} }

View file

@ -20,4 +20,23 @@ class PrintCookiesMiddleware:
def __call__(self, request): def __call__(self, request):
print(request.COOKIES) print(request.COOKIES)
response = self.get_response(request) response = self.get_response(request)
return response return response
# middlewares.py
import os
from django.http import HttpRequest
class OverrideHostMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request: HttpRequest):
# Override the host with the PUBLIC_URL environment variable
public_url = os.getenv('PUBLIC_URL', None)
if public_url:
# Split the public URL to extract the host and port (if available)
host = public_url.split("//")[-1].split("/")[0]
request.META['HTTP_HOST'] = host # Override the HTTP_HOST header
response = self.get_response(request)
return response

View file

@ -69,6 +69,7 @@ MIDDLEWARE = (
'corsheaders.middleware.CorsMiddleware', 'corsheaders.middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'adventures.middleware.OverrideHostMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',