Nginx ip_hash不会对与流星后端的连接进行负载均衡

前端之家收集整理的这篇文章主要介绍了Nginx ip_hash不会对与流星后端的连接进行负载均衡前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我无法让Nginx在启用ip_hash的情况下对内部连接进行负载均衡.我需要粘性会话,因为我在后端使用带有套接字的meteor,但所有请求总是命中同一个后端.

Nginx访问日志文件显示以下IP地址:

  1. 192.168.0.20 - - [xx/xxx/2017:xx:xx:xx +xxxx] "GET /favicon.ico HTTP/1.1" 404 5 "http://xxxx.lokal/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/56.0.2924.87 Safari/537.36"
  2. 192.168.0.11 - - [xx/xxx/2017:xx:xx:xx +xxxx] "GET /sockjs/602/dpkl6lfe/websocket HTTP/1.1" 101 55045 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/56.0.2924.87 Safari/537.36"
  3. 192.168.0.208 - - [xx/xxx/2017:xx:xx:xx +xxxx] "GET /sockjs/031/cx1kml79/websocket HTTP/1.1" 101 1146677 "-" "Mozilla/5.0 (iPad; cpu OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML,like Gecko) Version/10.0 Mobile/14D27 Safari/602.1"

是因为它们都来自同一个192.168.0.*子网吗?如果是这样,我该如何改变这种行为?

这是我的配置文件

  1. user www;
  2. worker_processes 4;
  3. error_log /var/log/Nginx/error.log;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. map $http_upgrade $connection_upgrade {
  9. default upgrade;
  10. '' close;
  11. }
  12. upstream demo {
  13. ip_hash;
  14. server 127.0.0.1:5000;
  15. server 127.0.0.1:5001;
  16. }
  17. include mime.types;
  18. default_type application/octet-stream;
  19. access_log /var/log/Nginx/access.log;
  20. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3,ref: POODLE
  21. ssl_prefer_server_ciphers on;
  22. tcp_nopush on;
  23. tcp_nodelay on;
  24. keepalive_timeout 65;
  25. types_hash_max_size 2048;
  26. gzip on;
  27. gzip_disable "msie6";
  28. server_tokens off; # for security-by-obscurity: stop displaying Nginx version
  29. server {
  30. listen 80;
  31. server_name xxxx.lokal;
  32. location / {
  33. proxy_pass http://demo;
  34. proxy_redirect off;
  35. proxy_set_header Host $host;
  36. proxy_set_header X-Real-IP $remote_addr;
  37. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  38. proxy_set_header X-Forwarded-Proto $scheme;
  39. proxy_http_version 1.1;
  40. proxy_set_header Upgrade $http_upgrade; # allow websockets
  41. proxy_set_header Connection "upgrade";
  42. proxy_buffering off;
  43. proxy_connect_timeout 43200000;
  44. proxy_read_timeout 43200000;
  45. proxy_send_timeout 43200000;
  46. if ($uri != '/') {
  47. expires 30d;
  48. }
  49. }
  50. }
最佳答案
是的,这是因为ip_hash仅使用IP地址的前三个八位字节来选择后端节点.

你可以使用hash $remote_addr;使Nginx使用完整的远程IP地址进行散列的指令.缺点是,如果节点发生故障,所有散列映射都会发生变化,会话将丢失.

有关上游选择方法的更多信息,请访问:
nginx upstream module documentation.

猜你在找的Nginx相关文章