Nginx代理Redis哨兵主从配置

宗政欣可
2023-12-01

Nginx代理Redis哨兵主从配置

一、环境

Nginx版本:1.21.6
Center7.5及以上或Mas OS
搭建Redis哨兵主从模式
Springboot集成Redis哨兵主从模式

  • 提示:Nginx必须安装upstream模块

二、配置

Nginx配置有三个方案(详见后续内容),其中:方案一将哨兵所有节点使用一个端口对外映射;方案二是每个哨兵端口配置一个对应的映射端口,总体和正常哨兵配置方式无差别;方案三其实就是方案一和方案二的结合体。个人认为其意义不大,有兴趣的小伙伴可以自行尝试。

2.1、方案一(推荐)
# stream模块配置和http模块在相同级别
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
}
2.2、方案二
# stream模块配置和http模块在相同级别
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
    }
    upstream redis1 {
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
    }
    upstream redis2 {
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
    server {
        listen 5433;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis1;
    }
    server {
        listen 5434;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis2;
    }
}
2.3、方案三
# stream模块配置和http模块在相同级别
stream {
    upstream redis {
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    upstream redis1 {
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
    }
    upstream redis2 {
        server 127.0.0.1:26381 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26380 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:26379 max_fails=3 fail_timeout=10s;
    }
    server {
        listen 5432;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis;
    }
    server {
        listen 5433;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis1;
    }
    server {
        listen 5434;
        proxy_connect_timeout 30s;
        proxy_timeout 60s;
        proxy_pass redis2;
    }
}
 类似资料: