V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
brader
V2EX  ›  程序员

我博客某些链接访问被 301 重定向的奇怪现象

  •  1
     
  •   brader · 2022-10-19 17:48:55 +08:00 · 1158 次点击
    这是一个创建于 553 天前的主题,其中的信息可能已经有所发展或是发生改变。

    博客地址 https://brad.9iqu.cn

    我的博客是使用 hexo 搭建的博客,有些链接会被重定向,不知道这是 hexo 的问题,还是我 nginx 配置的问题。

    比如我博客里面有个页面 a 标签地址是 https://brad.9iqu.cn/tags ,然后它就非要给我 301 重定向到 https://brad.9iqu.cn/tags/ 。反正不带 /后缀的导航标签,基本都被重定向,感觉这样有点影响性能

    下面是我的 nginx 配置

    #重定向到 https 请求
    server {
      listen          80;
      server_name     brad.9iqu.cn;
      return          301 https://$server_name$request_uri;
    }
    
    #https
    server {
        listen       443 ssl http2 ; #SSL 访问端口号为 443
        server_name  brad.9iqu.cn;
        ssl_certificate cert/1_brad.9iqu.cn_bundle.crt; #证书文件
        ssl_certificate_key cert/2_brad.9iqu.cn.key; #私钥文件
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
        ssl_prefer_server_ciphers on;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root  /www/hexo-blog/public;
            index index.html index.htm index.php;
        }
    
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    
        # deny access to .htaccess files, if Apache's document root
        location ~ /\.ht {
            deny  all;
        }
    }
    
    7 条回复    2022-10-20 18:29:59 +08:00
    proxytoworld
        1
    proxytoworld  
       2022-10-19 18:19:22 +08:00
    有的 cdn 也会这样搞
    brader
        2
    brader  
    OP
       2022-10-19 18:21:27 +08:00
    @proxytoworld 这个我没有上什么 cdn 的,我是在自己服务器自搭的
    Wincer
        3
    Wincer  
       2022-10-19 18:28:56 +08:00
    加几行配置:

    rewrite ^(.+)/+$ $1 permanent;
    rewrite ^(.+)/index.html$ $1 permanent;
    try_files $uri $uri/index.html =404;

    另外,访问被重定向是默认的行为:在访问的 /tags 页面时,网站目录下并没有一个名叫 tags 的文件,而存在一个 tags 的目录,因此给你重定向到了目录下,展示了目录下的 index.html 文件的内容。
    IvanLi127
        4
    IvanLi127  
       2022-10-19 18:31:42 +08:00 via Android
    你的 server 比较严谨,哈哈。他没把 index.html 给你吐出来就不错了
    10935336
        5
    10935336  
       2022-10-19 19:02:17 +08:00
    /tags 代表的是文件
    /tags/ 代表的才是目录
    eason1874
        6
    eason1874  
       2022-10-20 14:48:46 +08:00
    主流 CMS 都这样,会跳转,为了语义统一,区别像 #5 说的那样是文件和目录的区别

    你内页链接统一用带斜杠的就不会有多余跳转了,其他人不小心访问到不带斜杆的就让它自动跳转,没什么负担,对性能影响可以忽略不计
    brader
        7
    brader  
    OP
       2022-10-20 18:29:59 +08:00
    我把顶部导航的路径配置全部换成带 / 后缀的,现在好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5346 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:14 · PVG 17:14 · LAX 02:14 · JFK 05:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.