Nginx

反向代理
       proxy_pass http://127.0.0.1:80;
 
负载均衡
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream?bakend?{
server?192.168.0.14;
server?192.168.0.15;
}
 
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream?bakend?{
server?192.168.0.14?weight=10;
server?192.168.0.15?weight=1;
}
 
故障跳转
       error_page 404 /404.html;
            location = /40x.html {
        }
 
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
 
proxy_next_upstream error timeout http_404 http_502 http_503 http_504;
 
数据压缩
                gzip on;
                gzip_proxied any;
                gzip_http_version 1.0;
                gzip_types text/plain application/x-javascript text/css application/xml application/json;
                gzip_comp_level 3;
                gzip_buffers 4 8k;
 
 
地址重写
rewrite ^(.*)$?https://$host$1 permanent;
  location ^~/lds/bjsv2/json/v1 {
                rewrite ^/bjsv2/json/v1/external/BusinessLogService/pushBusinessLog/gzip                        /web-outer/json/v1/external/BusinessLogService/pushBusinessLog/gzip                     break;
                rewrite ^/bjsv2/json/v1/external/InvestmentService/pushInvestment/gzip                          /web-outer/json/v1/external/InvestmentService/pushInvestment/gzip                       break;
                rewrite ^/bjsv2/json/v1/external/ProductService/publishProduct/gzip                             /web-outer/json/v1/external/ProductService/publishProduct/gzip                          break;
                rewrite ^/bjsv2/json/v1/external/AccountService/pushAccount/gzip                                /web-outer/json/v1/external/AccountService/pushAccount/gzip                             break;
rewrite ^/bjsv2/json/v1/external/TokenService/refreshToken/gzip             /webouter/json/v1/external/TokenService/refreshToken/gzip                              break;
                rewrite ^/bjsv2/json/v1/external/InvestmentService/pushInvestmentRewardExtra/gzip               /web-outer/json/v1/external/InvestmentService/pushInvestmentRewardExtra/gzip            break;
                rewrite ^/bjsv2/json/v1/external/TokenService/getAccessToken/gzip                               /web-outer/json/v1/external/TokenService/getAccessToken/gzip                            break;
                rewrite ^/bjsv2/json/v1/external/SupplementService/pushUserInvitationCode/gzip                  /web-outer/json/v1/external/SupplementService/pushUserInvitationCode/gzip               break;
                gzip on;
                gzip_proxied any;
                gzip_http_version 1.0;
                gzip_types text/plain application/x-javascript text/css application/xml application/json;
                gzip_comp_level 3;
                gzip_buffers 4 8k;
                proxy_set_header Host $proxy_host;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_pass http://out;
        }
 
 
ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream?bakend?{
ip_hash;
server?192.168.0.14:88;
server?192.168.0.15:80;
}
 
fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream?backend?{
server?server1;
server?server2;
fair;
}
 
基于语言
http {
upstream rsphp {
             server 192.168.3.3:8080;
}
upstream rsjsp {
             server 192.168.3.20;
}
location ~* \.php$ {
proxy_pass http://rsphp;
}
location ~* \.jsp$ {
proxy_pass http://rsjsp;
}
 
基于浏览器
location / {
if ( $http_user_agent ~* chrome) {
proxy_pass http://rsphp;
}
if ( $http_user_agent ~* firefox) {
proxy_pass http://rsjsp
}
 
基于源地址的分发
安装geoip模块
vim nginx.conf
http {
upstream bj.server {
        server 192.168.3.3;
}
upstream sh.server {
        server 192.168.3.20;
}
upstream default.server {
        server 192.168.3.4;
}
geo $geo {
        default default;
        192.168.3.100/32 bj;
        192.168.3.110/32 sh;
}
server {
               location / {
proxy_pass http://$geo.server$request_uri;
?
        }
posted @ 2018-11-12 10:41  huifeidezhu  阅读(168)  评论(0编辑  收藏  举报