Proxy_pass method in multiple if in nginx location

Proxy_pass method in multiple if in nginx location

1. First, let's review the relevant knowledge about location in nginx

1) Location matching instructions:

  • ~ #The wavy line indicates a regular match, case-sensitive
  • ~* # indicates to perform a regular match, not case sensitive
  • ^~ #^~ indicates common character matching, not regular matching. If this option matches, only this option will be matched, and no other options will be matched. It is generally used to match directories.
  • = # Perform an exact match of common characters
  • @ #"@" defines a named location, used for internal targeting, such as error_page, try_files

2) The priority of location matching (regardless of the order of locations in the configuration file)

1.= Exact matches are processed first. If an exact match is found, nginx stops searching for further matches.
2. Ordinary character matching, regular expression rules and long block rules will be given priority over query matching, that is, if the item matches, it is also necessary to see if there is a regular expression match and a longer match.
3.^~ Only this rule is matched, and nginx stops searching for other matches. Otherwise, nginx will continue to process other location instructions.
4. Finally, the instructions with "~" and "~*" are matched. If a corresponding match is found, nginx stops searching for other matches. When there is no regular expression or no regular expression is matched, the verbatim match instruction with the highest degree of match will be used.

2. Proxy_pass in multiple ifs in nginx:

server {
listen 127.0.0.1:80;
    set $test A; 
  
    set $testB B;
    location / {
      if ($test ~* "A") { 
         proxy_pass http://www.so.com; 
         break; 
      } 
      if ($testB ~* "B") { 
         proxy_pass http://www.sogou.com; 
         #break; 
      } 
    }
}

I hope to use a certain proxy_pass when a certain condition is met. However, if multiple if statements are met, such as in the above example: when there is no break in the first if statement, the following will be executed; in order to execute proxy_pass after the first match, a break can be added. (There seems to be no such instruction as if else in nginx)

3. Determine the parameters for different proxy_pass:

Rewrite can only match by URL path, not by parameters, so if you want to implement parameter judgment, you need to use $arg_parameter.

location / {
    root html;
    index index.html index.htm index.php;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

    if ( $query_string ~* "usg=0" ) {
      proxy_pass http://local_workera;
    }

    if ( $query_string ~* "usg=1" ) {
      proxy_pass http://local_workerb;
    }

    if ( $arg_uid ~* "(.*[AB]$)" ) {
  proxy_pass http://local_workerf;
    }

    proxy_pass http://local_workera;
}

1) The two parameters usg=0 and usg=1 in the request path are fixed, so $query_string is used for regular matching; ($query_string's value is all the parameters in the request)

2) Next, if we want to transfer the request whose uid value ends with A or B to local_workerf for processing, we cannot use $query_string for regular matching at this time (because it is not easy to match /?uid=1A&t=1&usg=1 and /?uid=123&t=A&usg=0). At this time, we can only use $arg_uid for regular matching.

3) Since the two parameters usg=0 and usg=2 are mutually exclusive, according to the logic of the if instruction in the location above, they can be correctly processed without break and placed at the top. For uid matching, since it will conflict with usg, it can only be placed at the bottom or add a break, that is:

location / {
    root html;
    index index.html index.htm index.php;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
 
 if ( $arg_uid ~* "(.*[AB]$)" ) {
  proxy_pass http://local_workerf;
  break;
    }

    if ( $query_string ~* "usg=0" ) {
      proxy_pass http://local_workera;
    }

    if ( $query_string ~* "usg=1" ) {
      proxy_pass http://local_workerb;
    }

    proxy_pass http://local_workera;
}

This is the end of this article about the proxy_pass method in multiple ifs in nginx location. For more relevant nginx location proxy_pass content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of various usages of proxy_pass in nginx
  • Summary of Nginx location and proxy_pass path configuration issues
  • Detailed explanation of the difference between url ending with / and without / in nginx proxy_pass configuration
  • Implementation of proxy_pass in nginx reverse proxy
  • Differences between proxy_pass in two modules in nginx

<<:  JavaScript singleton mode to implement custom pop-up box

>>:  Solution to IDEA not being able to connect to MySQL port number occupation

Recommend

How to solve the problem of too many open files in Linux

The cause is that the process opens a number of f...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...

CSS to implement sprites and font icons

Sprites: In the past, each image resource was an ...

How to directly reference vue and element-ui in html

The code looks like this: <!DOCTYPE html> &...

How to set a fixed IP in Linux (tested and effective)

First, open the virtual machine Open xshell5 to c...

Docker installs the official Redis image and enables password authentication

Reference: Docker official redis documentation 1....

Use of Linux gzip command

1. Command Introduction The gzip (GNU zip) comman...

js array entries() Get iteration method

Table of contents 1. Detailed syntax of entires()...

js tag syntax usage details

Table of contents 1. Introduction to label statem...

Detailed explanation of viewing and setting file permissions on Mac

Preface To modify file permissions in the termina...

Summary of JS tips for creating or filling arrays of arbitrary length

Table of contents Preface Direct filling method f...

CentOS7.5 installation of MySQL8.0.19 tutorial detailed instructions

1. Introduction This article does not have screen...

Detailed explanation of Vue's caching method example

Recently, a new requirement "front-end cache...

W3C Tutorial (1): Understanding W3C

W3C, an organization founded in 1994, aims to unl...