ip_hash

ip_hash 可以保证用户访问可以请求到上游服务中的固定的服务器,前提是用户ip没有发生更改。
使用ip_hash的注意点:
不能把后台服务器直接移除,只能标记down.

If one of the servers needs to be temporarily removed, it should be marked with the down parameter in order to preserve the current hashing of client IP addresses.

  1. upstream tomcats {
  2. ip_hash;
  3. server 192.168.1.173:8080;
  4. server 192.168.1.174:8080 down;
  5. server 192.168.1.175:8080;
  6. }

参考:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#ip_hash

url_hash

根据每次请求的url地址,hash后访问到固定的服务器节点

  1. upstream tomcats {
  2. # url hash
  3. hash $request_uri;
  4. # 最少连接数
  5. # least_conn
  6. server 192.168.1.173:8080;
  7. server 192.168.1.174:8080;
  8. server 192.168.1.175:8080;
  9. }
  10. server {
  11. listen 80;
  12. server_name www.tomcats.com;
  13. location / {
  14. proxy_pass http://tomcats;
  15. }
  16. }

least_conn

  1. upstream tomcats {
  2. # url hash
  3. # hash $request_uri;
  4. # 最少连接数
  5. least_conn
  6. server 192.168.1.173:8080;
  7. server 192.168.1.174:8080;
  8. server 192.168.1.175:8080;
  9. }
  10. server {
  11. listen 80;
  12. server_name www.tomcats.com;
  13. location / {
  14. proxy_pass http://tomcats;
  15. }
  16. }