nginx 负载均衡配置

要保持session的话使用ip_hash

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=static:10m inactive=30d max_size=1g;

upstream tomcat {
#        ip_hash       ;
        #hash           $remote_addr consistent;
        server          127.0.0.1:8082 max_fails=1 fail_timeout=2s ; 
        server          127.0.0.1:8083 max_fails=1 fail_timeout=2s ;
        server          127.0.0.1:8084 max_fails=1 fail_timeout=2s ;
    server          127.0.0.1:8081 max_fails=1 fail_timeout=2s ;
    keepalive       16;
}
upstream tomcat-web {
    ip_hash       ;
    server        127.0.0.1:8081 max_fails=1 fail_timeout=2s;
    keepalive       16;
}
upstream tomcat-web-ua {
        ip_hash       ;
        server        127.0.0.1:8081 max_fails=1 fail_timeout=2s;
    server        127.0.0.1:8082 max_fails=1 fail_timeout=2s;
        keepalive       16;
}

server {
        listen       80;
        listen       [::]:80;
        server_name     bkt.jeagine.com;
        charset         utf-8;
        access_log      /var/log/nginx/tomcat.access.log  main;
        root            xxxx;
        index           index.html index.htm index.jsp;
    location ~  xxxx {
        if ($http_user_agent ~ "(Windows NT)" ) {
           proxy_pass              http://www.baidu.com;
           break;
           return 403;
        }
            proxy_pass              http://tomcat-web;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";
                add_header              X-Backend "$upstream_addr";
    }
    location ~ xxxx {
        proxy_pass              http://tomcat-web;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
    }

    location ~ xxxxx {
                proxy_pass              http://tomcat-web;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
        }
        location /api {
                proxy_pass              http://tomcat/api;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
        }
    location ^~  xxxxx {
                proxy_pass              http://tomcat-web-ua;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
        }
      location ~* ^.+.(js|css|ico|gif|jpg|jpeg|png|svg|xls)$ {
                proxy_pass              http://tomcat ;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                proxy_cache             static;
                proxy_cache_key         $host$uri$is_args$args;
                proxy_cache_valid       200 302 7d;
                proxy_cache_valid       404 1m;
                proxy_cache_valid       any 1h;
                add_header              X-Cache $upstream_cache_status;

                #log_not_found off;
                #access_log off;
                expires max;
        }
     location / {
                proxy_pass              http://tomcat-web-ua;
                proxy_redirect          off;
                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_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;

                proxy_connect_timeout   65;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";

                add_header              X-Backend "$upstream_addr";
        }

        location ~ /.ht {
                deny  all;
        }

}

关于if 语句

if (){
}

要有空格
推荐博客
https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
if 很重要

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值