Nginx rules: Auto-downgrade, mobile phone users access to jump mobile version and the PC version of the page

Source: Internet
Author: User
Tags response code


Work in order to meet business needs and ensure the availability of services, the configuration of some nginx jump rules, and the company's business-related information has been erased, to provide hope for everyone to help.


1. When the backend server has an exception and the response code is 500 501 502 503 504, the request is forwarded to a static downgrade server, which ensures that the business is not completely inaccessible, and is useful for sites with low-profile and real-time requirements.

App_servers: Application server, providing normal service page

Shopwebstatic: Static server, which provides a static page for timed crawls


2. Request Retry:

Proxy_next_upstream http_500 http_502 http_504 error timeout invalid_header;


3. Depending on the user Agent,cookie specific field, forward the request from the PC version to the mobile page or restore the access to the PC version page.

The default mobile phone user access to the site will jump to the phone version, jump to determine the basis is the user agent, when users click to access the computer version, in addition to judge the user agent needs to determine the user's access mode, The value of a specific field in a cookie determines whether a user's access request is to a mobile version or to a PC version.


mode=pc,mode the user's access mode is the PC version, and through this cookie field, it is possible to determine whether the user agent's request to match the smartphone field is forwarded to the phone version.




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 $;               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 ($host                   id = "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 $;               Set $dishurl $; 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 $;                   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 501 502 503 504 @shopstatic;       Location @shopstatic {access_log Logs/shops-static-web.access.log retry;    Proxy_pass http://shopwebstatic; }


This article is from "rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1406952

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.