String expressions support regular expressions and can be used to determine whether the case is sensitive. Therefore, there are four operators,
As follows:
~ Case sensitive (case sensitive) matching successful
~ * Case-insensitive matching is successful.
!~ Case-sensitive matching failed
!~ * Case-insensitive matching failed
1: restrict access to some types of clients
If Command
All Nginx built-in variables can be matched using the if command and regular expression, and some operations are performed based on the matching results.
| The code is as follows: |
Copy code |
Location /{ If ($ http_user_agent ~ MSIE ){ Return 503; } } |
# Restrict IE access
If you change MSIE to Mozilla, pc browsers like IE and firefox are basically restricted.
2 and 3 mainly deal with leeching
2: for different file types
Maybe this command is the most commonly used command for regular expression matching:
| The code is as follows: |
Copy code |
Location ~ . *. (Wma | wmv | asf | mp3 | mmf | zip | rar | jpg | gif | png | swf | flv) $ { If ($ http_referer ~ * Javagg.com ){ # Rewrite ^/http://www.javagg.com/403.html; Return 403; } }
|
3: for different directories
| The code is as follows: |
Copy code |
Location/img /{ Root/data/img /; If ($ http_referer ~ * Javagg.com ){ Rewrite ^/yun_qi_img/error.gif # Return 403; } } |
1. ^ ~ The identifier is followed by a string.
Nginx will stop matching regular expressions after matching the string (matching results of regular expressions in the location command are preferred), such as location ^ ~ /Images/. You want to perform some special operations on the/images/directory, such as adding the expires header and anti-Leech, however, you want to add only the expires header to all images except the images in this directory. This operation may use another location, for example: location ~ *. (Gif | jpg | jpeg) $. In this case, if there is a request for/images/1.jpg, how does nginx decide which location to perform the operation? The result depends on the identifier ^ ~, If you write: location/images/, nginxwill match 1.jpg to location ~ *. (Gif | jpg | jpeg) $ in this location, this is not the result you need, but adds ^ ~ After the identifier matches the/images/string, it stops searching for other location with regular expressions.
2. = indicates the exact search address,
For example, location =/matches only requests whose uri is/. If the request is/index.html, another location will be searched, instead of matching this. Of course, you can write two locations, location =/And location/, so that/index.html will match the latter. If your site has a large number of requests to/, you can use this method to speed up the request response.
3. @ indicates to name a location, that is, to customize a location. This location cannot be accessed by the outside world and can only be used for subrequests generated by Nginx, mainly error_page and try_files.
Note: These three identifiers are not followed by regular expressions. Although the configuration file is checked and there is no warning, they do not match.
To sum up, the matching order of the post-par value of the location command is as follows:
1. The location of the identifier "=" is matched first. If the request uri matches the location, the request uses the location configuration.
2. Perform String Matching. If the matched location has ^ ~ This identifier. If it matches to stop, the configuration of this location will be returned.
3. Perform regular expression matching according to the sequence defined in the configuration file. The configuration in the oldest matched location will be returned.
4. If the regular expression can match the requested uri, the location corresponding to the regular expression is used. If no, the second matching result is used.