Nginx Web负载均衡开局配置之——会话保持
  

justgonnab 3838

{{ttag.title}}
本帖最后由 justgonnab 于 2018-11-29 17:35 编辑

刚写了《Nginx Web负载均衡开局配置》这篇文章,为什么又写到《Nginx Web负载均衡开局配置之会话保持》?因为涉及到会话保持的nginx负载均衡在我所遇到的项目过程过程中,调试要比简单的静态页面负载均衡复杂的多。首先带有登陆认证的Web网页需要保证nginx识别前端客户端session的一致性,然后确保应用端回复的请求到会准确的到达指定的客户端,避免张三发出的请求,应用端结果却回复给了李四导致通讯失败。


所以这个问题涉及到业务系统负载均衡的可用性,会话保持是必须要解决的也是第一个要解决的问题。


好了,话不多说开始吧。


操作系统:Red Hat Enterprise Linux Server release 6.5 (Santiago)
nginx版本:nginx 1.12.1
Web环境:3个带有登陆功能相同的Web系统(tomcat、挂载同一磁盘、集群)
具体的Web环境搭建涉及软件项目实施这里不赘述,大家可以github下载完整的小应用用来测试。


网络拓扑如下:





首先来配置nginx.conf文件:
  1. #user  nobody;
  2. worker_processes  auto;
  3. worker_rlimit_nofile 1024000;

  4. #error_log  logs/error.log;
  5. #error_log  logs/error.log  notice;
  6. #error_log  logs/error.log  info;

  7. #pid        logs/nginx.pid;


  8. events {
  9.         
  10.     worker_connections  10240;
  11.         multi_accept on;
  12. }


  13. http {
  14.     include       mime.types;
  15.     default_type  application/octet-stream;

  16.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  17.      #                 '$status $body_bytes_sent "$http_referer" '
  18.      #                 '$upstream_addr $upstream_response_time $request_time '
  19.      #                '"$http_user_agent" "$http_x_forwarded_for"';

  20.     #access_log  logs/access.log  main;
  21.    

  22.     sendfile        on;
  23.     #tcp_nopush     on;

  24.     #keepalive_timeout  0;
  25.     keepalive_timeout 60;

  26.     #gzip  on;

  27.     #hash $cookie_jsessionid;

  28.     upstream webapp  {
  29.                            ip_hash;
  30.                                  server 192.168.150.128:8080;
  31.                                 server 192.168.150.128:8081;
  32.                                 server 192.168.150.128:8082;
  33.          }

  34.     server {
  35.         listen       80;
  36.         server_name  localhost;

  37.         charset utf-8;

  38.         #access_log  logs/host.access.log  main;
  39.                 access_log off;

  40.         location / {
  41.             proxy_pass   http://webapp;
  42.                 proxy_redirect off;
  43.                 proxy_http_version 1.1;
  44.                
  45.                 proxy_connect_timeout 60;
  46.                 proxy_send_timeout 60;
  47.                 proxy_read_timeout 60;
  48.                
  49.                
  50.                 proxy_set_header Connection "";
  51.                 proxy_set_header X-Real-IP $remote_addr;
  52.                 proxy_set_header REMOTE-HOST $remote_addr;
  53.                 proxy_set_header Host $http_host;
  54.                 proxy_set_header X-Forwarded-Host $http_host;
  55.                 proxy_set_header X-Forwarded-Server $host;
  56.                 proxy_set_header X-Forwarded-For $remote_addr;
  57.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  58.                 proxy_headers_hash_max_size 51200;
  59.                 proxy_headers_hash_bucket_size 6400;
  60.                 proxy_temp_file_write_size 1024k;
  61.                
  62.         }


  63.         #error_page  404              /404.html;

  64.         # redirect server error pages to the static page /50x.html
  65.         #
  66.         error_page   500 502 503 504  /50x.html;
  67.         location = /50x.html {
  68.             root   html;
  69.         }

  70.         # proxy the PHP scripts to 某公司 listening on 127.0.0.1:80
  71.         #
  72.         #location ~ \.php$ {
  73.         #    proxy_pass   http://127.0.0.1;
  74.         #}

  75.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  76.         #
  77.         #location ~ \.php$ {
  78.         #    root           html;
  79.         #    fastcgi_pass   127.0.0.1:9000;
  80.         #    fastcgi_index  index.php;
  81.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  82.         #    include        fastcgi_params;
  83.         #}

  84.         # deny access to .htaccess files, if 某公司's document root
  85.         # concurs with nginx's one
  86.         #
  87.         #location ~ /\.ht {
  88.         #    deny  all;
  89.         #}
  90.     }


  91.     # another virtual host using mix of IP-, name-, and port-based configuration
  92.     #
  93.     #server {
  94.     #    listen       8000;
  95.     #    listen       somename:8080;
  96.     #    server_name  somename  alias  another.alias;

  97.     #    location / {
  98.     #        root   html;
  99.     #        index  index.html index.htm;
  100.     #    }
  101.     #}


  102.     # HTTPS server
  103.     #
  104.     #server {
  105.     #    listen       443 ssl;
  106.     #    server_name  localhost;

  107.     #    ssl_certificate      cert.pem;
  108.     #    ssl_certificate_key  cert.key;

  109.     #    ssl_session_cache    shared:SSL:1m;
  110.     #    ssl_session_timeout  5m;

  111.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  112.     #    ssl_prefer_server_ciphers  on;

  113.     #    location / {
  114.     #        root   html;
  115.     #        index  index.html index.htm;
  116.     #    }
  117.     #}

  118. }
复制代码

这里重点看:


  1. upstream webapp  {
  2.                                 ip_hash;  
  3.                                 server 192.168.150.128:8080;
  4.                                 server 192.168.150.128:8081;
  5.                                 server 192.168.150.128:8082;
  6.                  }
复制代码

其中,ip_hash参数非常关键,它可以将每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

  1. location / {
  2.                  proxy_pass   http://webapp;
  3.                 proxy_redirect off;
  4.                 proxy_http_version 1.1;
  5.                
  6.                 proxy_connect_timeout 60;
  7.                 proxy_send_timeout 60;
  8.                 proxy_read_timeout 60;
  9.                
  10.                 proxy_set_header Connection "";
  11.                 proxy_set_header X-Real-IP $remote_addr;
  12.                 proxy_set_header REMOTE-HOST $remote_addr;
  13.                 proxy_set_header Host $http_host;
  14.                 proxy_set_header X-Forwarded-Host $http_host;
  15.                 proxy_set_header X-Forwarded-Server $host;
  16.                 proxy_set_header X-Forwarded-For $remote_addr;
  17.                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18.                 proxy_headers_hash_max_size 51200;
  19.                 proxy_headers_hash_bucket_size 6400;
  20.                 proxy_temp_file_write_size 1024k;
  21.                
  22.         }
复制代码

其中,第二、三段参数着重考虑连接性能,其中包括连接超时时间、代理发送和读取超时时间等。


配置完成后,检查配置文件是否有错误:


  1. [root@localhost nginx]# ./nginx -t
  2. nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/nginx.conf test is successful
复制代码

检测没问题后启动nginx,由于一般都是实际的业务系统,所以只能通过nginx所在系统的网络连接状态来查看负载均衡情况。


  1. TCP    192.168.150.128:63428    192.168.150.1:8080     ESTABLISHED

  2. TCP    192.168.150.128:63468    192.168.150.1:8080     ESTABLISHED

  3. TCP    192.168.150.128:63478    192.168.150.1:8080     ESTABLISHED

  4. TCP    192.168.150.128:63607    192.168.150.1:8080     ESTABLISHED

  5. TCP    192.168.150.128:63620    192.168.150.1:8080     ESTABLISHED

  6. TCP    192.168.150.128:63622    192.168.150.1:8080     FIN_WAIT_2

  7. TCP    192.168.150.128:63623    192.168.150.1:8080     ESTABLISHED

  8. TCP    192.168.150.128:63624    192.168.150.1:8080     ESTABLISHED

  9. TCP    192.168.150.128:63625    192.168.150.1:8080     ESTABLISHED

  10. TCP    192.168.150.128:63626    192.168.150.1:8080     ESTABLISHED

  11. TCP    192.168.150.128:63627    192.168.150.1:8081     ESTABLISHED

  12. TCP    192.168.150.128:63628    192.168.150.1:8081     ESTABLISHED

  13. TCP    192.168.150.128:63629    192.168.150.1:8080     ESTABLISHED

  14. TCP    192.168.150.128:63630    192.168.150.1:8081     ESTABLISHED

  15. TCP    192.168.150.128:63631    192.168.150.1:8080     ESTABLISHED

  16. TCP    192.168.150.128:63632    192.168.150.1:8080     ESTABLISHED

  17. TCP    192.168.150.128:63633    192.168.150.1:8080     ESTABLISHED

  18. TCP    192.168.150.128:63634    192.168.150.1:8081     ESTABLISHED

  19. TCP    192.168.150.128:63635    192.168.150.1:8080     ESTABLISHED

  20. TCP    192.168.150.128:63636    192.168.150.1:8080     ESTABLISHED

  21. TCP    192.168.150.128:63637    192.168.150.1:8080     ESTABLISHED

  22. TCP    192.168.150.128:63638    192.168.150.1:8080     ESTABLISHED

  23. TCP    192.168.150.128:63639    192.168.150.1:8080     ESTABLISHED

  24. TCP    192.168.150.128:63640    192.168.150.1:8080     ESTABLISHED

  25. TCP    192.168.150.128:63644    192.168.150.1:8080     ESTABLISHED

  26. TCP    192.168.150.128:63622    192.168.150.1:8081     ESTABLISHED

  27. TCP    192.168.150.128:63634    192.168.150.1:8081     ESTABLISHED

  28. TCP    192.168.150.128:63621    192.168.150.1:8081     ESTABLISHED
复制代码

注:其中192.168.150.128为nginx所在主机地址,192.168.150.1为三台Web服务器所在地址。

可以看到nginx所在主机只对2台Web服务器进行了连接分发,这是因为外网用户都是通过2台SSL VPN代理访问负载均衡服务器的,nginx只识别到2个源地址(2台SSL VPN的地址)认为只有2个客户端发起了访问,所以导致了负载不均匀,这种问题的解决方法并不是没有,二是需要调用nginx的扩展功能模块——sticky。由于调用sticky模块又会涉及到编译安装等一些列问题,所以本篇就不做说明了,后期有空再给大家更新。


这次的文章只是简要配置了基于会话保持的nginx负载均衡,其实负载均衡的调优还需要很多的工作量和实践路程,很多的技术难题会在实际的项目实施过程中不断涌现。通过不断地填坑,我争取归纳总结最佳的nginx实战经验与大家继续分享!

打赏鼓励作者,期待更多好文!

打赏
2人已打赏

好心情能长寿 发表于 2018-11-29 18:24
  
学习了,很不错
feeling 发表于 2018-11-30 20:10
  
很不错。
一条软白鲨 发表于 2018-11-30 20:55
  
学习学习了。。
sailyang 发表于 2018-12-1 11:03
  

学习了。感谢分享
法律框架 发表于 2018-12-1 16:38
  
真是难得一见的好帖,收藏·····
ie5000 发表于 2018-12-1 18:46
  
详细,谢谢分享
主动出击 发表于 2018-12-1 21:30
  
感谢分享。
发表新帖
热门标签
全部标签>
每日一问
功能体验
技术笔记
技术盲盒
新版本体验
2023技术争霸赛专题
技术咨询
干货满满
标准化排查
GIF动图学习
产品连连看
安装部署配置
信服课堂视频
秒懂零信任
自助服务平台操作指引
技术晨报
深信服技术支持平台
答题自测
答题榜单公布
卧龙计划
通用技术
畅聊IT
云计算知识
排障笔记本
安全攻防
社区帮助指南
专家问答
技术圆桌
在线直播
MVP
网络基础知识
升级
上网策略
测试报告
日志审计
问题分析处理
流量管理
每日一记
运维工具
用户认证
原创分享
解决方案
sangfor周刊
VPN 对接
项目案例
SANGFOR资讯
专家分享
技术顾问
信服故事
SDP百科
功能咨询
终端接入
授权
设备维护
资源访问
地址转换
虚拟机
存储
迁移
加速技术
产品预警公告
玩转零信任
信服圈儿
S豆商城资讯
技术争霸赛
「智能机器人」
追光者计划
纪元平台
华北区拉练
天逸直播
以战代练
文档捉虫活动
山东区技术晨报
齐鲁TV
华北区交付直播
每周精选

本版版主

12
185
6

发帖

粉丝

关注

本版达人

LoveTec...

本周分享达人

新手24116...

本周提问达人