Nginx mobile phone user access jump to the mobile phone version and PC version page

Source: Internet
Author: User

In order to meet business needs and ensure service availability, some nginx jump rules configured in the work have been erased from the company's business information and hope to help everyone.

1. when an exception occurs on the backend server, the response code is 500 501 502 503 504. The request is forwarded to the static downgrade server to ensure that the service is not completely inaccessible, it is very useful for browsing websites with low real-time requirements.

App_servers: Application Server, which provides the normal service page

Shopwebstatic: a static server that provides timed crawling of static pages

2. Request retry:

Proxy_next_upstream http_500 http_502 http_504 error timeout invalid_header;

3. Based on the user agent and cookie-specific fields, future requests from the source PC users will be forwarded to the mobile phone version page or the PC version page will be restored.

By default, a mobile phone user accesses the website and jumps to the mobile phone version. The jump judgment is based on the user agent. When a user clicks to access the computer version, in addition to determining the user agent, you also need to determine the user access mode and determine whether the user access request is sent to the mobile phone version or the PC version through the value of a specific field in the cookie.

Mode = pc. The mode indicates that the user's access mode is PC. Using this cookie field, you can determine whether to forward user requests matching the user agent's smart phone field to the mobile phone version.

Limit_conn_zone $ server_namezone = limit: 10 m; upstreamapp_servers {weight: 80 weight = 1max_fails = 2; weight: 80 weight = 1max_fails = 2 backup;} upstreamshopwebstatic {weight: 80 weight = 5; servershopwebstatic_server02_ip: 80 weight = 5;} server {listen80; listener; # config_appsinroot/data/webapps/shops-web/shared/webroot; access_logs/shops-w Eb. access. logmain; error_loglogs/shops-web.error.lognotice; # config_assist_endlimit_connlimit280; dynamic; location/{proxy_intercept_errorson; location ~ * ^/Shop/(\ d +)/menu {set $ mobile0; set $ shopid $1; set $ hostid0; if ($ http_user_agent ~ * "(Android | iPhone | WindowsPhone)") {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 $ mobile0; set $ shopid $1; set $ dishurl $2; if ($ http_user_agent ~ * "(Android | iPhone | WindowsPhone)") {set $ mobile "$ {mobile} 1";} if ($ http_cookie !~ * "Mode = pc") {set $ mobile "$ {mobile} 1";} if ($ mobile = "011") {rewrite ^/(. *) $ http://mobile-servers/ Shop/$ shopid/product-$ dishurlredirect; break;} proxy_pass http://app_servers;break ;} Location ~ * ^/Map/shop/(\ d +) $ {set $ shopid $1; rewrite ^/(. *) $ http://www.servers/shop/ $ Shopid/mappermanent; proxy_set_headerHost "www. servers"; break;} location ~ * ^/Shop/(\ d +) (/map | /)? $ {Set $ mobile0; if ($ http_user_agent ~ * "(Android | iPhone | WindowsPhone)") {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 ;} # Appserverfavcionlocation ~ * ^. */Favicon. ico $ {root/data/webapps; expires30d; break;} if (! -F $ request_filename) {proxy_pass http://app_servers;break ;} Error_page500501502503504 @ shopstatic; location @ shopstatic {access_logs/shops-static-web.access.logretry; proxy_pass http://shopwebstatic ;}

Update:

It is found that in the past configured rules, because the access host name is not determined, when an application is configured under both the pc and mobile phone domain names, when the Domain Name of the pc version is redirected to the mobile phone version, an endless loop redirection occurs, such as www (when the mobile phone is accessed) ---- automatically jump to the m domain name --- after the m domain name is resolved to the application, it will jump to the m domain name once according to the user agent and cookie rules, and then into an endless loop.

When performing domain name jump, it is best to make a judgment on the domain name.

Location ~ */Mylist/{set $ mobile0; if ($ http_user_agent ~ * "(Android | iPhone | WindowsPhone | 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 ^/(. *) $ response ;}

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

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.