Nginx location syntax

Source: Internet
Author: User
Tags regular expression

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

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.