[Django]-How to generate user defined "404 page not found" in nginx?

3👍

You don’t need this error_page 404 block and make sure you have proxy_intercept_errors off;. If these two conditions are met, nginx will let the application (here, Django) returns its own 404 page.

This configuration file should be fine:

server {
    listen localhost:8899;
    root /home/mulagala/Desktop/projects/28-05-2014/mysite/;
    access_log /var/log/nginx/example.log;
    error_log /var/log/nginx/example.error.log;

    location / {
        proxy_pass http://127.0.0.1:8060;
    }

    location /static/ {
        autoindex on;
        alias /home/mulagala/Desktop/projects/28-05-2014/mysite/static/;
    }
} 

0👍

Here’s an Nginx config example serving 404 page using nginx not django itself:

upstream faarco {
    server faarco_app:8000;
}

limit_req_zone $binary_remote_addr zone=one:10m rate=50r/s;

server {
    client_max_body_size 20m;

    listen 80;

    location / {
        proxy_pass http://faarco;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_intercept_errors on;  # note
        error_page 404 /custom_404.html;  # note
    }

    location /static/ {
        alias /code/static/;
    }

    location /media/ {
          root /code/;
    }

    error_page 404 /custom_404.html;
    location = /custom_404.html {  # note
        root /code/static/error_pages;
        internal;
    }
}

Ensure the /code/static/error_pages/custom_404.html file and path exist and have 755 permission.

Leave a comment