Bhavin Shah
Bhavin Shah

Reputation: 120

Serving Static content from Nginx but handling error pages in Django

My static and media files are served by Nginx and other requests are served by Django.

I have a custom 404 page which is served by Django. Since Nginx is handling static content any requests to /static/* which throws 404 is also handled by Nginx. I can set static custom 404 page in Nginx but I want Django to handle any 404.

Is it possible to serve static content from Nginx but in case of 404, the request get forwarded to Django?

Upvotes: 2

Views: 363

Answers (1)

e4c5
e4c5

Reputation: 53774

Assuming that you have defined your django proxy pass

upstream django_server {
    server 127.0.0.1:8000;
}

Then you can do this

location /static/ {
     try_files $uri @django;
     ....
}
location @django { 
    proxy_pass django_server;
}

Upvotes: 1

Related Questions