nginx 手机版页面判断_nginx手机用户访问跳转手机版与PC版页面

本文介绍了如何使用Nginx配置规则,实现当后端服务器异常时转向静态页面,以及根据User Agent和Cookie判断用户设备,进行手机与PC版页面的智能跳转,确保业务可用性和用户体验。
摘要由CSDN通过智能技术生成

工作中为满足业务需要以及保证服务的可用性,配置的一些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;

}

2014.05.25 更新:

发现过去配置的规则中,由于未判断访问的主机名导致当一个应用同时被配置在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/$1 last;

break;

}

proxy_pass                http://jboss8080;

break;

}

本文出自 “从菜鸟到老鸟” 博客,请务必保留此出处http://liuqunying.blog.51cto.com/3984207/1406952

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值