Learning notes for nginx logical operations if ()

Source: Internet
Author: User
Tags regular expression

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.