Issue
I have an nginx server which acts as a load balancer.
The nginx is configured to upstream 3 tries:
proxy_next_upstream_tries 3
I am looking for a way to pass to the backend server the current try number of this request - i.e. first, second or last.
I believe it can be done by passing this data in the header, however, how can I configure this in nginx and where can I take this data from?
Thanks
Solution
I sent this question to Nginx support and they provided me this explanation:
As long as you are using proxy_next_upstream mechanism for retries, what you are trying to do is not possible. The request which is sent to next servers is completely identical to the one sent to the first server nginx tries - or, more precisely, this is the same request, created once and then sent to different upstream servers as needed. If you want to know on the backend if it is handling the first request or it processes a retry request after an error, a working option would be to switch proxy_next_upstream off, and instead retry requests on 502/504 errors using the error_page directive.
See http://nginx.org/r/error_page for examples on how to use error_page.
So, I did as they advised me:
proxy_intercept_errors on;
location / {
proxy_pass http://example.com;
proxy_set_header NlbRetriesCount 0;
error_page 502 404 @fallback;
}
location @fallback {
proxy_pass http://example.com;
proxy_set_header NlbRetriesCount 1;
}
Answered By - Gal S Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.