Nginx配置自定义的403页面

1.开启nginx的状态码,虚拟主机配置中加入下边一段

location /nginx_status
{
stub_status on;
access_log off;
}

或着在nginx的http模块加入:fastcgi_intercept_errors on;

2.在server模块加入

根据需求来配置,
因为deny语句把所有对403.html的访问给deny了,所以需要在locaction = /403.html里面加上allow all,让所有的IP地址能访问403.html具体过程如下


需求A:允许某个拒绝所有

location / {
allow 192.168.1.0/24;
deny all;
error_page 403 /403.html;
location /403.html {
allow all;
}
}

需求B:拒绝某个允许所有

location / {
deny 192.168.1.0/24;
alllow all;
error_page 403 /403.html;
location /403.html {
allow all;
}
}

3.编写403页面,并放入html下面

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<h1>403 Forbidden</h1>
<p>You don't have permission to access the URL on this server. Sorry for the inconvenience.<br/>
This is a our test website, please visit our official website <a href="http://www.localhost.com" title="Click View Site " ><font size="5">www.localhost.com</a>
</font><br/>
Thank you very much!</p>
URL: http://www.localhost.com
<br/>Date:
<script language="JavaScript" type="text/javascript">
var enabled = 0; today = new Date();
var date;
M=today.getMonth() + 1
D=today.getDate()
HH=today.getHours()
MM=today.getMinutes()
SS=today.getSeconds()
if (M<10)
{
M="0"+M
}
if (D<10)
{
D="0"+D
}
if (MM<10)
{
MM="0"+MM
}
if (HH<10)
{
HH="0"+HH
}
if (SS<10)
{
SS="0"+SS
}
date = (today.getFullYear()) + "/" + M + "/" + D + " " + HH+":"+MM+":"+SS +"";
document.write(date);
</script>
</html>

4.service nginx restart  重启nginx服务器

5.如果没有生效加上这么一段

location = /403.html {
root /html;
allow all;
}
posted @ 2019-07-05 16:59  Mr.peter  阅读(13466)  评论(0编辑  收藏  举报