K
K
Kirill Vasiliev2015-12-16 15:21:32
Nginx
Kirill Vasiliev, 2015-12-16 15:21:32

Nginx reverse proxy depending on server response?

Hello everyone,
Show me in which direction to move if at all possible
There is nginx, it works as a reverse proxy and redirects all requests to 10.10.10.10:8000
Everything works great, but there is one nuance, periodically the server 10.10.10.10 gives a 401st message about lack of authorization.
The task is the following, if 401 came from the server, then you need to redirect to 10.10.10.11:8888
I can’t figure out how to filter responses from the server

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2015-12-16
@vasilevkirill

The traditional solution to the problem

location / {
    proxy_pass http://10.10.10.10:8000;
    proxy_intercept_errors on;
    error_page 401 = @fallback;
}

location @fallback {
    proxy_pass http://10.10.10.11:8888;
}

Y
Yuri, 2015-12-16
@xtreme

In such a situation, it would be worthwhile to first figure out why 401 is returned from the backend. And on the backend, I replaced 401 with another error (as an example of 404). For example, if the backend also has nginx, then this is done simply by the error_page directive. Well, on the frontend, turn on the second server in the upstream with the backup parameter and proxy_next_upstream http_404;
In this case, the request will go to the second backend.
As an option a little more complicated (I would, for example, do this) - take the nginx sources, find the proxy module there, write a piece of code for handling http_401 in the image and likeness of processing other errors, make a patch for the future (useful for updates), build your package with modified sources and upgrade. Well, then along the thumb - proxy_next_upstream http_401; and an upstream server with the backup parameter.
In any case, not enough information - why does the error occur? Is it supposed to be there or is it some kind of bug? Is it possible to fix a bug if it's a bug? Does it make sense to have another backend if a 401 is received? Perhaps, if you do not need to process the request further, but give the request a stub, just give the stub?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question