简体   繁体   中英

Nginx redirecting traffic to a reserve server

I have an Nginx server. Which distributes traffic to multiple servers, below is the config:

            location /rc/temp/ {
                    proxy_pass https://rc1-test.jer.com;
            }
            location /rc/ {
                    if ($testip) {
                            proxy_pass http://192.168.0.100;
                    }
                    proxy_pass http://192.168.0.103;

            }
            location / {
                    if ($is) {
                            proxy_pass https://rc6-test.jer.com;
                    }
                    proxy_pass https://rc8-test.jer.com;
            }

I want to know if it is possible to add some if...else so that there would be an opportunity, for example, if some one with the server is unavailable, then the traffic is redirected to the reserve. For example, for location /rc/temp/ if https://rc1-test.jer.com is not available; then direct traffic to a clone of this server https://rc2-test.jer.com;. Thanks.

Try error_page directive:

    location /rc/temp/ {
        proxy_pass https://rc1-test.jer.com;
        error_page 500 @fallback
    }
    location @fallback {
        proxy_pass https://rc2-test.jer.com;
    }

You may change the 500 code in the error_page directive with some other error code if you need to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM