nginx访问目录是没加/的重定向控制
static 模块提供了root与alias功能;发现目标是目录时;但URI末尾未加/时;会返回301重定向;重定向后会加/
指令
1 2 3 4 5 6 7 8 9 | Syntax: server_name_in_redirect on | off; # 控制返回那个域名 Default: server_name_in_redirect off; Context: http, server, location Syntax: port_in_redirect on | off; #控制返回的端口 Default: port_in_redirect on; Context: http, server, location Syntax: absolute_redirect on | off; #返回重定向时时否添域名,默认是会添域名 Default: absolute_redirect on; Context: http, server, location |
配置
1 2 3 4 5 6 7 8 | [root@python vhast] # cat dir.conf server { server_name www. dir .com fwe. dir .com; #第一个为主域名;第二个为辅助域名 server_name_in_redirect off; listen 8088 ; absolute_redirect off; root html / ; } |
测试
1 2 3 4 5 6 7 8 | [root@python vhast] # curl www.dir.com:8088/first -I HTTP / 1.1 301 Moved Permanently Server: nginx / 1.15 . 9 Date: Wed, 10 Jul 2019 10 : 30 : 04 GMT Content - Type : text / html Content - Length: 169 Connection: keep - alive Location: / first / |
修改配置
1 2 3 4 5 6 7 | server { server_name www. dir .com fwe. dir .com; server_name_in_redirect off; listen 8088 ; #absolute_redirect off; root html / ; } |
测试
1 2 3 4 5 6 7 8 | [root@python vhast] # curl www.dir.com:8088/first -I HTTP / 1.1 301 Moved Permanently Server: nginx / 1.15 . 9 Date: Wed, 10 Jul 2019 10 : 31 : 21 GMT Content - Type : text / html Content - Length: 169 Location: http: / / www. dir .com: 8088 / first / Connection: keep - alive |
测试加主机名
1 2 3 4 5 6 7 8 | root@python vhast] # curl -H "Host:aaa" localhost:8088/first -I HTTP / 1.1 301 Moved Permanently Server: nginx / 1.15 . 9 Date: Wed, 10 Jul 2019 10 : 34 : 36 GMT Content - Type : text / html Content - Length: 169 Location: http: / / aaa: 8088 / first / Connection: keep - alive |
修改配置
1 2 3 4 5 6 7 8 | [root@python vhast] # cat dir.conf server { server_name www. dir .com fwe. dir .com; server_name_in_redirect on; #表示修改为主域名在返回的URI里 listen 8088 ; #absolute_redirect off; root html / ; } |
测试
1 2 3 4 5 6 7 8 | [root@python vhast] # curl -H "Host:aaa" localhost:8088/first -I HTTP / 1.1 301 Moved Permanently Server: nginx / 1.15 . 9 Date: Wed, 10 Jul 2019 10 : 36 : 46 GMT Content - Type : text / html Content - Length: 169 Location: http: / / www. dir .com: 8088 / first / Connection: keep - alive |
草都可以从石头缝隙中长出来更可况你呢
分类:
web
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏