nginx规则:自动降级,手机用户访问跳转手机版与PC版页面
工作中为满足业务需要以及保证服务的可用性,配置的一些nginx跳转规则,与公司业务相关信息已经抹去,提供出来希望对大家有帮助。
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/$1 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/$1 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; }
本文出自 “从菜鸟到老鸟” 博客,请务必保留此出处http://liuqunying.blog.51cto.com/3984207/1406952
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。