Nginx核心配置-location的匹配案例实战篇

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.location语法规则介绍

  在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是用户请求的字符串,即域名后面的web文件路径,然后使用location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。

  语法规则: location [=|~|~*|^~] /uri/ { … }
    =:
      用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止向下匹配并立即处理请求。
    ~:
      用于标准uri前,表示包含正则表达式并且区分大小写
    ~*:
      用于标准uri前,表示包含正则表达式并且不区分大写
    !~:
      用于标准uri前,表示包含正则表达式并且区分大小写不匹配
    !~*:
      用于标准uri前,表示包含正则表达式并且不区分大小写不匹配
    ^~:
      用于标准uri前,表示包含正则表达式并且匹配以什么开头
    $:
      用于标准uri前,表示包含正则表达式并且匹配以什么结尾
    \:
      用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等
    *:
      用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符

二.匹配案例-精确匹配

1>.编辑主配置文件(本篇博客试验过程并不修改主配置文件内容哟)

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
keepalive_timeout 65 65; #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/location.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/image;
index index.html;
} location /01.jpg {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

3>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# ll
total 364
-rw-r--r-- 1 root root 248743 Dec 15 22:38 01人柱力.jpg
-rw-r--r-- 1 root root 122026 Dec 16 2019 02.迪丽热巴.jfif
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp 01人柱力.jpg /yinzhengjie/data/web/nginx/html/image/01.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp 02.迪丽热巴.jfif /yinzhengjie/data/web/nginx/html/01.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -R /yinzhengjie/data/web/nginx/html/
/yinzhengjie/data/web/nginx/html/:
total 124
-rw-r--r-- 1 root root 122026 Dec 16 19:35 01.jpg
drwxr-xr-x 2 root root 38 Dec 16 19:35 image
-rw-r--r-- 1 root root 88 Dec 15 23:13 index.html /yinzhengjie/data/web/nginx/html/image:
total 248
-rw-r--r-- 1 root root 248743 Dec 16 19:35 01.jpg
-rw-r--r-- 1 root root 49 Dec 15 23:07 index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>.启动nginx服务

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

5>.浏览器访问"http://node101.yinzhengjie.org.cn/01.jpg",如下图所示,它并没有显示火影忍者关于人柱力的图片,而是显示了精确匹配的图片,因此我们得出精确匹配优先级高的结论。

三.匹配案例-区分大小写

1>.编写子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/location.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/image;
index index.html;
} location ~ /F.?\.JPG {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# cd /yinzhengjie/data/web/nginx/html/
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]# ll
total 124
-rw-r--r-- 1 root root 122026 Dec 16 19:35 01.jpg
drwxr-xr-x 2 root root 38 Dec 16 19:35 image
-rw-r--r-- 1 root root 88 Dec 15 23:13 index.html
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]# cp 01.jpg Fg.JPG
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]# cp 01.jpg Fg.jpg
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]# ll
total 364
-rw-r--r-- 1 root root 122026 Dec 16 19:35 01.jpg
-rw-r--r-- 1 root root 122026 Dec 16 20:10 Fg.jpg
-rw-r--r-- 1 root root 122026 Dec 16 20:03 Fg.JPG
drwxr-xr-x 2 root root 38 Dec 16 19:35 image
-rw-r--r-- 1 root root 88 Dec 15 23:13 index.html
[root@node101.yinzhengjie.org.cn /yinzhengjie/data/web/nginx/html]#

3>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 4785 1 0 19:36 ? 00:00:00 nginx: master process nginx
nginx 4953 4785 0 20:05 ? 00:00:00 nginx: worker process
nginx 4954 4785 0 20:05 ? 00:00:00 nginx: worker process
nginx 4955 4785 0 20:05 ? 00:00:00 nginx: worker process
nginx 4956 4785 0 20:05 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 4785 1 0 19:36 ? 00:00:00 nginx: master process nginx
nginx 4953 4785 0 20:05 ? 00:00:00 nginx: worker process is shutting down
nginx 4966 4785 3 20:05 ? 00:00:00 nginx: worker process
nginx 4967 4785 2 20:05 ? 00:00:00 nginx: worker process
nginx 4968 4785 3 20:05 ? 00:00:00 nginx: worker process
nginx 4969 4785 2 20:05 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>.浏览器输入"http://node101.yinzhengjie.org.cn/Fg.jpg"访问小写字母的url,访问不到,因为"jpg"是小写字母,和咱们配置的nginx规则符合,尽管服务器路径下的确存在该文件依旧是无法访问。

5>.浏览器可以成功访问"http://node101.yinzhengjie.org.cn/Fg.JPG",因为我们写的"JPG"是大写字母,和咱们配置的nginx规则符合

四.匹配案例-不区分大小写

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/location.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/image;
index index.html;
} #区分大小写
#location ~ /F.?\.JPG {
# root /yinzhengjie/data/web/nginx/html;
# index index.html;
#} #不区分大小写
location ~* /F.?\.JPG {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.重新加载nginx服务

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.浏览器输入"http://node101.yinzhengjie.org.cn/Fg.jpg"访问小写字母的url,如下图所示,可以访问到了,因为咱们没有区分URI的大小写

4>.浏览器可以成功访问"http://node101.yinzhengjie.org.cn/Fg.JPG",照样是可以正常访问的

五.匹配案例-URI开始

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/location.conf
server {
listen ;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html;
index index.html;
} location ^~ /static {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : 01人柱力.jpg
-rw-r--r-- root root Dec : .迪丽热巴.jfif
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/data/web/nginx/html/static
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp 01人柱力.jpg /yinzhengjie/data/web/nginx/html/static/.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp .迪丽热巴.jfif /yinzhengjie/data/web/nginx/html/static/.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/html/static/
total
-rw-r--r-- root root Dec : .jpg
-rw-r--r-- root root Dec : .jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>.如下图所示,浏览器访问“http://node101.yinzhengjie.org.cn/static/01.jpg”,可以访问成功

5>.如下图所示,浏览器访问“http://node101.yinzhengjie.org.cn/static/02.jpg”,可以访问成功

六.匹配案例-文件名后缀

1>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/location.conf
server {
listen ;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html;
index index.html;
} location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#

2>.创建测试数据

[root@node101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/data/web/nginx/static
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp /etc/passwd /yinzhengjie/data/web/nginx/static/passwd.js
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : 01人柱力.jpg
-rw-r--r-- root root Dec : .迪丽热巴.jfif
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp 01人柱力.jpg /yinzhengjie/data/web/nginx/static/.jpg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp .迪丽热巴.jfif /yinzhengjie/data/web/nginx/static/.jpeg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total
-rw-r--r-- root root Dec : .jpg
-rw-r--r-- root root Dec : .jpeg
-rw-r--r-- root root Dec : passwd.js
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root : ? :: nginx: master process nginx
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
nginx : ? :: nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>.客户端测试访问资源

  浏览器访问:http://node101.yinzhengjie.org.cn/01.jpg,如下图所示,访问成功啦

  浏览器访问:http://node101.yinzhengjie.org.cn/02.jpeg,如下图所示,范围成功啦

  浏览器访问:http://node101.yinzhengjie.org.cn/passwd.js,如下图所示,访问成功啦。

七.匹配案例-优先级

匹配优先级:
  =, ^~, ~/~*,/

location优先级:
  (location =) > (location 完整路径) > (location ^~ 路径) > (location~,~* 正则顺序) > (location 部分起始路径) > (/)

八.生产使用案例

#直接匹配网站根会加速Nginx访问处理:
location = / {
  ......;
}
location / {
  ......;
}
#静态资源配置:
location ^~ /static/ {
  ......;
}
# 或者
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
  ......;
}
#多应用配置
location ~* /app1 {
  ......;
}
location ~* /app2 {
  ......;
}

最新文章

  1. Logging configuration
  2. 在Debian下编译Postgresql
  3. 【C#】线程协作式取消
  4. css文件内引用外部资源文件的相对路径
  5. C/C++中堆与栈
  6. vi/vim使用指北 ---- Moving Around in a Hurry
  7. Partitioning by Palindromes
  8. 转载C#泛型集合—Dictionary<K,V>使用技巧
  9. 启动tomcat时报classpath not found
  10. Visual Studio express
  11. Dnasp计算LD
  12. CodeForces 484A Bits
  13. MSSQL-并发控制-1-Transaction
  14. 微信跳一跳的mini辅助设计
  15. jenkins配置findbugs失败---不要随便忽略警告!一个因为文件所有权引发的血案
  16. python 排序算法
  17. 在myeclipse中使用./和../遇到的问题
  18. 实用的DDos攻击工具
  19. 设计模式学习--面向对象的5条设计原则之开放封闭原则--OCP
  20. Python基础语法学习整理

热门文章

  1. pinpoint 安装指南
  2. stringstream字符串流的妙用
  3. 远程文件传输命令•RHEL8/CentOS8文件上传下载-用例
  4. K8S 如何实现将git代码下拉到指定的容器路径中
  5. SWIG 3 中文手册——4. 脚本语言
  6. 【layui】设置select只向下展开
  7. springmvc项目转为springboot
  8. 理解 Virtual DOM(摘)及评价
  9. 单片机成长之路(51基础篇) - 023 N76e003 系统时钟切换到外部时钟
  10. LinkedHashMap 的核心就 2 点,搞清楚,也就掌握了