How to configure Nginx to distinguish between PC or mobile phone access to different domain names

How to configure Nginx to distinguish between PC or mobile phone access to different domain names

The new official website is online, but the experience of accessing the new official website on a mobile phone is very poor. It is required to access the old official website when accessing the new official website on a mobile phone. Automatic jump can be achieved by modifying the Nginx configuration.

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

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)" ){ // Redirect judgment rewrite ^/(.*)$ https://m.7d.com$uri redirect; // redirect means 302 redirect (temporary transfer) 
  }   
  …
}

Then the configuration file of the old official website is changed to the new domain name. Jump judgment is also required to jump back to the new official website if the request is not for mobile access.

 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 a ! before ~. If it is not accessed from a mobile device, it will jump back to the new official website.
    rewrite ^/(.*)$ https://m.7d.com$uri redirect; // redirect means 302 redirect}   
  …
}

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

In fact, it is based on HTTP_USER_AGENT to make a judgment and use Nginx to do a 302 jump. If the last word redirect in the rewrite statement is changed to permanent, it means a 301 jump (permanent transfer).

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation of Nginx configuration of multi-port and multi-domain name access
  • Solve the problem of modifying configuration access of tp5 under nginx
  • How to configure nginx to limit the access frequency of the same IP
  • Steps to configure nginx ssl to implement https access (suitable for novices)
  • Detailed explanation of Nginx access restriction configuration
  • Detailed explanation of nginx using ssl module configuration to support HTTPS access
  • How to use nginx to configure access to wgcloud

<<:  vue uses Ele.me UI to imitate the filtering function of teambition

>>:  mysql method to view the currently used configuration file my.cnf (recommended)

Recommend

Practical explanation of editing files, saving and exiting in linux

How to save and exit after editing a file in Linu...

Introduction to MySQL database performance optimization

Table of contents Why optimize? ? Where to start?...

Advantages and disadvantages of conditional comments in IE

IE's conditional comments are a proprietary (...

Linux installation MySQL tutorial (binary distribution)

This tutorial shares the detailed steps of instal...

Vue implements online preview of PDF files (using pdf.js/iframe/embed)

Preface I am currently working on a high-quality ...

Detailed examples of replace and replace into in MySQL into_Mysql

MySQL replace and replace into are both frequentl...

Detailed explanation of the production principle of jQuery breathing carousel

This article shares the specific process of the j...

HTML Tutorial: Collection of commonly used HTML tags (5)

These introduced HTML tags do not necessarily ful...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

Example code for implementing 3D Rubik's Cube with CSS

Let's make a simple 3D Rubik's Cube today...

Teach you how to write maintainable JS code

Table of contents What is maintainable code? Code...

How to write object and param to play flash in firefox

Copy code The code is as follows: <object clas...

Detailed tutorial on installing Tomcat8.5 in Centos8.2 cloud server environment

Before installing Tomcat, install the JDK environ...

Implementation of element multiple form validation

In the project, form testing is often encountered...