Home > Backend Development > PHP Tutorial > nginx location basic configuration and location best practices

nginx location basic configuration and location best practices

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-28 08:28:06
Original
923 people have browsed it

Redirect to http://www.tuicool.com/articles/Jr63qy

Grammar rules: location [=|~|~*|^~] /uri/ { … }
= The beginning means an exact match
^~ The beginning means uri Starting with a regular string, it can be understood as matching the url path. nginx does not encode the URL, so the request is /static/20%/aa, which can be matched by the rule ^~ /static/ /aa (note the space). The beginning of
~ indicates case-sensitive regular matching
~* The beginning indicates case-insensitive regular matching
!~ and !~* are case-sensitive non-matching and case-insensitive non-matching regular matching respectively
/ universal matching, Any request will be matched.
In the case of multiple location configurations, the matching order is (comes from reference materials, has not been actually verified, you will know after trying it, no need to be rigid, just for reference):
First match =, secondly match ^~, secondly by file Regular matching in order, and finally / universal matching. When a match is successful, the matching is stopped and the request is processed according to the current matching rules.
Example, there are the following matching rules:
location = / {
#Rule A
}
location = /login {
#Rule B
}
location ^~ /static/ {
#Rule C
}
location ~ .( gif|jpg|png|js|css)$ {
#RuleD
}
location ~* .png$ {
#RuleE
}
location !~ .xhtml$ {
#RuleF
}
location !~ * .xhtml$ {
#Rule G
}
location / {
#Rule H
}
Then the effect is as follows:
Access the root directory/, such as http://localhost/ will match rule A
Access http:/ /localhost/login will match rule B, http://localhost/register will match rule H
Visit http://localhost/static/a.html will match rule C
Visit http://localhost/a.gif, http ://localhost/b.jpg will match rule D and rule E, but rule D takes precedence and rule E has no effect, while http://localhost/static/c.png will match rule C first
Access http: //localhost/a.PNG will match rule E, but will not match rule D, because rule E is not case-sensitive.
Accessing http://localhost/a.xhtml will not match rule F and rule G. http://localhost/a.XHTML will not match rule G because it is not case-sensitive. Rule F and Rule G belong to the elimination method, which conforms to the matching rules but will not be matched, so think about where they will be used in actual applications.
Visit http://localhost/category/id/1111 and finally match rule H. Because none of the above rules match, nginx should forward the request to the back-end application server at this time, such as FastCGI (php), tomcat (jsp) , nginx exists as a direction proxy server.
Several rules commonly used in practical applications are as follows:
# Directly matches the website root. Accessing the website homepage through the domain name is more frequent. Using this will speed up the processing, the official website says.
#This is forwarded directly to the back-end application server, or it can be a static homepage
# The first required rule
location = / {
proxy_pass http://tomcat:8080/index
}

# The second The first required rule is to handle static file requests, which is the strength of nginx as an http server
# There are two configuration modes, directory matching or suffix matching, choose one or use them together
location ^~ /static/ {
# Request /static/a.txt will be mapped to the actual directory file: /webroot/res/static/a.txt
root /webroot/res/;
}
location ~* .(gif|jpg|jpeg|png|css| js|ico)$ {
root /webroot/res/;
}

#The third rule is the general rule, used to forward dynamic requests to the back-end application server
#Non-static file requests are dynamic requests by default. Based on actual grasp
#After all, with the popularity of some current frameworks, there are very few cases with .php, .jsp suffixes
location / {
proxy_pass http://tomcat:8080/
}
For the above basic recommended configuration, there is one In addition, there is one thing to note about forwarding. For example, the following configuration forwards a directory:
The key lies in the last /, when accessing localhost/outer/in.html, case A will be forwarded to tomcat:8080/in.html, and case B will be forwarded to tomcat:8080/outer /in.html, so be sure to pay attention.

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

Related labels:
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
Latest Issues
mysqldump without specifying location
From 1970-01-01 08:00:00
0
0
0
web - nginx location search algorithm problem! ?
From 1970-01-01 08:00:00
0
0
0
nginx location matching problem
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template