1.nginx的下载编译安装这里略过(各种依赖问题自己解决)
2.下载Nginx一致性hash模块
地址:https://github.com/replay/ngx_http_consistent_hash
下载完成后上传到linux服务器
3.重新编译nginx(.configure),例如:
./configure --prefix=/home/zmoon/nginx-1.5.12 --with-pcre=../pcre-8.40 --with-stream --with-ngx_http_consistent_hash
4.配置hash一致性模块 (放在配置文件http模块下)
upstream mq{ consistent_hash $remote_addr; # server id 字段,如果配置id字段,则使用id字段作为server标识,否则使用server ip和端口作为server标识 server 192.168.51.65:15672 id=111 weight=1; server 192.168.50.177:15672 id=222 weight=1; } server{ listen 15671; server_name localhost; location / { #故障转移的条件:如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。 proxy_next_upstream http_502 http_504 error timeout invalid_header ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://mq; } }
完成配置
备注:故障转移有问题,还在找解决方案
nginx 1.18.0 后版本可以直接使用hash指令 配置一致性hash 并且支持故障转移 只需要修改配置文件
配置如下:
upstream mq{ hash $request_uri consistent; server 192.168.51.65:15672 weight=1; server 192.168.50.177:15672 weight=1; } # upstream mq{ # ip_hash; # server 192.168.51.65:15672 weight=1 fail_timeout=10s max_fails=2; # server 192.168.50.177:15672 weight=1 fail_timeout=10s max_fails=2; # } server{ listen 15671; server_name localhost; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://mq; } }
以上配置是配置在http模块走的七层代理,但是发送mq消息 需要代理 5672 端口不能用以上配置 需要时用4层的代理配置 配置文件需要放在stream(和http模块平级)模块下,并且$request_uri 参数不能使用
stream{ upstream mq2{ hash $remote_addr consistent; server 192.168.51.65:5672 weight=1; server 192.168.51.67:5672 weight=1; } server{ listen 5671; proxy_pass mq2; } }
由于access.log 里面没有走四层代理的日志 可以添加日志打印方式如下:
stream{
# 日志打印 log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol "$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"'; access_log logs/stream-access.log proxy; upstream mq2{ hash $remote_addr consistent; server 192.168.51.114:5672 weight=1; server 192.168.50.177:5672 weight=1; } server{ listen 5671; proxy_pass mq2; } }
参考文档:
http://lnmp.ailinux.net/nginx-hash
https://www.cnblogs.com/rxysg/p/15679534.html
使用ip_hash参考:https://www.cnblogs.com/rxysg/p/15679534.html
修改ip_hash局域网失效问题:https://blog.csdn.net/qq_45367825/article/details/111355402
nginx负载均衡策略:https://blog.csdn.net/zhouhengzhe/article/details/114573924