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

CSS and HTML and front-end technology layer diagram

The relationship between Javascript and DOM is ve...

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers 1、onkeyup = ...

20 JavaScript tips to help you improve development efficiency

Table of contents 1. Declare and initialize array...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

In-depth explanation of Session and Cookie in Tomcat

Preface HTTP is a stateless communication protoco...

Detailed explanation of JavaScript upload file limit parameter case

Project scenario: 1. Upload file restrictions Fun...

Best Practices for Sharing React Code

When any project develops to a certain complexity...

A brief discussion on JS prototype and prototype chain

Table of contents 1. Prototype 2. Prototype point...

Vue implements the question answering function

1. Request answer interface 2. Determine whether ...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

A brief discussion on the problem of Docker run container being in created state

In a recent problem, there is such a phenomenon: ...

Example of how to implement value transfer between WeChat mini program pages

Passing values ​​between mini program pages Good ...

Three JavaScript methods to solve the Joseph ring problem

Table of contents Overview Problem Description Ci...