Using error_page to a named location - possible?
Maxim Dounin
mdounin at mdounin.ru
Tue Mar 6 17:47:28 UTC 2012
Hello!
On Tue, Mar 06, 2012 at 04:37:41PM +0000, Ed W wrote:
> 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?
Yes, see below.
>
> So, I have
>
> server {
> listen 8000 default_server;
>
> root /var/www/cp/htdocs/public;
>
> location / {
> proxy_read_timeout 5;
> proxy_pass http://127.0.0.1:3000;
> 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 {
> internal;
> }
> }
>
>
> 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 "internal"?)
Yes.
> I can't see that it's possible to use something like:
>
> location / {
> proxy_pass http://127.0.0.1:3000;
> error_page 502 @502;
> }
>
> location @502 {
> index /502.html;
> root /var/www;
> internal;
> }
Try something like:
location @502 {
internal;
root /var/www;
rewrite ^ /502.html break;
}
This should work at least for GET requests.
> 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?)
This should work too.
Maxim Dounin
More information about the nginx
mailing list