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 http://some.address.com 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 http://some.address.com/errors/5xx.html, 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.

nginx.conf

server {
   server_name some.address.com;

   location / {
       // some settings
       proxy_pass http://127.0.0.1:8084/;
   }

   location ^~ /errors/ {
       internal;
       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.

        internal;

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.