Redirect request based on request type in case of error

devkuamr nginx-forum at forum.nginx.org
Tue Jan 9 06:04:22 UTC 2018


Hi,
I want to redirect GET request to another server in case of errors. I am
using the below configuration but nginx is having problem while starting
with this configuration.

worker_processes  8;
events {
   worker_connections  1024;
}
http {
                include       mime.types;
                include       map.txt;
                default_type  application/octet-stream;
                sendfile        on;

                        upstream 271889976{
                                server 172.198.1.19:8080;
                        }

                        upstream 271889979{
                                server 172.198.1.18:8080;
                        }


                upstream backup {
                        server 172.198.1.20:8080;
                }

        server {
                listen       8080;
                server_name  localhost;
                proxy_intercept_errors on;
                        error_page 502 @redirect;
                        proxy_read_timeout 10s;
                location / {
                                proxy_pass http://$newhost$request_uri;
                                proxy_hide_header store;
                }
                        location @redirect{
                               if ($request_method = GET){
                                        proxy_pass
http://backup$request_uri;
                                        break;
                                }
                                if ($request_method = POST){
                                        return upstream_status;
                                }

                        }

        }

        include servers/*;
}

I am getting "invalid return code "upstream_status"" with above
configuration file.

For POST request I want the same error returned by server. and in case of
GET I want request to be redirected on "backup upstream".

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278044,278044#msg-278044



More information about the nginx mailing list