nginx的 try_file配置

遇到的问题

用户访问域名的过程简述一下

www.sumumutop-》cdn-》源站域名-》源站ip-》ingress-》service-》pod-》nginx-》html_dir
  • 1.问题表现
    https://www.sumumutop/abc/dedf/ 如果在后面加一个/ 访问的时候 会访问到 https://www.sumumutop/abc/dedf
    然后在这个页面上 刷新一下会跳转到源站的域名上
    http://yuan.sumumutop/abc/dedf

  • 2.打开控制台可以看到有一个301到了源站地址。
    image
    用户访问的东西在cdn上没有,然后回源也是正常的。那为什么页面刷新一下就成了源站地址了呢

  • 3.命令行用curl命令在测试环境复现了问题:

# 不加/ 可以看到 301了一下然后location 到了源站的域名
suyajun@MacBook-Pro-2 ~ % curl -I https://test.sumumutop/abc/dedf
HTTP/2 301
date: Fri, 07 Mar 2025 10:40:22 GMT
content-type: text/html
content-length: 239
location: http://yuan.sumumutop/abc/dedf
access-control-allow-origin: *
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: Origin, Content-Type, Accept, Authorization
strict-transport-security: max-age=31536000; includeSubDomains

# 加上/ 状态码就成了200。
suyajun@MacBook-Pro-2 ~ % curl -I https://test.sumumutop/abc/dedf/
HTTP/2 200
date: Fri, 07 Mar 2025 10:40:42 GMT
content-type: text/html; charset=utf-8
content-length: 169176
last-modified: Fri, 14 Feb 2025 10:36:22 GMT
vary: Accept-Encoding
etag: "67af1ca6-294d8"
access-control-allow-origin: *
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: Origin, Content-Type, Accept, Authorization
accept-ranges: bytes
strict-transport-security: max-age=31536000; includeSubDomains

解决方法

  • 1 修改我们的nginx配置文件

可以看到多了一个index.html

image

  • 2 验证
# 加上/和不加/ 状态返回的都是是200,没有进行301跳转
suyajun@MacBook-Pro-2 ~ % curl -I https://test.sumumutop/abc/dedf
HTTP/2 200
date: Fri, 07 Mar 2025 10:40:42 GMT
content-type: text/html; charset=utf-8
content-length: 169176
last-modified: Fri, 14 Feb 2025 10:36:22 GMT
vary: Accept-Encoding
etag: "67af1ca6-294d8"
access-control-allow-origin: *
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: Origin, Content-Type, Accept, Authorization
accept-ranges: bytes
strict-transport-security: max-age=31536000; includeSubDomains


suyajun@MacBook-Pro-2 ~ % curl -I https://test.sumumutop/abc/dedf/
HTTP/2 200
date: Fri, 07 Mar 2025 10:40:42 GMT
content-type: text/html; charset=utf-8
content-length: 169176
last-modified: Fri, 14 Feb 2025 10:36:22 GMT
vary: Accept-Encoding
etag: "67af1ca6-294d8"
access-control-allow-origin: *
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: Origin, Content-Type, Accept, Authorization
accept-ranges: bytes
strict-transport-security: max-age=31536000; includeSubDomains

知识补充

   location /abc {
       alias /data/abc;
       index index.html; 
       # index.html 是默认的索引文件。如果用户访问 /abc/ 目录时,Nginx 会自动查找 /data/abc/index.html 文件并返回给用户
       try_files $uri $uri/index.html /abc/index.html;
       # $uri:表示当前请求的 URI。比如请求 /data/about.html,$uri 会是 /data/about.html。 
       # $uri/index.html:如果 $uri 是一个目录,Nginx 会尝试访问该目录下的 index.html 文件 
       # /abc/index.html:如果前两个路径都没有匹配到文件,最后 Nginx 会返回 /data/abc/index.html 文件.在某些情况下如果没有找到对应的文件,就会返回  /abc/index.html
       #如果都不存在,它会返回 /abc/index.html(适用于前端路由)。
   }

posted @ 2025-03-07 20:57  Hello_worlds  阅读(658)  评论(0)    收藏  举报