keepalived+nginx负载均衡反向代理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived+nginx负载均衡反向代理相关的知识,希望对你有一定的参考价值。

keepalived配置

vrrp_script chk_http_port {
    script "/root/check_nginx_pid.sh"
    interval 2                          #(检测脚本执行的间隔)
    weight 2
}
vrrp_instance VI_1 {
    #备用服务器上为 BACKUP
    state MASTER
    #绑定vip的网卡为ens33,你的网卡和阿铭的可能不一样,这里需要你改一下
    interface ens33
    virtual_router_id 52
    #备用服务器上为90
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_http_port            #(调用检测脚本)
    }
    virtual_ipaddress {
        192.168.198.24
    }
}

nginx反向代理配置:
user nginx;
worker_processes 2;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘

                   ‘$status $body_bytes_sent "$http_referer" ‘

                   ‘"$http_user_agent" "$http_x_forwarded_for"‘;
access_log    /var/log/nginx/access.log main;
#sendfile on;
tcp_nopush      on;
tcp_nodelay     on;
keepalive_timeout  65;
 gzip  on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 client_header_buffer_size    1k;
large_client_header_buffers  4 4k;

upstream proxy_test {
server 192.168.198.144:80 weight=4 max_fails=2 fail_timeout=30s; #如果你要测试,把这里换成你自己要代理后端的ip
server 192.168.198.145:80 weight=9 max_fails=2 fail_timeout=30s;
#ip_hash; #当负载两台以上用ip来hash解决session的问题,一台就别hash了。
}
server {
listen 80;
server_name www.mysvr1.com;
location / {
proxy_pass http://proxy_test; #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口, 或url
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .(html|php|jsp|jspx|dp)?$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://proxy_test; #转向tomcat处理
}
}
}

以上是关于keepalived+nginx负载均衡反向代理的主要内容,如果未能解决你的问题,请参考以下文章

Nginx反向代理,负载均衡,redis session共享,keepalived高可用

nginx反向代理tomacat+keepalived实现动静分离负载均衡高可用

Tomcat+nginx+Keepalived部署反向代理负载均衡高可用

LVS(负载均衡)+keepalived(HA)+Nginx(反向代理)+Web(动静态网站服务器)

Nginx安装 配置反向代理 负载均衡 upstream ssl证书提供https访问 ha nginx keepalived双主热备 LVS实现高可用负载 Keepalived+Lvs+Nginx

keepalived高可用集群