Nginx configuration does not support the logic and & logic of the if condition or | operation, and does not support the nested syntax of the if condition. Otherwise, the following error is reported: nginx: [emerg] invalid condition.
We can indirectly implement it using variables.
Statement to be implemented:
The code is as follows: |
Copy code |
If ($ arg_unitid = 42012 & $ uri ~ /Thumb /){ Echo "www.111cn.net "; } |
If you configure it as follows, the error nginx: [emerg] invalid condition will be reported.
This can be achieved as follows:
The code is as follows: |
Copy code |
Set $ flag 0; If ($ uri ~ ^/Thumb/%0-9%%_160.jpg $ ){ Set $ flag "$ {flag} 1 "; } If ($ arg_unitid = 42012 ){ Set $ flag "$ {flag} 1 "; } If ($ flag = "011 "){ Echo "www.111cn.net "; } |
For example, nginx implements multi-if judgment
The code is as follows: |
Copy code |
# Statements to be implemented If ($ remote_addr ~ "^ (12.34 | 56.78)" & $ http_user_agent ~ * "Spider "){ Return 403; } # This is equivalent and truly available configuration Set $ flag 0; If ($ remote_addr ~ "^ (12.34 | 56.78 )"){ Set $ flag "$ {flag} 1 "; } If ($ http_user_agent ~ * "Spider "){ Set $ flag "$ {flag} 2 "; } If ($ flag = "012 "){ Return 403; }
|
Example
The code is as follows: |
Copy code |
If ($ http_user_agent ~ MSIE ){ Rewrite ^ (. *) $/msie/$1 break; } // If UA contains "MSIE", rewrite requests to the/msie Directory If ($ http_cookie ~ * "Id = ([^;] + )(? :; | $ )"){ Set $ id $1; } // If the cookie matches the regular expression, set the variable $ id to be equal to the regular expression reference part. If ($ request_method = POST ){ Return 405; } // If the submission Method is POST, 405 (Method not allowed) is returned) If (! -F $ request_filename ){ Break; Proxy_pass http: // 127.0.0.1; } // If the request file name does not exist, reverse proxy localhost If ($ args ~ Post = 140 ){ Rewrite ^ http://example.com/permanent; } // If the query string contains "post = 140 & Prime;", it is permanently redirected to example.com. |