一、安装Nginx
选择一个安装即可。
二、Nginx配置文件修改
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream xdx97.com {
server 127.0.0.1:1997;
server 127.0.0.1:1998;
}
server {
listen 8089;
location / {
proxy_pass http://xdx97.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
完整的nginx.conf配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 9876;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream xdx97.com {
server 127.0.0.1:1997;
server 127.0.0.1:1998;
}
server {
listen 8089;
location / {
proxy_pass http://xdx97.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
三、测试
3-1、开启你本地的两个服务
3-2、使用下面这个在线工具进行测试
http://www.easyswoole.com/wstool.html
3-3、测试结果如下
四、其它
nginx60s_115">4-1、nginx默认60s没有消息传输,就会中断连接。
解决办法一: 把这个60s,设置的大一些。
proxy_read_timeout 600s;
具体位置
server {
listen 8089;
location / {
proxy_pass http://xdx97.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 600s;
}
}
解决办法二: 前端写一个定时方法,定时去请求建立心跳连接。
4-2、Nginx实现负载均衡Linux版本(六种负载策略)
https://www.xdx97.com/article/793503147019468800
参考文章:
https://blog.csdn.net/thbsp/article/details/80377480
https://www.cnblogs.com/interdrp/p/12148200.html