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

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

Detailed explanation of the difference between tags and elements in HTML

I believe that many friends who are new to web pag...

Steps to customize icon in Vue

ant-design-vue customizes the use of Ali iconfont...

Use of vuex namespace

Table of contents Since Vuex uses a single state ...

Vue routing returns the operation method of restoring page status

Route parameters, route navigation guards: retain...

Introduction to 10 online development tools for web design

1. Online Text Generator BlindTextGenerator: For ...

Implementation of Docker deployment of SQL Server 2019 Always On cluster

Table of contents Docker deployment Always on clu...

Docker container exits after running (how to keep running)

Phenomenon Start the Docker container docker run ...

The meaning and calculation method of QPS and TPS of MySQL database

When doing DB benchmark testing, qps and tps are ...

A brief introduction to React

Table of contents 1. CDN introduction 1.1 react (...

HTML table tag tutorial (44): table header tag

<br />In order to clearly distinguish the ta...

The image element img has extra blank space in IE6

When doing DIV+CSS layout of the page, it is very...