Fix support for X-Accel-Redirect

This commit is contained in:
2026-01-05 14:39:45 +01:00
parent 9a469378df
commit 4ec7b8fc18
5 changed files with 68 additions and 2 deletions

View File

@@ -63,7 +63,12 @@ def get_items_ordering(config="items_ordering"):
class Render404(View):
def get(self, request, exception):
return render(request, "base.html", {"title": "404 page not found"})
return render(
request,
"base.html",
{"title": "404 page not found"},
status=404,
)
class GetData(View):

View File

@@ -34,3 +34,4 @@ ALLOWED_HOSTS = ["127.0.0.1", "myhost"]
CSRF_TRUSTED_ORIGINS = ["https://myhost"]
STATIC_URL = "static/"
MEDIA_URL = "media/"
USE_X_ACCEL_REDIRECT = True

View File

@@ -207,6 +207,16 @@ ROLLING_STOCK_TYPES = [
FEATURED_ITEMS_MAX = 6
# If True, use X-Accel-Redirect (Nginx)
# when using X-Accel-Redirect, we don't serve the file
# directly from Django, but let Nginx handle it
# in Nginx config, we need to map /private/ to
# the actual media files location with internal directive
# eg:
# location /private {
# internal;
# alias /path/to/media;
# }
# make also sure that the entire /media is _not_ mapped directly in Nginx
USE_X_ACCEL_REDIRECT = False
try:

View File

@@ -94,10 +94,17 @@ class DownloadFile(View):
if not os.path.exists(file.path):
break
# in Nginx config, we need to map /private/ to
# the actual media files location with internal directive
# eg:
# location /private {
# internal;
# alias /path/to/media;
# }
if getattr(settings, "USE_X_ACCEL_REDIRECT", False):
response = HttpResponse()
response["Content-Type"] = ""
response["X-Accel-Redirect"] = file.url
response["X-Accel-Redirect"] = f"/private/{file.name}"
else:
response = FileResponse(
open(file.path, "rb"), as_attachment=True