AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 832086
Accepted
user292113
user292113
Asked: 2017-02-12 23:10:14 +0800 CST2017-02-12 23:10:14 +0800 CST 2017-02-12 23:10:14 +0800 CST

Nginx - 如何禁用error_page的重写

  • 772

我目前在 Fedora 25 Server Edition x86_64 上使用 Nginx 1.10.2 托管几个小型静态网站。

我已将 Nginx 配置为假设.html没有文件扩展名 ( try_files) 的 for 请求,并在请求时将 (永久rewrite) 重定向到.htmlURL 的 -less 版本.*\.html。

我也有自定义错误页面。据我目前所知,该error_page指令与重写不能很好地配合,因为我遇到了应该返回正常错误消息的页面的重定向循环......我相信这是所有相关的配置:

server {
    [...]

    try_files  $uri  $uri.html  $uri/index.html  =404;

    error_page  400  /errors/400.html;
    error_page  401  /errors/401.html;
    error_page  403  /errors/403.html;
    error_page  404  /errors/404.html;
    error_page  408  /errors/408.html;
    error_page  500  /errors/500.html;
    error_page  503  /errors/503.html;

    # Remove "index" from URL if requesting the front page.
    rewrite  "^/index(\.html)?$"    "/"    permanent;

    # Strip .html file extension, trailing slash, or "index.html".
    rewrite  "/(.+)(\.html|/|/index.html)$"  "/$1"  permanent;

    [...]
}

以下是我认为Nginx 正在做的事情:

  1. 客户请求/fake-page
  2. 查找/fake-page,/fake-page.html或/fake-page/index.html.
  3. 当这些都不匹配时,内部重定向以显示错误 404 页面。
  4. 但随后/errors/404.html被带标志的 .html 剥离permanent ,导致 301 用户重定向。

我在最后rewrite一行尝试了几种变体,甚至将它放在一个location ^~ /errors/ {}块中(我认为这应该意味着重写只适用于不在/errors/ 目录下的 URL)。但是我所做的一切都导致错误 404 永久重定向到 404页面,然后它不会返回实际的 404 状态 ---或者它最终陷入重定向循环。

rewrite redirect configuration nginx errordocument
  • 3 3 个回答
  • 10086 Views

3 个回答

  • Voted
  1. Richard Smith
    2017-02-13T02:00:05+08:002017-02-13T02:00:05+08:00

    我建议您将rewrites内部location块包裹起来,否则很难控制它们的全球影响力。

    此示例似乎适用于您在问题中发布的代码段:

    server {
        root /path/to/root;
    
        location / {
            # Remove "index" from URL if requesting the front page.
            rewrite  "^/index(\.html)?$"    "/"    permanent;
    
            # Strip .html file extension, trailing slash, or "index.html".
            rewrite  "/(.+)(\.html|/|/index.html)$"  "/$1"  permanent;
    
            try_files  $uri  $uri.html  $uri/index.html  =404;
        }
    
        error_page  400  /errors/400.html;
        error_page  401  /errors/401.html;
        error_page  403  /errors/403.html;
        error_page  404  /errors/404.html;
        error_page  408  /errors/408.html;
        error_page  500  /errors/500.html;
        error_page  503  /errors/503.html;
    
        location /errors/ {
        }
    }
    
    • 1
  2. Anubioz
    2017-02-13T21:02:36+08:002017-02-13T21:02:36+08:00

    这是一个棘手的问题,但最终可以按要求工作:

    server {
    
        [...]
    
        root /path/to/root;
        set $docroot "/path/to/root";
        error_page  400  /errors/400.html;
        error_page  401  /errors/401.html;
        error_page  403  /errors/403.html;
        error_page  404  /errors/404.html;
        error_page  408  /errors/408.html;
        error_page  500  /errors/500.html;
        error_page  503  /errors/503.html;
    
        location = /errors/404.html {
            root $docroot;
            internal;
        }
    
        location ~ ^/index(\.html)?$
        {
            return 301 "/";
            break;   
        }
    
         location ~ ^/$
        {
            try_files  $uri  $uri.html  $uri/index.html  =404;
            break;
        }
    
        location ~ ^/(.+)(\.html|/|/index.html)$
        {
            if (-f $docroot/$1) {
                return 301  "/$1";
                break;
            }
    
            if (-f $docroot/$1.html) {
                return 301  "/$1";
                break;
            }
    
            if (-f $docroot/$1/index.html) {
                return 301  "/$1";
                break;
            }
    
            try_files missing_file  =404; 
        }
    
        location ~ ^/(.+)(?!(\.html|/|/index.html))$
        {
            try_files  $uri  $uri.html  $uri/index.html  =404;    
        }
    
        [...]
    }
    

    我稍后会尝试用评论来扩展它;)

    • 1
  3. Best Answer
    user292113
    2017-02-17T02:03:10+08:002017-02-17T02:03:10+08:00

    受@Anubioz 的回答启发,我找到了一个更简单的解决方案。

    server {
        [...]
    
        # This part's pretty common.
        # Assume ".html" or ".../index.html" if the original request doesn't
        # match any real files. Return error 404 if still can't find any
        # matching files.
        try_files  $uri  $uri.html  $uri/index.html  =404;
    
        # Match any resulting HTTP status codes with the custom error pages
        # that I designed.
        error_page  400  /errors/400.html;
        error_page  401  /errors/401.html;
        error_page  403  /errors/403.html;
        error_page  404  /errors/404.html;
        error_page  408  /errors/408.html;
        error_page  500  /errors/500.html;
        error_page  503  /errors/503.html;
    
        # Make sure the paths to the custom error pages are not transformed
        # before sending the actual status codes to a request.
        # These error pages are for *internal* use only.
        location = "/errors/400.html" {
            internal;
        }
        location = "/errors/401.html" {
            internal;
        }
        location = "/errors/403.html" {
            internal;
        }
        location = "/errors/404.html" {
            internal;
        }
        location = "/errors/408.html" {
            internal;
        }
        location = "/errors/500.html" {
            internal;
        }
        location = "/errors/503.html" {
            internal;
        }
    
        # Remove "index" from URL if requesting the front page.
        location ~ "^/index(\.html)?$" {
            return  301  "/";
        }
    
        # Strip .html file extension, trailing slash, or index,
        # forcing the "pretty" URL, if user requests a ".html" URL.
        location ~ "^/(.+)(\.html|/|/index|/index.html)$" {
            return  301  "/$1";
        }
    
        [...]
    }
    

    location包含指令的块可以internal防止错误页面被通常的 URL 处理所触及。我确实尝试只匹配/errors/目录,这样我就不必一遍internal又一遍地重复指令,但是 Nginx 匹配传入的请求以重写规则,基本上按规则选择器的特异性排序:只有与每个自定义错误页面的完全匹配才有足够的特异性在 Nginx 开始对其进行 URL 重写过程之前,抓住错误页面。

    (我对 Nginx 行话的使用可能有点不对劲,但在我看来这就是正在发生的事情。而且这个配置完全符合代码注释所说的,这正是我从一开始就想要的。)

    • 1

相关问题

  • 阿帕奇虚拟主机

  • IIS 7 中的 URL 重写

  • IIS7 请求跟踪日志上的 URL_CHANGED 事件是什么?

  • 如何强制我的网址始终以 www 开头?

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve