1. 当后端服务器出现异常,响应码为500 501 502 503 504,请求转发到静态降级服务器,从而保证业务不至于完全无法访问,对于浏览型且实时性要求不高的站点非常有用。
app_servers:应用服务器,提供正常服务页面
shopwebstatic:静态服务器,提供定时爬取的静态页面
2.请求重试:
proxy_next_upstream http_500 http_502 http_504 error timeout invalid_header;
3.根据user agent,cookie特定字段,将来源为PC版用户的请求转发到手机版页面或者恢复访问PC版本页面。
默认手机用户访问站点会跳转到手机版本,跳转判断依据是user agent,当用户点击访问电脑版本的时候,除了要判断用户的user agent外还需要判断用户的访问模式,通过cookie中特定字段的值来判断用户访问请求是到手机版本还是到PC版本。
mode=pc,mode标记用户的访问模式是PC版本,通过此cookie字段,可判断是否将user agent匹配智能手机字段的用户的请求转发到手机版本。
limit_conn_zone $server_name zone=limit:10m; upstream app_servers { server app_server01_ip:80 weight=1 max_fails=2; server app_server01_ip:80 weight=1 max_fails=2 backup; } upstream shopwebstatic { server shopwebstatic_server01_ip:80 weight=5; server shopwebstatic_server02_ip:80 weight=5; } server { listen 80; server_name qunying.liu.dianping.com; # config_apps_begin root /data/webapps/shops-web/shared/webroot; access_log logs/shops-web.access.log main; error_log logs/shops-web.error.log notice; # config_apps_end limit_conn limit 280; proxy_next_upstream http_500 http_502 http_504 error timeout invalid_header; location / { proxy_intercept_errors on; location ~* ^/shop/(/d+)/menu{ set $mobile 0; set $shopid $1; set $hostid 0; if ( $http_user_agent ~* "(Android|iPhone|Windows Phone)" ){ set $mobile "${mobile}1"; } if ( $host ~* "m/.dianping/.com" ){ set $hostid "${hostid}1"; } if ( $http_cookie !~* "mode=pc"){ set $mobile "${mobile}1"; } if ( $hostid = "01" ){ proxy_pass http://app_servers/shop/$shopid/mobilemenu; break; } if ( $mobile = "011" ){ rewrite ^/(.*)$ http://mobile-servers/\ redirect; break; } proxy_pass http://app_servers; break; } location ~* ^/shop/(/d+)/dish-(.*){ set $mobile 0; set $shopid $1; set $dishurl $2; if ( $http_user_agent ~* "(Android|iPhone|Windows Phone)" ){ set $mobile "${mobile}1"; } if ( $http_cookie !~* "mode=pc"){ set $mobile "${mobile}1"; } if ( $mobile = "011" ){ rewrite ^/(.*)$ http://mobile-servers/shop/$shopid/product-$dishurl redirect; break; } proxy_pass http://app_servers; break; } location ~* ^/map/shop/(/d+)$ { set $shopid $1; rewrite ^/(.*)$ http://www.servers/shop/$shopid/map permanent; proxy_set_header Host "www.servers"; break; } location ~* ^/shop/(/d+)(/map|/)?$ { set $mobile 0; if ( $http_user_agent ~* "(Android|iPhone|Windows Phone)" ){ set $mobile "${mobile}1"; } if ( $http_cookie !~* "mode=pc"){ set $mobile "${mobile}1"; } if ( $mobile = "011" ){ rewrite ^/(.*)$ http://mobile-servers/\ redirect; break; } proxy_pass http://app_servers; break; } # appserverfavcion location ~* ^.*/favicon.ico$ { root /data/webapps; expires 30d; break; } if ( !-f $request_filename ) { proxy_pass http://app_servers; break; } } error_page 500 501 502 503 504 @shopstatic; location @shopstatic { access_log logs/shops-static-web.access.log retry; proxy_pass http://shopwebstatic; }
发现过去配置的规则中,由于未判断访问的主机名导致当一个应用同时被配置在pc和手机域名下,当访问pc版本域名跳转手机版本时会产生死循环重定向,如www(手机访问时)—-自动跳转到m域名—m域名解析到应用会再根据user agent和cookie规则跳转一次m域名,然后陷入死循环。
在做域名跳转的时候,最好对域名做一个判断。
location ~* /mylist/{ set $mobile 0; if ( $http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle)" ){ set $mobile "${mobile}1"; } if ( $http_host !~ "m.dianping.com" ){ set $mobile "${mobile}2"; } if ( $cookie_vmod !~ "pc" ){ set $mobile "${mobile}3"; } if ( $mobile = "0123" ){ rewrite ^/(.*)$ http://m.dianping.com/\ last; break; } proxy_pass http://jboss8080; break; }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/116252.html