【问题标题】:How to remove and Redirect away from static .html endings on Nginx?如何从 Nginx 上的静态 .html 结尾删除和重定向?
【发布时间】:2014-05-27 07:15:56
【问题描述】:

目前我正在使用以下配置文件来尝试将所有 URL 从 example.com/page.html 更改为 example.com/page.html。由于 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 的请求。我不确定这是否与此有关。 最后,我找到代码的原始线程是这个:redirect /foo.html to /foo but not / to /index in nginx 它似乎在那里为 OP 工作,但我无法让它工作。

【问题讨论】:

    标签: html redirect nginx varnish


    【解决方案1】:

    是的,这很合乎逻辑,因为您永远不会尝试自己访问$uri,因此服务器会尝试example.com/images/image.png.html

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

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

    【讨论】:

    • 修复了它。我知道它会是那样的小东西。非常感谢您的帮助。
    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2020-03-03
    • 2020-10-04
    • 2020-07-03
    • 2013-11-26
    • 1970-01-01
    • 2015-05-02
    • 2012-07-01
    相关资源
    最近更新 更多