如何在Nginx上删除和重定向静态.html结尾



目前我正在使用以下配置文件,试图将所有URL从example.com/page.html更改为example.com/pagehtml。由于SEO原因,page.html应该不再可访问,它应该是一个非常简单的重定向。经过搜索,我发现了以下代码:

server {
        listen  myip8:8080;
        root /mydir;
        index index.html index.htm;
        server_name example.com;
# example.com/index gets redirected to example.com/
location ~* ^(.*)/index$ {
    return 301 $scheme://$host$1/;
}
# example.com/foo/ loads example.com/foo/index.html
location ~* ^(.*)/$ {
    try_files $1/index.html @backend;
}
# example.com/a.html gets redirected to example.com/a
location ~* .html$ {
    rewrite ^(.+).html$ $scheme://$host$1 permanent;
}
# anything else not processed by the above rules:
# * example.com/a will load example.com/a.html
# * or if that fails, example.com/a/index.html
location / {
    try_files $uri.html $uri/index.html @backend;
}
# default handler
# * return error or redirect to base index.html page, etc.
location @backend {
    return 404;
}

然而,我遇到了一个问题。找不到我所有的静态资产。CSS、JS等只是给出一个404错误。

代码中的什么可能导致404?

另外,值得注意的是我的服务器设置。我有两个独立的VPS。一个清漆和一个Nginx。Varnish服务器代理对Nginx的请求。我不确定这是否与此有关。最后,我发现代码的原始线程是这样的:在nginx中重定向/foo.html到/foo,而不是/to/index它似乎对OP有效,但我无法让它发挥作用。

yea这是非常合乎逻辑的,因为您从来没有尝试自己访问$uri,所以服务器尝试example.com/images/image.png.html

由于您已经处理了上面的html案例,因此应该将$uri添加为第一优先级。

try_files $uri $uri.html $uri/index.html @backend;

最新更新