nginx – Why can't I access my error page directly?

Zabri asked:

I have a NodeJS application to which I forward requests using proxy_pass. When the NodeJS application is off, I want to display a custom error page. So I put a 5xx.html file into /var/www/some-folder/errors and forward errors to it.

Now when I try to access while the NodeJS server is off, the 502 error is raised and nginx correctly returns the 5xx.html page.

Now according to nginx.conf (see below), I would assume that if I request, it gets forwarded through the location ^~ /errors/ directive to /var/www/some-folder and then to /errors/5xx.html. But instead of getting the 5xx.html page, I get the default nginx 404 Not Found page.

Why can’t I access the 5xx.html page directly? Thank you.


server {

   location / {
       // some settings

   location ^~ /errors/ {
       root /var/www/some-folder;

   error_page 502 503 504 505 /errors/5xx.html;


My answer:

You can’t access the page directly because you specified in your configuration to not allow such requests.


See the nginx documentation:

Specifies that a given location can only be used for internal requests. For external requests, the client error 404 (Not Found) is returned.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.