mardi 27 octobre 2020

nginx redirect on proxy connection instead of proxy_pass

Got 3 servers

"server3" which runs nginx and two backend servers (server1,server2).

Got the following configuration on server3 :

server {
    listen       80;

    location     @onerror {
        return 302  http://server2.com$request_uri;
    }

    location / {
        proxy_connect_timeout   1s;
        proxy_pass http://server1.com/;
        error_page 400 401 402 403 404 405 500 501 502 503 504  @onerror
    }
}

My goal is , request reaches server3 and it tries server1 , if it can not connect or got other error it redirects to server2.

This works just fine but not fully "optimized" since if server1 works all the traffics goes trough server3, which is not really needed since 302 redirect would do the job and client would be redirected to server1.

Load balancing is not an option if server1 is up and running all clients needs to use it , only if it is down client needs to use server2.

Now for my question,

Any way i can config nginx to "try" to connect to server1 , if it works don't use proxy_pass but use 302 redirect instead ?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire