架构师成长记_第四周_06_Nginx配置文件之location的匹配解析

本文详细介绍了Nginx配置中location指令的匹配规则,包括默认匹配、精确匹配和正则匹配。通过实例展示了如何配置location来指定静态文件路径、处理特定URL请求。了解这些匹配方式对于优化Nginx服务器的性能和管理Web应用至关重要。
摘要由CSDN通过智能技术生成

Nginx配置文件之location的匹配解析

    server {
        listen       89;
        server_name  localhost;
		
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
	
	    server {
        listen       90;
        server_name  localhost;
		
        location / {
            root   /home/foodie-shop;
            index  index.html;
        }
    }

默认的匹配 “/

 server {
        listen       89;
        server_name  localhost;
		
		# 默认匹配, 会匹配到 root 指定的路径
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

在这里插入图片描述

精确的匹配, 使用=

资源路径: /home/beyond/img/face1.png
在这里插入图片描述

 server {
        listen       91;
        server_name  localhost;
		
		# 精确匹配, 会匹配到 root 指定的路径
        location = / {
            root   html;
            index  index.html index.htm;
        }
        # 精确匹配, 只能精准的匹配到 /beyond/img/face1.png
        location = /beyond/img/face1.png {
        root   /home;
        }
    }

使用正则匹配 ~* 正则^~ 以某个路径开头

* 表示不区分大小写

 server {
        listen       91;
        server_name  localhost;
		
		# 正则匹配, 会匹配到 root 指定的路径
        location ~* \.(GIF|png|bmp|jpg|jpeg) {
            root   /home;
        }
    }
 server {
        listen       91;
        server_name  localhost;
		
		# 非正则匹配, 会匹配到 root 指定的路径下的以/beyond/img/路径开头的所有文件
        location ^~ /beyond/img {
            root   /home;
        }
    }

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_大木_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值