Home>

New official website,But the experience of accessing the new official website on the mobile phone is very poor,Require access to the old website when accessing the new website on your phone,You can modify the nginx configuration to achieve automatic redirection.

The first is the new official website's nginx configuration file plus a jump judgment.Use user-agent to determine whether the source is mobile or pc:

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 means 302 redirect (temporary transfer)
  }
  ...
}

Then the configuration file of the old official website was changed to the new domain name.Also do jump judgment,Redirect requests that are not mobile access back to the new official website.

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)") {//~ Add one before! , If it ’s not mobile access,Jump back to the new official website.
    rewrite ^/(. *) $https://m.7d.com$uri redirect;//redirect means 302 redirect
  }
  ...
}

In this way, when the mobile phone accesses the 7d.com domain name of the new official website,Actually visited the content of the old official website.Click on your browser's address bar.You will find the domain name is m.7d.com.

In fact, it is judged based on http_user_agent.Use nginx to make a 302 jump. If the last word redirect in the rewrite statement is changed to permanent, it means a 301 jump (permanent transfer).

  • Previous Spring boot + mybatis to dynamically switch data source example code
  • Next Python find out the largest or smallest number of indexes in a list