Using error_page to a named location - possible?

Ed W lists at
Tue Mar 6 16:37:41 UTC 2012

Hi, as part of a larger problem I have a captive portal which grabs all 
incoming URLs and after evaluating a bunch of logic redirects the caller 
somewhere else.  I use proxy_pass to pass the request to my cgi code.

However, I would like to customise the error message in the event that 
the upstream daemon is unavailable.  I can't figure out how to do that 
affecting part of the incoming URLs.  Is it possible to do something 
like "error_page @502_error" in conjunction with proxy_pass?

So, I have

server {
   listen 8000 default_server;

   root /var/www/cp/htdocs/public;

   location / {
     proxy_read_timeout 5;
     error_page 502 /502.html;
     proxy_set_header Host $http_host;
     proxy_set_header X-Server-Addr $server_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-HTTPS 0;
     proxy_set_header X-Captive-Portal-Redirect 1;
   location /502.html {

However, if I visit "http://somewhere/502.html" I get a 404 response 
(presumably matching the location /502.html and the 404 is due to the 

I can't see that it's possible to use something like:

   location / {
     error_page 502 @502;

location @502 {
   index /502.html;
   root /var/www;

Does someone have a suggestion on how to customise the error response 
without affecting ANY incoming URLs (they must ALL be passed through the 
proxy under normal working situation)

(Its occured to me that I could perhaps setup another "server { }" block 
listening on another port, then use a different proxy_pass in the 
"location @502 {}"?  Seems ugly, but would that work?)

Thanks for any thoughts

Ed W

More information about the nginx mailing list