簡體   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