1、location 区段
-
location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
-
location 是有顺序的,会被第一个匹配的location 处理,这里的顺序不是写在配置文件中的上下顺序,而是匹配规则顺序(nginx内部规定的)。
-
基本语法如下:
location [=|~|~*|^~|@] pattern{……}
2、location 前缀含义
= 表示精确匹配,优先级也是最高的( 必须一模一样才能匹配到)
^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
!~ 表示区分大小写不匹配的正则
!~* 表示不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到
@ 内部服务跳转
查找顺序和优先级
= 大于 ^~ 大于 ~|~*|!~|!~* 大于 /
多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
3、location 配置示例
1、没有修饰符(通用匹配) 表示:必须以指定模式开始
[root@fuzaijuheng nginx]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location /abc {
root /home/www/nginx; #必须在这个目录下创建/abc
index 1.html; #/home/www/nginx/abc
}
那么,如下是对的:
http://192.168.1.9/abc
[root@fuzaijuheng nginx]# nginx -s reload
[root@fuzaijuheng nginx]# mkdir /home/www/nginx/abc -p
[root@fuzaijuheng nginx]# vim /etc/nginx/nginx.conf
[root@fuzaijuheng nginx]# vim /home/www//nginx/abc/1.html
[root@fuzaijuheng nginx]# cat /home/www//nginx/abc/1.html
abc
[root@fuzaijuheng nginx]# curl 192.168.122.37/abc/1.html
abc
2、=表示:必须与指定的模式精确匹配
[root@fuzaijuheng nginx]# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/http_access.log main;
location / {
root /usr/share/nginx/html;
index a.html index.htm;
}
location = / {
root /usr/share/nginx/html;
index b.html index.htm;
}
}
[root@fuzaijuheng nginx]# nginx -s reload
[root@fuzaijuheng nginx]# mkdir -p /usr/share/nginx/html
[root@fuzaijuheng nginx]# vim /usr/share/nginx/html/a.html
[root@fuzaijuheng nginx]# cat /usr/share/nginx/html/a.html
a
[root@fuzaijuheng nginx]# vim /usr/share/nginx/html/b.html
[root@fuzaijuheng nginx]# cat /usr/share/nginx/html/b.html
b
>测试:
http://192.168.1.9 #会先匹配到b.html,应为=优先级最高
=/
http://192.168.1.9/a.html
/
[root@fuzaijuheng nginx]# curl 192.168.122.37
b
3、~ 表示:指定的正则表达式要区分大小写
server {
listen 80;
server_name localhost;
location ~ /abc {
root /home/www/nginx;
index 1.html index.htm;
}
}
测试访问:
http://192.168.122.37/abc
不正确的
http://192.168.122.37/ABC
[root@fuzaijuheng abc]# vim /etc/nginx/nginx.conf
[root@fuzaijuheng abc]# nginx -s reload
[root@fuzaijuheng abc]# curl 192.168.122.37/abc
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@fuzaijuheng abc]# curl 192.168.122.37/abc/1.html
abc
[root@fuzaijuheng abc]# curl 192.168.122.37/ABC/1.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
========================================
如果将配置文件修改为
[root@fuzaijuheng abc]# vim /etc/nginx/nginx.conf
server {
listen 80;
location ~ /ABC {
root /home/www/nginx;
index 1.html index.htm;
}
}
访问:
http://192.168.122.37/ABC/
[root@fuzaijuheng abc]# nginx -s reload
在创建目录和文件:
[root@fuzaijuheng abc]# cd /home/www/nginx/
[root@fuzaijuheng nginx]# mkdir ABC
[root@fuzaijuheng nginx]# vim ABC/1.html
[root@fuzaijuheng nginx]# curl 192.168.122.37/ABC/1.html
ABC
[root@fuzaijuheng nginx]# curl 192.168.122.37/abc/1.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
结论:~ 需要区分大小写。而且目录需要根据大小写定义。
4、^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。
5、@ :定义命名 location 区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如error_page等