nginx規則:自動降級,手機使用者訪問跳轉手機版與PC版頁面

來源:互聯網
上載者:User

標籤:nginx


工作中為滿足業務需要以及保證服務的可用性,配置的一些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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.