Nginx location
The Location command in Nginx is an important command in NginxHttpCoreModule. The Location command is used to configure the matched URI. URI is the "/uri/" in the syntax, which can be a string or regular expression. However, to use a regular expression, you must specify a prefix.
Nginx location syntax
Basic syntax: location [= | ~ | ~ * | ^ ~] /Uri /{... }
= Strict match. If the query matches, the search is stopped and the request is processed immediately.
~ Case-sensitive matching (regular expressions are available)
~ * Case-insensitive matching (regular expressions are available)
!~ And !~ * Case-insensitive and case-insensitive
^ ~ If this prefix is used for a regular string, it indicates that if the path matches, the regular expression is not tested.
Location syntax: location [= | ~ | ~ * | ^ ~] /Uri /{... }
Note:
1 ,~ Case-sensitive matching
2 ,~ * Case-insensitive match
3 ,!~ And !~ * Case-insensitive and case-insensitive
Example 1:
Location /{}
Match any query because all requests start. However, regular expression rules are preferentially matched with queries.
Example 2:
The code is as follows: |
Copy code |
Location = /{}
|
Match only/
Example 3:
The code is as follows: |
Copy code |
Location ~ *. (Gif | jpg | jpeg) $ { Rewrite. (gif | jpg) $/logo.png; }
|
Note: case-insensitive matching of any file ending with gif, jpg, and jpeg
Nginx location application instance
The code is as follows: |
Copy code |
Location = /{ # Match/query only. } Location /{ # Match any query because all requests start. However, regular expression rules and long block rules are preferentially matched with queries. } Location ^ ~ /Images /{ # Match any queries starting with/images/and stop searching. No regular expression will be tested. } Location ~ *. (Gif | jpg | jpeg) $ { # Match any request that has ended with gif, jpg, or jpeg. } Location ~ *. (Gif | jpg | swf) $ { Valid_referers none blocked start.igrow.cn sta.igrow.cn; If ($ invalid_referer ){ # Anti-Leech Rewrite ^/http: // $ host/logo.png; } } Location ~ *. (Js | css | jpg | jpeg | gif | png | swf) $ { If (-f $ request_filename ){ # Set the expiration time based on the file type Expires 1 h; Break; } } Location ~ *. (Txt | doc) $ { # Prohibit access to a directory Root/data/www/wwwroot/linuxtone/test; Deny all; } |
++ Some available global variables
$ Args
$ Content_length
$ Content_type
$ Document_root
$ Document_uri
$ Host
$ Http_user_agent
$ Http_cookie
$ Limit_rate
$ Request_body_file
$ Request_method
$ Remote_addr
$ Remote_port
$ Remote_user
$ Request_filename
$ Request_uri
$ Query_string
$ Scheme
$ Server_protocol
$ Server_addr
$ Server_name
$ Server_port
$ Uri