使用 Nginx 本地模拟集群访问
1、http session 共享 2、websocket 连接断开机制 3、cas 单点登录
二、安装 Nginx
http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz
https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar zxvf pcre-8.44.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/xxxxx/pcre-8.44
make
make install
# 查看 Nginx 版本
/usr/local/nginx/sbin/nginx -v
三、配置 Nginx
conf
目录,新建目录 servers
mkdir servers
nginx.conf
文件,在 http 全局块中增加配置 include servers/*.conf;
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include servers/*.conf;
server {
listen 80;
...
test.conf
upstream mytest {
server 127.0.0.1:8081 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8082 max_fails=3 fail_timeout=30s;
}
server {
listen 8099;
server_name 127.0.0.1;
location / {
proxy_pass http://mytest;
# proxy_set_header Host $host;
# 以下配置是支持 websocket 请求
proxy_set_header Upgrade $http_upgrade;
proxy_http_version 1.1;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
./nginx
# 如果修改配置需要重启则使用
./nginx -s reload 命令
127.0.0.1:8099
测试,可以在后台看到是访问到了不同的端口服务。
# 重新载入配置文件
./nginx -s reload
# 停止
./nginx -s stop
# 重启
./nginx -s reopen
weight
指定权重)、IP HASH 、扩展策略。
www.testlyqiang.com
被转发到两台后端服务上,只需要将 test.conf
改为如下配置,并且配置 hosts
:
upstream www.testlyqiang.com {
server 127.0.0.1:8081 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8082 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.testlyqiang.com;
location / {
proxy_pass http://www.testlyqiang.com;
# proxy_set_header Host $host;
# 以下配置是支持 websocket 请求
proxy_set_header Upgrade $http_upgrade;
proxy_http_version 1.1;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 www.testlyqiang.com