How to implement nested if method in nginx

How to implement nested if method in nginx

Nginx does not support nested if statements, nor does it allow logical judgment in if statements. The following error will be reported:

nginx: [emerg] "if" directive is not allowed

When the business requires multiple condition judgments, it can be implemented with the help of intermediate variables

For example, our website has multiple subdomains on the PC side, but only one domain name on the mobile side. The corresponding relationship is as follows:

  • www.test.com --> m.test.com
  • sub1.test.com --> m.test.com/sub1
  • sub2.test.com --> m.test.com/sub2
  • sub3.test.com --> m.test.com/sub3

The effect to be achieved: When accessing the PC domain name on the mobile terminal, 301 jump to the corresponding mobile terminal domain name

The rewrite rules for nginx are as follows:

# Is it a mobile terminal? set $mobile 0;
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
  set $mobile 1;
}

# Get the subdomain set $prefix 1;
if ($host ~* "sub1.test.com") {
  set $prefix 2;
}
if ($host ~* "sub2.test.com") {
  set $prefix 3;
}
if ($host ~* "sub3.test.com") {
  set $prefix 4;
}
set $sign "${mobile}${prefix}";
if ($sign = 11) {
  rewrite ^(.*) http://m.test.com$1 permanent;
}
if ($sign = 12) {
  rewrite ^(.*) http://m.test.com/sub1$1 permanent;
}
if ($sign = 13) {
  rewrite ^(.*) http://m.test.com/sub2$1 permanent;
}
if ($sign = 14) {
  rewrite ^(.*) http://m.test.com/sub3$1 permanent;
}

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:
  • Solution to "No input file specified" in nginx+php
  • Detailed explanation of judgment conditions and multiple condition judgments of if statements in Nginx
  • Nginx implements if multiple judgment configuration method example
  • Implementing mathematical comparison function in IF statement in Nginx
  • Examples of using IF, AND, and OR statements in Nginx
  • Nginx if statement plus regular expression to achieve string truncation

<<:  MySQL 5.7.18 Green Edition Download and Installation Tutorial

>>:  JavaScript to achieve full or reverse selection effect in form

Recommend

Win32 MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

Steps to split and compress CSS with webpack and import it with link

Let's take a look at the code file structure ...

Steps for installing MySQL 8.0.16 on Windows and solutions to errors

1. Introduction: I think the changes after mysql8...

Detailed explanation of how to view the current number of MySQL connections

1. View the detailed information of all current c...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

MySQL 5.7.20 compressed version download and installation simple tutorial

1. Download address: http://dev.mysql.com/downloa...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

mysql 8.0.18 mgr installation and its switching function

1. System installation package yum -y install mak...

How to implement the King of Glory matching personnel loading page with CSS3

Those who have played King of Glory should be fam...

mysql5.7 remote access settings

Setting up remote access in mysql5.7 is not like ...

How to elegantly implement the mobile login and registration module in vue3

Table of contents Preface Input box component lay...

Implementation of Nginx+ModSecurity security module deployment

Table of contents 1. Download 2. Deployment 1.Ngi...

Tutorial on processing static resources in Tomcat

Preface All requests in Tomcat are handled by Ser...