nginx 负载均衡配置

来源:互联网 发布:桌面手机支架 知乎 编辑:程序博客网 时间:2024/06/06 18:52

要保持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 很重要

原创粉丝点击