Introduction to the use of Nginx location syntax _nginx

Source: Internet
Author: User

Nginx Location Introduction

The Location instruction in Nginx is an important instruction in Nginxhttpcoremodule. The Location directive is used to configure the matching URI, which is the "/uri/" in the syntax, and can be a string or regular expression. However, if you want to use regular expressions, you must specify a prefix.

Nginx Location Grammar

Basic syntax: location [=|~|~*|^~]/uri/{...}

= Strict match. If the query matches, the search is stopped and the request is processed immediately.
~ to match case sensitivity (regular expressions are available)
~* matches case-insensitive (available regular expressions)
!~ and!~* are case insensitive and case-insensitive mismatches, respectively.
^~ If you use this prefix for a regular string, tell Nginx to not test the regular expression if the path matches.

Nginx Location Application Example

Location =/{
	# Match/query only.
	
}
Location/{
	# matches any query because all requests have been/started. However, regular expression rules and long block rules will be matched by precedence and query.
	
}
Location ^~/images/{
	# matches any query that has a/images/start and stops the search. Any regular expressions will not be tested.
	
}
Location ~* \. (gif|jpg|jpeg) $ {
	# matches any requested end of GIF, JPG, or JPEG.
	
}
Location ~* \. (gif|jpg|swf) $ {
 Valid_referers none blocked start.igrow.cn sta.igrow.cn;
 if ($invalid _referer) {
 #防盗链
 rewrite ^/http://$host/logo.png;
 }
}
 
Location ~* \. (js|css|jpg|jpeg|gif|png|swf) $ {
if (f $request _filename) {
  #根据文件类型设置过期时间
  expires  1h;
  break;
}
}
 
Location ~* \. (txt|doc) ${ 
	#禁止访问某个目录
  root/data/www/wwwroot/linuxtone/test;
  Deny all;
}

The following are supplementary:

Nginx Location Basic Grammar

Location

syntax:location [=|~|~*|^~]/uri/{...}
Syntax: Location [=|~|~*|^~]/uri/{...}

Default:no
Default: No

Context:server
Context: Server

The

This directive allows different configurations depending on the URI. It can be configured using both conventional strings and regular expressions. To use regular expressions, your must use the prefix ~* for case insensitive match and ~ for case sensitive match.
This instruction accepts different structures depending on the URL. You can configure the use of normal strings and regular expressions. If you use regular expressions, you must use the ~* prefix to select a case-insensitive match or to select a case-sensitive match.

To determine which location directive matches a particular query, the conventional strings-are checked. Conventional strings match the beginning portion of the query and are case-sensitive-the most specific match would be use D (below on how to Nginx determines this). Afterwards, regular expressions are checked in the defined file. The regular expression to match the query would stop the search. If no regular expression matches are found, the result from the Convention string search is used. The
determines which location directive matches a specific instruction, and the general string is the first Test. The general string matches the start of the request and is case-sensitive, and the most explicit match will be used (see below to see how nginx determines it). The regular expressions are then tested in the order of the configuration file. A regular expression that finds the first Bibi stops the search. If no matching regular expression is found, the result of the regular string is used.

There are two ways to modify this behavior. The prefix "=", which matches an exact query. If the query matches, then searching stops and the request is handled immediately. For example, if the request "/" occurs frequently, then using "location =/" would expedite the processing of this request.
There are two ways to modify this behavior. The first method is to use the "=" prefix, which will only perform a strict match. If the query matches, the search is stopped and the request is processed immediately. Example: If a "/" request occurs frequently, using "location =/" will expedite the processing of this request.

The second is to use the prefix ^~. This prefix was used with a conventional string and tells Nginx to don't check regular expressions if the path provided is a Match. For instance, "location ^~/images/" would halt searching if the query begins with/images/-all regular Expression Direc Tives would not to be checked.
The second is to use the ^~ prefix. If you use this prefix for a regular string then tell Nginx if the path matches then do not test the regular expression.

Furthermore it is important to know this NGINX does the comparison not URL-encoded, so if you have a URL like "/images/%20" /test ' then use '/images//test ' to determine the location.
And it's important that NGINX do comparisons without URL coding, so if you have a URL link '/images/%20/test ', then use "images//test" to qualify location.

To summarize, the order in which directives are checked is as follows:
Summary, instructions are accepted in the following order:

1. directives with the = prefix that match the query exactly. If found, searching stops.
1. = The instructions for the prefix strictly match this query. If found, stop the search.
2. All remaining directives with conventional strings, longest match a. If This match used the ^~ prefix, searching stops.
2. The remaining regular string, long in front. If this match uses the ^~ prefix, the search stops.
3. Regular expressions, in order of definition in the configuration file.
3. Regular expressions, according to the order in the configuration file.
4. If #3 yielded a match, which is used. Else the match from #2 is used.
4. Use this result if the third step produces a match. Otherwise, the result of the second step is used.

Example:
Example:

Location =/{
# matches the query/only.
# only match/query.
[Configuration A]
}
Location/{
# matches any query, since all queries begin with/, but regular
# expressions and any longer conventional blocks would be
# matched.
# matches any query because all requests have been/started. However, regular expression rules and long block rules will be matched by precedence and query.
[Configuration B]
}
Location ^~/images/{
# matches any query beginning With/images/and halts searching,
# so regular expressions won't be checked.
# matches any queries that have been/images/and stops searching. Any regular expressions will not be tested.
[Configuration C]
}
Location ~* ". (Gif|jpg|jpeg) $ {
# matches any request ending in gif, JPG, or JPEG. However, all
# requests to the/images/directory'll be handled by
# Configuration C.
# matches any requests that have an end of GIF, JPG, or JPEG. However, requests for all/images/directories will use Configuration C.
[Configuration D]
}

Example requests:
Example Request:

*

/-> Configuration A
*

/documents/document.html-> Configuration B
*

/images/1.gif-> Configuration C
*

/documents/1.jpg-> Configuration D

Note this to you could define these 4 configurations of the order and the results would remain the same.
Note: Defining these 4 configurations in any order will still be the same.

Nginx Location syntax, with simple configuration

Introduction Nginx is a very lightweight HTTP server written by the Russians, Nginx, which is pronounced "engine X", is a high-performance http and reverse proxy server, but also a IMAP/POP3/SMTP proxy server.
Second, location grammatical grammar: location [=|~|~*|^~]/uri/{...}
Note:
1, ~ to match case matching
2, ~* for case-insensitive matching
3,!~ and!~* respectively for case-sensitive mismatches and case-insensitive mismatches

Example one:

Location/{}
Matches any query because all requests start with/. However, regular expression rules are matched by precedence and query.

Example two:

Location =/{}
Match only/

Example three:

Location ~* \. (Gif|jpg|jpeg) $ {
Rewrite \. (gif|jpg) $/logo.png;

Note: Case-insensitive matches any file ending with Gif,jpg,jpeg

Third, rewrite grammar

Last-basically all with this flag.
Break-Abort Rewirte, no continue match
Redirect-returns the temporarily redirected HTTP status 302
Permanent-Returns HTTP status for permanent redirection 301
1. The following are the expressions that you can use to determine:
-F and!-f are used to determine whether a file exists
-D and!-d used to determine whether a directory exists
-E and!-e used to determine whether a file or directory exists
-X and!-x are used to determine whether a file is executable
2, the following is a global variable that can be used as a judgment

Example: http://localhost:88/test1/test2/test.php

Copy Code code as follows:

$host: localhost
$server _port:88
$request _uri:http://localhost:88/test1/test2/test.php
$document _uri:/test1/test2/test.php
$document _root:d:\nginx/html
$request _filename:d:\nginx/html/test1/test2/test.php

Four, redirect grammar

Copy Code code as follows:

server {
Listen 80;
server_name start.igrow.cn;
Index index.html index.php;
root HTML;
if ($http _host!~ "^star\.igrow\.cn$" {
Rewrite ^ (. *) http://star.igrow.cn$1 redirect;
}
}

Five, anti-theft chain

Copy Code code as follows:

Location ~* \. (gif|jpg|swf) $ {
Valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid _referer) {
Rewrite ^/http://$host/logo.png;
}
}

Set expiration time based on file type

Copy Code code as follows:

Location ~* \. (js|css|jpg|jpeg|gif|png|swf) $ {
if (-f $request _filename) {
Expires 1h;
Break
}
}

Seven, prohibit access to a directory

Copy Code code as follows:

Location ~* \. (Txt|doc) ${
Root/data/www/wwwroot/linuxtone/test;
Deny all;
}

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.