【问题标题】:Return custom 403 error page with nginx使用 nginx 返回自定义 403 错误页面
【发布时间】:2011-03-08 08:10:59
【问题描述】:

每当发生 403 错误时,我都会尝试在 /temp/www/error403.html 中显示错误页面。

这应该是每当用户尝试通过 https (ssl) 访问站点并且它的 IP 在 blovkips.conf 文件中时,但目前它仍然显示 nginx 的默认错误页面。 我的其他服务器有相同的代码(没有任何阻塞),它可以工作。

是否阻止 IP 访问自定义 403 页面? 如果是这样,我该如何让它工作?

server  {
    # ssl
    listen               443;
    ssl                  on;
    ssl_certificate      /etc/nginx/ssl/site.in.crt;
    ssl_certificate_key  /etc/nginx/ssl/site.in.key;
    keepalive_timeout    70;

    server_name localhost;


    location / {
            root   /temp/www;
            index  index.html index.htm;
}

# redirect server error pages to the static page
error_page   403  /error403.html;
# location = /error403.html {
#         root   /temp/www;
# }

    # add trailing slash if missing
    if (-f $document_root/$host$uri) {
            rewrite ^(.*[^/])$ $1/ permanent;
    }      

    # list of IPs to block
    include blockips.conf;
}

编辑: 已将 error_page 代码从 504 更正为 403,但我仍然遇到同样的问题

【问题讨论】:

    标签: webserver nginx http-status-code-403 custom-error-pages


    【解决方案1】:

    我在来这里之前做了很多谷歌搜索,但现在又做了一些,在 5 分钟内我得到了答案:P

    似乎我不是唯一遇到此问题的人:

    error_page 403 /e403.html;
      location = /e403.html {
      root   html;
      allow all;
    }
    

    http://www.cyberciti.biz/faq/unix-linux-nginx-custom-error-403-page-configuration/

    似乎我认为对我的错误页面的访问被阻止是正确的。

    【讨论】:

      【解决方案2】:

      问题可能是您尝试从禁止访问的网络服务器发送 403“禁止”错误。 Nginx 将 error_page 指令视为内部重定向。所以它正在尝试服务器https://example.com/error403.html,这也是被禁止的。

      因此,您需要像这样使错误页面不从 https 中提供:

      error_page  403   http://example.com/error403.html
      

      或将必要的“允许访问”选项添加到错误页面路径的位置。测试这个的方法是直接访问 /error403.html 页面。如果您无法以这种方式访问​​,那么当有人收到实际的 403 错误时,它就无法正常工作。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题...关键是我已经在服务器上下文级别(或者如果您愿意,可以在虚拟主机级别)实现了 ip 白名单,所以每个位置也会有这个(基本上 /403.html 不会)不可访问):

        server {
          listen       *:443 ssl;
          server_name  mydomain.com ;
          error_page 403 /403.html;
          .....
          if ($exclusion = 0) { return 403; } #implemented in another conf.d files (see below)
          location ~ \.php$ {
            root          /var/www/vhosts/mydomain.com/httpdocs;
            include       /etc/nginx/fastcgi_par
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_connect_timeout 3m;
            fastcgi_read_timeout 3m;
            fastcgi_send_timeout 3m;
          }
          location /403.html {
            root      /usr/share/nginx/html;
            allow all;
          }
        
          ...
        }
        

        排除 conf.d 文件示例:

        geo $exclusion {
          default 0;
          10.0.0.0/8  Local network
          80.23.120.23 Some_ip
          ...
        }
        

        要解决这个问题,只需在位置级别(上下文)执行返回 403:

        server {
          listen       *:443 ssl;
          server_name  mydomain.com ;
          error_page 403 /403.html;
          .....
          location ~ \.php$ {
            if ($exclusion = 0) { return 403; } 
            root          /var/www/vhosts/mydomain.com/httpdocs;
            include       /etc/nginx/fastcgi_par
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_connect_timeout 3m;
            fastcgi_read_timeout 3m;
            fastcgi_send_timeout 3m;
          }
          location /403.html {
            root      /usr/share/nginx/html;
            allow all;
          }
        
          ...
        }
        

        为我工作。

        【讨论】:

          【解决方案4】:

          在列出的配置中似乎有一个 boo-boo,因为它只向自定义页面发送错误代码 503(“服务不可用”),所以对于 403(“禁止”),您可能想要使用:

          error_page 403 /error403.html
          

          【讨论】:

          • (这里我假设blockips.conf文件是有效的,每一行都像deny 1.2.3.4;。)
          • 是的,据我所知,blockips.conf 是正确的,我只有这个未注释:全部拒绝; (用于测试)
          • 好的,我现在用 403 而不是 503 编辑了配置文件,重新启动了,我仍然得到默认的 '403 Forbidden nginx' 任何其他想法?
          • 我只是尝试输入所有错误代码(400-599 ex 499),我仍然得到默认的 nginx 错误页面。
          猜你喜欢
          • 2012-01-31
          • 2013-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-09
          • 1970-01-01
          • 2014-04-24
          相关资源
          最近更新 更多