1. First, let's review the relevant knowledge about location in nginx 1) Location matching instructions:
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. 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:
|
<<: JavaScript singleton mode to implement custom pop-up box
>>: Solution to IDEA not being able to connect to MySQL port number occupation
Notes on installing MySQL database, share with ev...
Table of contents Preface Six features of JSON.st...
1. Install vsftpd component Installation command:...
Table of contents 1. Array.at() 2. Array.copyWith...
I believe that many friends who are new to web pag...
ant-design-vue customizes the use of Ali iconfont...
Table of contents Since Vuex uses a single state ...
Route parameters, route navigation guards: retain...
1. Online Text Generator BlindTextGenerator: For ...
Table of contents Docker deployment Always on clu...
Phenomenon Start the Docker container docker run ...
When doing DB benchmark testing, qps and tps are ...
Table of contents 1. CDN introduction 1.1 react (...
<br />In order to clearly distinguish the ta...
When doing DIV+CSS layout of the page, it is very...