|
|
@ -238,85 +238,6 @@ ip_hash简单易用,但有如下问题:
|
|
|
|
|
|
|
|
|
|
|
|
来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡
|
|
|
|
来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡
|
|
|
|
|
|
|
|
|
|
|
|
sticky_cookie_insert:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
使用sticky_cookie_insert启用会话亲缘关系,这会导致来自同一客户端的请求被传递到一组服务器的同一台服务器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
与ip_hash不同之处在于,它不是基于IP来判断客户端的,而是基于cookie来判断
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
因此可以避免上述ip_hash中来自同一局域网的客户端和前段代理导致负载失衡的情况
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.部署
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
环境:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
一台服务器做负载均衡并且使用第三方模块sticky来做会话保持
|
|
|
|
|
|
|
|
192.168.182.130(sticky)
|
|
|
|
|
|
|
|
两台服务器做web-server
|
|
|
|
|
|
|
|
192.168.182.131(web-server-1)
|
|
|
|
|
|
|
|
安装nginx并运行 自己创建一个测试界面,使用curl能否访问到内容,保证web-server-1可用。
|
|
|
|
|
|
|
|
192.168.182.132(web-server-2)
|
|
|
|
|
|
|
|
安装nginx并运行 自己创建一个测试界面,使用curl能否访问到内容,保证web-server-2可用。
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
下载插件:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
下载源码包:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解压:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian ~]# unzip 08a395c66e42.zip
|
|
|
|
|
|
|
|
[root@xingdian ~]# tar xf nginx-1.22.0.tar.gz
|
|
|
|
|
|
|
|
[root@xingdian ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
配置:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian nginx-1.22.0]# cd nginx-1.22.0
|
|
|
|
|
|
|
|
[root@xingdian nginx-1.22.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
编译安装:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian nginx-1.22.0]# make && make install
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
配置负载均衡和会话保持:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
vim /etc/nginx/nginx.conf
|
|
|
|
|
|
|
|
以下只是要添加的内容:
|
|
|
|
|
|
|
|
upstream xingdian {
|
|
|
|
|
|
|
|
server 192.168.182.131;
|
|
|
|
|
|
|
|
server 192.168.182.132;
|
|
|
|
|
|
|
|
sticky ;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim /etc/nginx/conf.d/default.conf
|
|
|
|
|
|
|
|
以下为配置文件修改的内容:
|
|
|
|
|
|
|
|
location / {
|
|
|
|
|
|
|
|
proxy_pass http://xingdian;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
重启服务:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdian nginx-1.22.0]# systemctl restart nginx
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 四:动静分离
|
|
|
|
## 四:动静分离
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.介绍
|
|
|
|
#### 1.介绍
|
|
|
|