Below is example of NGINX reverse proxy with 2 backend load balanced:
upstream backend {
ip_hash;
server localhost:8080 fail_timeout=3;
server localhost:8081 fail_timeout=3;
}
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://backend/;
proxy_redirect default;
proxy_cookie_domain localhost mydomain.com;
}
}
- The upstream directive defines a cluster named backend of 2 backend servers (localhost:8080 and localhost:8081). fail_timeout parameter specifies request to the node will be deemed fail if no response is obtained after 3 seconds.
- The ip_hash directive causes request coming from same ip to be associated with the same backend. This is often called sticky session. Other popular strategy is using cookie.
- The cluster name backend is referenced by proxy_pass directive inside location
Add down parameter to avoid request being passed to specific backend:
upstream backend {
ip_hash;
server localhost:8080 fail_timeout=3;
server localhost:8081 fail_timeout=3 down;
}
This is handy when performing no-outage release.
Don’t forget to reload the configuration using nginx -s reload