nginx 的try_files $uri $uri/ xxxxx用法

文章详细解释了Nginx配置中的try_files指令如何处理URL请求,当无法匹配预定义的目录结构时,会将请求导向到指定的默认页面。通过实例分析www.1.com配置,展示了try_files在实际应用中的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.try_files $uri $uri/ xxxxx理解

当访问的地址中不匹配$uri(用户访问地址)和$uri/(用户访问地址/下面)时候,会跳转到定义的最后的xxxx页面

2.案例

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf |grep vhost

include vhost/*.conf;

[root@localhost ~]# cat /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    location  /abc/ {

        root /data/wwwroot/www.1.com/;

        index index.html;

        try_files $uri $uri/ /abc/a/index.html;

        #当http://www.1.com/abc 和http://www.1.com/abc/下的路径匹配不上时候,如:http://www.1.com/abc/xxxxx,跳转到最后一个:/abc/a/index.html 

    }

}

[root@localhost ~]# ls /data/wwwroot/www.1.com/

abc

[root@localhost ~]# tree /data/wwwroot/www.1.com/abc/

/data/wwwroot/www.1.com/abc/

├── 1.html

├── 2.html

├── a

│   ├── a.html

│   └── index.html

├── b

│   ├── b.html

│   └── index.html

└── index.html

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/1.html

111

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/2.html

222

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/index.html

abc_index

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/a/a.html

aaa

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/a/index.html

a_index

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/b/b.html

bbb

[root@localhost ~]# cat /data/wwwroot/www.1.com/abc/b/index.html

b_index

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

当访问地址中匹配上前面两个定义的地址时候,则进行正常的访问,如下: $uri $uri/

 

 

 

 当访问地址中没有匹配上前面两个定义的地址时候,则进会跳转到最后定义的地址,如下: /abc/a/index.html

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

运维实战课程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值