Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to distinguish between different domain names accessed by PC or mobile phone in Nginx configuration

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The new official is online, but the experience of visiting the new official website on the mobile phone is very poor. When you visit the new official website on the mobile phone, you can automatically jump to the old official website by modifying the Nginx configuration.

First, add a jump judgment to the Nginx configuration file of the new official website, and determine whether the source is mobile or PC through user-agent:

Server {listen 80; server_name www.7d.com 7d.com; / / new official website domain name rewrite. * https://$host$request_uri last;} server {listen 443 ssl; server_name www.7d.com 7d.com; / / new official website domain name root / data/7d; charset utf-8; ssl_certificate / usr/local/nginx/_.7d.com.crt; ssl_certificate_key / usr/local/nginx/_.7d.com.key Rewrite / S1 $http://s1.7d.com/start last; if ($http_user_agent ~ * "(Android | iPhone | Windows Phone | UC | Kindle)") {/ / jump judgment rewrite ^ / (.*) $https://m.7d.com$uri redirect; / / redirect represents 302 jump (temporary transfer)}. }

Then change the configuration file of the old official website to the new domain name, and also make a jump judgment, and jump back to the new official website for requests that are not accessed by the mobile end.

Server {listen 80; server_name m.7d.com; / / old official website domain name rewrite. * https://$host$request_uri last;} server {listen 443 ssl; server_name m.7d.com; / / old official website domain name root / data/7d; charset utf-8; ssl_certificate / usr/local/nginx/_.7d.com.crt; ssl_certificate_key / usr/local/nginx/_.7d.com.key Rewrite / S1 $http://s1.7d.com/start last; if ($http_user_agent! ~ * "(Android | iPhone | Windows Phone | UC | Kindle)") {/ / ~ preceded by! If it is not accessed by mobile, jump back to the new official website Rewrite ^ / (.*) $https://m.7d.com$uri redirect; / / redirect means 302Jump}. }

In this way, when the mobile phone accesses the domain name of the new official website 7d.com, it actually accesses the content of the old official website. Click on the browser's address bar and you will find that the domain name is m.7d.com.

In fact, it is based on HTTP_USER_AGENT to judge, using Nginx to do a 302 jump, if the last word in the rewrite sentence redirect is changed to permanent, it means 301jump (permanent transfer).

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report