Location in nginx configuration file

Source: Internet
Author: User

Location

Syntax: Location [= | ~ | ~ * | ^ ~] /Uri /{... } Default: No

Context: Server

This command accepts different structures with different URLs. You can configure regular strings and regular expressions. If you use a regular expression, you must use ~ * The prefix is case-insensitive or ~ Select case-sensitive matching.

Determine which location command matches a specific command. The regular string is the first test. The starting part of a regular string matching request is case sensitive. The most explicit match will be used (see how nginx determines it ). Then, the regular expression is tested according to the order in the configuration file. Finding the regular expression for the first match will stop searching. If no matching regular expression is found, the regular string result is used.

There are two methods to modify this behavior. The first method is to use the "=" prefix and only perform strict matching. If the query matches, the search will be stopped and the request will be processed immediately. Example: If a "/" request occurs frequently, use "location =/" to accelerate the processing of the request.

The second is to use ^ ~ Prefix. If this prefix is used for a regular string, it indicates that if the path matches, the regular expression is not tested.

In addition, nginx does not have URL encoding. Therefore, if you have a URL link '/images/% 20/test', use "images/test" to limit the location.

In summary, commands are accepted in the following order: 1. = the prefix command strictly matches this query. If yes, stop searching. 2. The remaining regular strings are long in front. If this match is used ^ ~ Prefix. The search is stopped. 3. Regular expressions, in the order in the configuration file. 4. If the third step produces a match, use this result. Otherwise, use the matching result in step 2.

Example:

Location =/{# Only matches/queries. [Configuration a]} location/{# Matches any query because all requests start. However, regular expression rules and long block rules are preferentially matched with queries. [Configuration B]} location ^ ~ /Images/{# match any queries starting with/images/and stop searching. No regular expression will be tested. [Configuration C]} location ~ * \. (GIF | JPG | JPEG) $ {# match any request that has ended with GIF, JPG, or JPEG. However, configuration C is used for all/images/directory requests. [Configuration D]}

Example request:

/ -> configuration A/documents/document.html -> configuration B/images/1.gif -> configuration C/documents/1.jpg -> configuration D

Note: The results of defining the four configurations in any order will remain the same.

(Location =)> (full location path)> (location ^ ~ Path)> (location ~ *,~ Regular Expressions)> (location part start path) Regular Expressions affect Matching Based on the preference order in the configuration file. The preference matches the preference. Others match the preference based on the matching length.

Reference: http://wiki.nginx.org/NginxHttpCoreModule#location

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.