繁体   English   中英

使用nginx返回自定义403错误页面

[英]Return custom 403 error page with nginx

我试图在发生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;
}

编辑:更正了错误页面代码从504到403,但我仍然有同样的问题

在来到这里之前我做了大量的谷歌搜索,但刚刚做了一些,在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/

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

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

因此,您需要使错误页面不是由https提供的,如下所示:

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

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

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

error_page 403 /error403.html

我有同样的问题......重点是我已经在服务器上下文级别(或者你喜欢的vhost级别)实现了ip白名单,所以每个位置都会有这个(basicaly /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;
  }

  ...
}

适合我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM