碰到一个nginx问题,有没有佬复现一下

我看下我朋友电脑试下

根路径加了嘛?

重启了吗?nginx改完配置得重启

对的 缓存重启都弄烂了。

这个方法有用

location = /index {
    alias /var/www/static/new/index.html;
    add_header Content-Type text/html;

}
可以试试加上这一行,或者
location = /index {
return 301 /index/;
}
然后开隐身模式试试还会不会下载,我用edge也自动下载网页了。

1 个赞

牛的牛的

   location / {
        root   /var/www/static/old;
       index  index.html;
    }
    location  /index/ {
        root /var/www/static/new;
        index index.html;
    }

    location = /index {
        return 301 /index/;
    }

这样就完美了

有一个疑问,就是nginx的精确匹配 = /index后面接alias /var/www/static/new目录就怎么样都不行,一定要接alias /var/www/static/new/index.html具体文件

/var/www/static/new/试试

一样的,一开始的bug不是访问不到new,是访问old出问题 :melting_face:

这个我也不清楚

我一开始好像理解错你的意思了,我试了一下,然后上午我这个问题问了gpt,他也是说用这个目录,结果居然报404,特别奇怪

这是我测试的, 可以正常访问, 可以试试看

location / {
    root   D:/test/old/;
    index index.html;
}

location  /index/ {
    alias D:/test/new/;
 }

对,这个是可以的,不过有的浏览器不会给你ip/index补最后一"/",然后加了个重定向

    location = /index {
        return 301 /index/;
    }
1 个赞
 location  = /index {
        root /var/www/static/new/;
        index index.html;
    }

经过实验精准匹配这样写404的原因,精准匹配= /后面接的是文件,即访问的文件不是/var/www/static/new/index.html,而是/var/www/static/new/index这个文件。

 location  = /index {
        alias /var/www/static/new/index.html;
    }

同理,这个可以的原因也是将精准匹配的/var/www/static/new/index文件 alias到了 /var/www/static/new/index.html

因为直接接的是文件,导致浏览器访问可能是去下载该文件,可以通过设置add_header Content-Type text/html;或default_type “text/html”,来告知浏览器进行渲染文件为html,就能避免访问该页面去下载页面