Home > Backend Development > PHP Tutorial > nginx location configuration instructions

nginx location configuration instructions

WBOY
Release: 2016-07-29 08:33:11
Original
1186 people have browsed it

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

nginx location syntax

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

= strict match. If this query matches, the search is stopped and the request is processed immediately.
~ is case-sensitive matching (regular expressions are available)
~* is case-insensitive matching (regular expressions are available)
!~ and !~* are case-sensitive mismatching and case-insensitive mismatching respectively
^~ If used with a regular string, this tells nginx not to test the regular expression if the path matches.

Location syntax syntax: location [=|~|~*|^~] /uri/ { … }
Note:
1, ~ are case-sensitive matching
2, ~* are case-insensitive matching
3, !~ and !~* are case-sensitive mismatch and case-insensitive mismatch respectively.

Example 1:

location / { }

matches any query because all requests start with /. But regular expression rules will be prioritized for query matching.

Example 2:

The code is as follows Copy code
location =/ {}

Only matches /

Example 3:

Copy code
location ~* .(gif|jpg|jpeg)$ {
rewrite .(gif|jpg)$ /logo.png;

Note: Matches anything starting with gif, jpg, The file ending in jpeg


nginx location application example

The code is as follows Copy the code

location = / {
# Only matches / query.

}location / {
# Matches any query since all requests start with /. But regular expression rules and long block rules will be prioritized and matched against the query.

}location ^~ /images/ {
# Match any query starting with /images/ and stop searching. Any regular expression will not be tested.

}location ~* .(gif|jpg|jpeg)$ {
# Match any request that ends with gif, jpg or jpeg.

}location ~* .(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
#Anti-hotlinks
rewrite ^/ http:// $host/logo.png;
}
}
location ~* .(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
#Set expiration time based on file type
expires 1h;
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


The above introduces the nginx location configuration instructions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template