问题原文
原帖链接 · cedivad · CC BY-SA 3.0
I'm looking for an HTTP load balancer that will handle server errors silently. What i want is to load balance every single request so that it works out, in the worst case with a little of timeout.
If the working web node returns an HTTP 500 server error, the load balancer will have to retry the request with another web node. If the second node does return another 500 error, doing the same with the last node (i suppose to have 3 nodes). If the last node returns a 500 error, display it to the end user.
If a server node timeout (takes more than 1 or 2 second to answer) the request will have to be routed to another server, the client should receive a good answer within 2+ seconds.
已采纳解答
原帖链接 · Kristaps · CC BY-SA 3.0
You can nginx with HttpProxyModule (it's pretty standart module and usually is inside nginx) to implement such load balancer.
Nginx is lightweight, fast and has a lot of functionality (you can even embed lua code in it).
Example config for your use case would be
upstream backend {
server 10.0.0.1;
server 10.0.0.2;
server 10.0.0.3;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://backend;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
The secret sauce is proxy_next_upstream directive, which determines in what cases the request will be transmitted to the next server. Possible values are :
- error — an error has occurred while connecting to the server, sending a request to it, or reading its response;
- timeout — occurred timeout during the connection with the server, transfer the request or while reading response from the server;
- invalid_header — server returned a empty or incorrect answer;
- http_500 — server returned answer with code 500
- http_502 — server returned answer with code 502
- http_503 — server returned answer with code 503
- http_504 — server returned answer with code 504
- http_404 — server returned answer with code 404
- off — it forbids the request transfer to the next server
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
