目录

Nginx负载均衡(反向代理)

7层负载均衡

7层就是用域名来进行转发

类似配置文件:

vim /etc/nginx/conf.d/xxx.conf

upstream  wordpress {
    server  192.168.1.20:8000 weight=5 max_fails=3 fail_timeout=30s;
    server  192.168.1.21:8000 weight=5 max_fails=3 fail_timeout=30s;
}

server {
    listen 80 ;
    server_name wordpress.zahui.fan;

    location / {
        proxy_pass http://wordpress;
    }
}

4层负载

4层负载就是类似端口转发

vim /etc/nginx.conf最后添加:

stream {
    include port_forward.conf;
}

vim /etc/nginx/port_forward.conf

简单配置

# 访问本机的5555端口, 转发到172.16.1.7的22端口
server {
    listen 5555;
    proxy_pass 172.16.1.7:22;
}

复杂配置

upstream mysql-server {
    server 192.168.1.20:3306 weight=5 max_fails=3 fail_timeout=30s;
    server 192.168.1.21:3306 weight=5 max_fails=3 fail_timeout=30s;
    server 192.168.1.22:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
    listen 3306;
    proxy_connect_timeout 1s;
    proxy_timeout 10s;
    proxy_pass mysql-server;
}