【Nginx】Nginx中location的使用方法详解

前情提要:本篇博客介绍了nginx的核心配置模块中的location部分的详解,包括location的语法规则(= ^~ ~ ~* 不带符号 \)的功能、用法以及详细的示例。通过本篇博客可以加深对location的使用的理解。

一、location简介

1.1 location使用介绍

  • 在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;

  • ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri

  • uri是用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。

1.2 location语法规则

cpp 复制代码
# 语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }

=           #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求

^~          #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
			#对uri的最左边部分做匹配检查,不区分字符大小写
			
~           #用于标准uri前,表示包含正则表达式,并且区分大小写

~*          #用于标准uri前,表示包含正则表达式,并且不区分大写

不带符号	 #匹配起始于此uri的所有的uri

\            #用于标准uri前,表示包含正则表达式并且转义字符。可以将  . * ?等转义为普通符号

#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号

二、location后面不加修饰符示例

在server部分使用location配置一个web界面,例如:当访问nginx服务器的/logo.jpg的时候要显示指定 HTML文件的内容,精确匹配一般用于匹配组织的logo等相对固定的URL,匹配优先级最高

cpp 复制代码
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhost.conf 
server {
        listen 80;
        server_name www.doubledragon.org;
        location /null {					# 不带修饰符,代表匹配以/null开头的所有请求
                return 200 "/null-1";
        }
}  

# 访问测试
[root@Nginx ~]# curl www.doubledragon.org/null
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null/
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null/1
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null/2
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null1
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null12
/null-1
[root@Nginx ~]# curl www.doubledragon.org/null123
/null-1

# 修改配置文件
server {
        listen 80;
        server_name www.doubledragon.org;
        location /null {
                return 200 "/null-1";
        }

        location /blog/ {
                return 200 "/blog/";		# 不带修饰符,代表匹配/blog/开头的所有请求
        }       
}

# 重新测试
[root@Nginx ~]# curl www.doubledragon.org/blog/23
/blog/
[root@Nginx ~]# curl www.doubledragon.org/blog1/23
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/blog
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/blog/
/blog/

三、location后面加 = 示例

cpp 复制代码
# 编辑子配置文件
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhost.conf 
server {
        listen 80;
        server_name www.doubledragon.org;
        location = /login/ {
                return 200 "=/login/";		# 精确匹配/login/
        }

        location = /blog {
                return 200 "=/blog";		# 精确匹配/blog
        }
} 

[root@Nginx ~]# nginx -s reload

# 访问测试
[root@Nginx ~]# curl www.doubledragon.org/login/
=/login/
[root@Nginx ~]# curl www.doubledragon.org/login
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/login1/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/login/1
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>

[root@Nginx ~]# curl www.doubledragon.org/blog
=/blog
[root@Nginx ~]# curl www.doubledragon.org/blog/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/blog/1
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>
[root@Nginx ~]# curl www.doubledragon.org/blog1
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>

四、location后面加 ^~ 示例

cpp 复制代码
# 编辑子配置文件
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhost.conf 
server {
        listen 80;
        server_name www.doubledragon.org;

        location ^~ /doc {					# 匹配/doc开头的,若匹配成功则会阻止正则匹配
                return 200 "/doc";
        }

        location ~* ^/document$ {
                return 200 "/document";		# 正则匹配,精确匹配/document
        }
}

[root@Nginx ~]# nginx -s reload

# 访问测试
[root@Nginx ~]# curl www.doubledragon.org/doc
/doc
[root@Nginx ~]# curl www.doubledragon.org/document
/doc
[root@Nginx ~]# curl www.doubledragon.org/doc1
/doc
[root@Nginx ~]# curl www.doubledragon.org/doc/2
/doc
[root@Nginx ~]# curl www.doubledragon.org/do
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.2</center>
</body>
</html>

五、location后面加 ~ 示例

cpp 复制代码
# 编辑子配置文件
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhost.conf
server {
        listen 80;
        server_name www.doubledragon.org;
        
        location ~ ^/api {
                return 200 "^/api";		# 匹配以/api开头的,区分大小写
        }       

        location ~ \.php$ {
                return 200 ".php";		# 匹配以.php结尾的,区分大小写
        }       
}

[root@Nginx ~]# nginx -s reload

# 访问测试
[root@Nginx ~]# curl www.doubledragon.org/api
^/api
[root@Nginx ~]# curl www.doubledragon.org/apii
^/api
[root@Nginx ~]# curl www.doubledragon.org/api/1
^/api
[root@Nginx ~]# curl www.doubledragon.org/index.php
.php
[root@Nginx ~]# curl www.doubledragon.org/html/index.php
.php
[root@Nginx ~]# curl www.doubledragon.org/api.php
^/api

六、location后面加 ~* 示例

cpp 复制代码
# 编辑子配置文件
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhost.conf 

server {
        listen 80;
        server_name www.doubledragon.org;

        location ~* ^/api {
                return 200 "^/api";		# 匹配以/api开头的,不区分大小写
        }
}

[root@Nginx ~]# nginx -s reload

# 访问测试
[root@Nginx ~]# curl www.doubledragon.org/api
^/api
[root@Nginx ~]# curl www.doubledragon.org/API
^/api
[root@Nginx ~]# 

综上,以上就是关于location的使用的详细示例

相关推荐
乐维_lwops13 分钟前
2026年如何实现多分支机构全网设备统一运维监控一
运维·cmdb·运维监控·网管·agent ops
钛态23 分钟前
前端安全防线:CSRF 攻击链路与双重 Token 校验的工程实现
前端·vue·react·web
Cx330❀25 分钟前
【MySQL基础】一文吃透“表的约束”:从 Null/Default 到主外键的终极安全法则
linux·服务器·数据库·c++·mysql·安全
程序员JerrySUN38 分钟前
Jetson 刷机深度解析:flash.sh vs l4t_initrd_flash.sh(含安全与磁盘加密对比)
linux·网络·arm开发·安全·系统安全
视觉AI1 小时前
VS Code Remote-SSH 连接Jetson踩坑完整解决记录(网段不通+主机密钥变更双重故障)
运维·网络·人工智能·windows·ssh·边缘计算
银河麒麟操作系统1 小时前
闸机高效稳跑,通勤提速升级!银河麒麟赋能北京轨交新基建
运维·服务器·安全·交通物流
六点_dn2 小时前
Linux学习笔记-printf命令
linux·运维·算法
蜡台2 小时前
Linux Python 安装使用
linux·运维·服务器
欧神附体1232 小时前
在虚拟机中添加网卡,修改网卡名并关闭SElinux
linux·服务器·网络
张忠琳2 小时前
【kubernetes】Node Feature Discovery 0.20.0-devel 源码深度解析 — 第四卷:CLI入口、工具链与全局架构总结
云原生·容器·架构·kubernetes·nvidia