前几天做一个nginx的反向代理,来代理websocket。
因为上线时间的问题,所以是单节点运行。现在准备做集群优化,然后上容器环境。
这样就需要配置nginx的负载均衡。不废话了,下面是配置文件。当然配置后需要验证,验证的时候会出现很多奇怪的问题。这里就不进行描述了。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream testCluserN {
# 这里如果是本机要使用127.0.0.1 测试的时候如果使用ip websocket建立链接的时候会报错
# max_fails=10 fail_timeout=10s 测试的时候也必须要加
# 轮询
server 127.0.0.1:8445 max_fails=10 fail_timeout=10s;
server 10.1.12.153:8445 max_fails=10 fail_timeout=10s;
# 权重
#server 127.0.0.1:8445 max_fails=10 fail_timeout=10s weight = 8;
#server 10.1.12.153:8445 max_fails=10 fail_timeout=10s weight = 10;
# ip_hash
# ip_hash;
#server 127.0.0.1:8445 max_fails=10 fail_timeout=10s ;
#server 10.1.12.153:8445 max_fails=10 fail_timeout=10s ;
# 下面俩种略
# fair(第三方插件)
# url_hash(第三方插件)
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 2345;
server_name 10.1.12.202;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://testCluserN;
proxy_read_timeout 1200s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}