Home > other >  Redirect for a single file with Django and Nginx
Redirect for a single file with Django and Nginx

Time:04-08

I have a Django project serving static correctly files at /static. I'd like to serve a single .txt file in the root though, without the static in the URL.

This is my Nginx section:

location /ms11973759.txt {
        try_files /home/myhome/nms/static/ms11973759.txt;
    }

I get a 404, although I can access the file via mysite/static/ms11973759.txt. What am I missing?

The following also does not work:

    location /ms11973759.txt {
        root /home/myhome/nms/static;
    }

CodePudding user response:

For some reason this is the only approach that finally worked for me.

I added a new view:

from django.http import HttpResponse
from django.views.decorators.http import require_GET

@require_GET
def ms11973759_txt(request):
    lines = [
        "my",
        "many",
        "lines",
    ]
    return HttpResponse("\n".join(lines), content_type="text/plain")

...and then added a

[...]path("ms11973759.txt", ms11973759_txt),[...]

to my urls.py.

  • Related