【问题标题】:Nginx custom error page and geoip block.Nginx 自定义错误页面和 geoip 块。
【发布时间】:2018-02-05 08:05:43
【问题描述】:

如果在 nginx 中发现错误(例如 404、403..等),我正在尝试显示自定义错误页面。当我使用以下配置访问 /test 时显示 404 错误页面。

error_page 403 /403.html;
error_page 404 /404.html;

location = /403.html {
  root /usr/share/nginx/html/errors;
  allow all;
}

location = /404.html {
  root /usr/share/nginx/html/errors;
  allow all;
}

# Everything is a 404
location /test {
  return 404; #return the code 404
}

之后,我尝试添加基于 geoip 模块的条件检查。如果$deny_request等于1,nginx会直接返回403。

# Geoip checking to set deny request value. 
if ($blocked_country) {
  set $deny_request 1;
}

if ($deny_request = 1) {
  return 403;
}

http 状态 403 按预期返回。但是,它不是自定义错误页面,而是返回 nginx 默认的 403 错误页面。似乎不可能使用自定义错误页面检查服务器块。我错过了什么吗?

【问题讨论】:

    标签: nginx geoip custom-error-pages


    【解决方案1】:

    同样的问题让我疯狂了 2 周或更长时间,顺便说一句,我找到了一个可行的解决方案,如下:

    # - error
    error_page 502 @offline;
    location @offline {
        root /var/web/example.org/www;
        try_files /502.html 502;
    } 
    

    我在这个答案Nginx won't use custom 502 error page 中找到了解决方案,虽然我不明白为什么您的解决方案不起作用,但我认为它应该。

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 2014-04-24
      • 1970-01-01
      • 2015-08-12
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      相关资源
      最近更新 更多