使用Nginx对后端服务做主备

原文转载自:https://www.cnblogs.com/shenyuanhaojie/p/16451472.htmlicon-default.png?t=N7T8https://www.cnblogs.com/shenyuanhaojie/p/16451472.html

1. 需求背景

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

2. 实验环境

主机IP主机名称安装应用
192.168.10.115nginx-servernginx
192.168.10.116tomcat01tomcat / jdk
192.168.10.50tomcat02tomcat / jdk

3. 实验步骤

  • 192.168.10.115 安装 nginx
  • 192.168.10.116 安装 tomcat01
  • 192.168.10.50 安装 tomcat02

修改 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, 需要等待一段时间,这里测试了下大概一分钟。可以使用定时脚本检测服务状态解决。

4. 参考文章

Nginx + Tomcat 实现负载均衡_nigex 负载均衡tomcat-优快云博客文章浏览阅读4w次,点赞2次,收藏18次。文章目录引言一、案列架构引言  以 LNMP 为例,一个企业内部最基础的架构组成需要一个处理静态 Web 服务的页面,一个动态 Web 服务的页面和数据库。而我们在 Linux 平台上通过 Nginx + PHP 实现动静分离,而实际生产中往往一台 nginx 需要 “对应” 多个动态处理的服务(即 tomcat),所以如何将前端接收到的动态请求转交给后端多个 tomcat 处理,是我们此处研究的内容。一、案列架构..._nigex 负载均衡tomcathttps://blog.youkuaiyun.com/shenyuanhaojie/article/details/120839008?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165709703216782391835720%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165709703216782391835720&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-3-120839008-null-null.185%5Ev2%5Econtrol&utm_term=nginx&spm=1018.2226.3001.4450Nginx配置主备模式配置_nginx主备-优快云博客文章浏览阅读4.1k次。//配置nginx 负载IP端口upstream tomcatserver{server localhost:8082 max_fails=2 fail_timeout=30s;server localhost:8083 max_fails=2 fail_timeout=30s backup;}server { listen 9300; server_name localhost; fastcgi_connect_timeo..._nginx主备https://blog.youkuaiyun.com/weixin_46652177/article/details/122882471

Nginx是一个高性能的HTTP和反向代理服务器,也可以作为IMAP/POP3/SMTP代理服务器。在配置Nginx作为反向代理以连接到后端服务时,你需要设置一个或多个server块,这些块中包含location块来定义如何转发请求到后端服务器。 以下是一个简单的Nginx配置示例,展示了如何配置Nginx以将请求转发到后端的HTTP服务: ``` http { # 定义全局变量和参数 ... # 服务器块开始 server { # 监听端口 listen 80; # 服务器名称 server_name example.com; # 处理根目录下的请求 location / { # 转发到后端服务的地址和端口 proxy_pass http://backend_server_ip:backend_server_port; # 设置一些代理相关的参数 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 配置连接超时时间等 proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; proxy_buffers 4 32k; ... } # 处理其他路径的请求 location /api { ... } # 其他配置... } # 服务器块结束 ... } ``` 在这个配置中,`server`块定义了一个虚拟主机,监听80端口,并指定了服务器名称。`location /`块定义了当用户访问根路径时,Nginx将请求转发到`backend_server_ip`指定的后端服务器的`backend_server_port`端口。通过`proxy_pass`指令指定后端服务的URL,其他`proxy_set_header`指令用于添加或修改传递到后端服务的HTTP头部信息,以确保后端服务能够正确地识别原始请求的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值