如何使用 nginx 对后端服务做主备


1. 需求背景

nginx 使用 upstream 模块将后端服务配置成主、备模式,只要主的不挂,流量一直流向主;当主的挂掉,流量流向备节点;目标是替换 keepalived。

2. 实验环境

主机IP 主机名称 安装应用
192.168.10.115 nginx-server nginx
192.168.10.116 tomcat01 tomcat / jdk
192.168.10.50 tomcat02 tomcat / jdk

3. 实验步骤

192.168.10.115 安装 nginx

PASS

192.168.10.116 安装 tomcat01

PASS

192.168.10.50 安装 tomcat02

PASS

修改 nginx.conf,参考如下配置

[root@c7-5 ~]#cat /usr/local/nginx/conf/nginx.conf

#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;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream tomcat_server {
        server 192.168.10.116:8080 max_fails=2 fail_timeout=60s weight=2;
        server 192.168.10.50:8080 max_fails=2 fail_timeout=60s weight=1 backup;
    }  

    server {
        listen       80;
        server_name  localhost;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location ~ .*\.jsp$ {
            proxy_connect_timeout 1;
            proxy_read_timeout 1;
            proxy_send_timeout 1;
            proxy_next_upstream error timeout http_502 http_503 http_504;
            proxy_pass http://tomcat_server;
            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 / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
            root /usr/local/nginx/html/picture;
            expires 10d;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

[root@c7-5 ~]#nginx -t
...
[root@c7-5 ~]#systemctl restart nginx

浏览器访问 nginxhttp://192.168.10.115/index.jsp

不停刷新浏览器,可以发现访问的后端一直是 tomcat01

关闭 tomcat01,刷新浏览器

访问到了 tomcat02

开启 tomcat01,刷新浏览器

访问的还是 tomcat02,没有切换到 tomcat01,等待一会,再刷新,可以切换到 tomcat01。

PS:这样就完成了 nginx 对后端服务做主备。缺点是后端 master 服务恢复后,访问不能快速切换到 master, 需要等待一段时间,这里测试了下大概一分钟。可以使用定时脚本检测服务状态解决。


参考:
Nginx + Tomcat 实现负载均衡
Nginx 配置主备模式


posted @ 2022-07-06 16:46  公博义  阅读(3336)  评论(0编辑  收藏  举报