haproxy implements session binding based on cookies

Session binding of haproxy based on cookie:
Experimental premise:
1. This experiment is implemented on the 64-bit system of centos7.2
2. There are 3 hosts in the experiment, and the address topology is as follows
haproxy proxy: 192.168.137.216
web1: 192.168.137.131
web2: 192.168.137.128

   3.测试时请关闭防火墙和selinux以免带来问题

One: Install the software
(1) Configure web1
1.1 First install httpd on the web1 server
yum install httpd -y

      1.2 在web1上httpd根目录下写入内容到index.html
          echo "this is web1" >/var/www/html/index.html

      1.3 启动httpd
          systemctl start httpd.service

(2) Configure web2
2.1 Install httpd on the web2 server
yum install httpd -y

      2.2 写入内容到httpd根目录
          echo "this is web2" > /var/www/html/index.html

      2.3 启动httpd
          systemctl start httpd.service

(3) Configure haproxy
3.1 yum install haproxy
yum install haproxy -y

      3.2 配置haproxy
       haproxy安装后会在etc/haproxy目录下生成haproxy.cfg的配置文件,大多数的配置选项都在这个文件中定义
打开配置文件然后修改零添加下列选项:

        backend webserver
balance     roundrobin
cookie      serverid insert nocache indirect
server      web1 192.168.137.131:80 check cookie webserver1
server      web2 192.168.137.128:80 check cookie webserver2

haproxy implements session binding based on cookies

3.3 Start
haproxy systemctl start haproxy.service

注言:
   在实验中我使用的cookie的选项是insert,nocache和indirect.这几个选项的作用不同.
        indirect:
              如果不配合"indirect"选项,服务端可以看到客户端请求时的所有cookie信息。如果配合"indirect"选项,则haproxy在将请求转发给后端时,将删除自己设置的cookie,使得后端只能看到它自己的cookie,这样对后端来说,整个过程是完全透明的,它不知道前面有负载均衡软件
           preserve:
              如果insert关键字配合"preserve"关键字,那么当后端设置了cookie时,haproxy将强制保留该cookie,不做任何修改。例如在后端服务器上的应用程序也设置了cookie,例如后端的cookie名称为PHPSESSID,那么也就是说,如果将haproxy的cookie名称也设置为PHPSESSID,那么客户端第一次请求时收到的响应报文中将只有一个"Set-Cookie"字段,且这个字段的值是后端服务器设置的,和haproxy无关
           ignore-persist:
              其实,通过cookie表保持和后端的会话只是默认情况,haproxy允许"即使使用了cookie也不进行会话绑定"的功能。这可以通过ignore-persist指令来实现。当满足该指令的要求时,表示不将该cookie插入到cookie表中,因此无法实现会话保持,即使haproxy设置了cookie也没用。
               例如:
                  backend dynamic_group
                    acl  url_dynamic   path_end  -i .php
                    ignore-persist if  url_dynamic
                    cookie app_cook insert nocache
                    server app1 192.168.100.60:80 cookie app_server1
                    server app2 192.168.100.61:80 cookie app_server2
               这表示当请求uri以".php"结尾时,将忽略会话保持功能。这表示,对于php结尾的请求,app_cook这个cookie从头到尾都是摆设
                 当然,上面的设置是不合理的,更合理的应该是这样的
                    acl url_static  path_beg         /static /images /img /css
                    acl url_static  path_end         .gif .png .jpg .css .js
                    ignore-persist  if url_static
               与ignore-persist相对的是force-persist,但不建议使用该选项,因为它和option redispatch冲突

Screenshot of the result achieved:
haproxy implements session binding based on cookies

     在其中有cookie首部,值是我刚才设置的webserver1.多次访问的也都是web1

     ![](http://i2.51cto.com/images/blog/201804/23/37811dba033f582f5c1df1ebbb45d01a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

     此次试验到此结束!
       如有不当之处请指出,作者会努力修改!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324698594&siteId=291194637