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

领导需求:通过nginx配置,使直接访问ip为老页面,访问ip/index为新页面

下面对问题进行简化,可以复现bug,实际上的问题更复杂,所以有人把new和old放一起是不行的哈

复现bug:

# mkdir -p /var/www/static/new
# mkdir -p /var/www/static/old
# echo "new" > /var/www/static/new/index.html
# echo "old" > /var/www/static/old/index.html

修改nginx配置文件
vim nginx.conf

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

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

出现情况:直接访问ip也会匹配到location /index,与index index.html;配置有关,dist打包默认为index.html,所以这个不能改

想过使用通用匹配 = /index,还是出现其他bug

想要效果,访问ip出现old 访问ip/index出现new

帮顶

ip后加个/,加个空格试试呢

不行的,还是会匹配到index,感觉网站后面都会隐藏一个index.html.

location / {
    root /var/www/static/old;
    index index.html;
    try_files $uri $uri/ =404;
}

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

location /index/ {
    alias /var/www/static/new/;
    try_files $uri $uri/ =404;
}
2 个赞

试了一下,因为
location = /index {
alias /var/www/static/new/index.html;
}
这一段,然后直接变成下载那个index.html文件了

FYI.

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

        location ^~ /new {
            alias /var/www/static/new;
            index  index.html index.htm;
        }

经理非要用index,要是能换成new的话,就没那么多毛病了 :joy:

如果只有这两个path,那直接写死就可以吧

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

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

我试下

这个没影响的,试试?

location ^~ /index

1 个赞

这个不行,写死了之后/都匹配不到了,然后跳到默认的nginx页面,然后/index报的404,感觉这个nginx的精确匹配好多问题

试了一下,这个会影响直接访问ip,就是访问ip也是会匹配到index规则,哈哈,你可以复现我的bug然后试一下 :joy:

location /index {
rewrite ^/index$ /index.html break;
root /var/www/static/new;
}

我这用网页访问 ip/index会自动跳转至index/,然后显示为new。不清楚你这是用什么浏览器访问的。

我用的谷歌,我试下其他浏览器

我也是chrome。

正常的呀,理论没问题

这样的话,直接访问ip出现的就是新页面了

对的,但是那个bug可以按我给的方法复现一下