Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
render custom 404 in nginx or something else if 404 doesn't exist

I'd like to host HTML files uploaded by users and I need to display custom 404 if they provided it, or display my own 404 otherwise.

Sample workflow:

  1. visitor tries to access example.com/pages/page1.html
  2. pages/page1.html does not exists but $user has uploaded 404.html so display it
  3. $user has not provided 404.html so display simple message myself

So far I have covered the steps 1 & 2 but cannot make the logic for last one.

My /simplified/ config:

# each user has their own storage path etc
# just to illustrate
map $host $user {
    dog.example.com             group/1/2/3;
    cat.example.com             group/4/5/1;
    dog-want-cookie.example.com group/9/9/9;
}

server {
    listen 8000;
    server_name ~^(?<vhost>[^.]*)\.example\.com$;

    root /sites/$user;

    location / {        
        try_files $uri $uri/index.html @not_found;
    }

    location @not_found {
        internal;
        error_page 404 /404.html;
    }
}

Not sure where to "invoke" a config for my own error "page".

location @my404 {
    return 404 "Page not found.";
}

I'm using Nginx 1.19.3 on Debian.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use error_page to invoke the named location rather than try_files. See this document for details.

For example:

error_page 404 @not_found;

location / {        
    try_files $uri $uri/index.html =404;
}

Use try_files to test the existence of the 404.html file.

For example:

location @not_found {
    try_files /404.html /fallback404.html =404;
}

You do not need to mark named locations as internal, as they cannot be accessed externally.

In the above example, the fallback404.html file is in the same document root. The =404 is necessary for the syntax, but is never reached as the second file always exists. See this document for details.


To use a fallback 404.html file in a different document root, you can cascade two named locations.

For example:

location @not_found {
    try_files /404.html @fallback;
}
location @fallback {
    root /path/to/fallback/files;
    try_files /404.html =404;
}

Alternatively, you can replace the content of the @fallback location with your return statement.

For example:

location @fallback {
    return 404 "Page not found.";
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!