首先在/root/access目录下新建一个HTML文件,作为访问页面,名为admin.html
[root@localhost code]# vim /root/access/admin.html
<html>
<head>
<meta charset="utf-8">
<title>vincen</title>
</head>
<h1>本机允许访问</h1>
<h1>hello</h1>
</html>
1、修改Nginx的配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
在原有的location内容里修改root的路径
root /root/access; #页面访问的路径
在这配置文件里的location后面再增加多一个location的内容,然后保存并退出
location ~ ^/admin.html {
root /root/access; #页面访问的路径
index index.html index.htm; #访问html文件或者htm
deny 61.141.96.68; #拒绝IP为61.141.96.68访问
allow all; #允许所有IP访问(除了61.141.96.68)
}
2、检查配置文件的语法是否正确
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#弹出以上两个提示则说明语法正确
3、重新加载Nginx服务
[root@localhost ~]# nginx -s reload -c /etc/nginx/nginx.conf
4、在本机浏览器上访问这个虚拟机(IP),也就是访问Nginx。因为配置文件设置拒绝本机IP访问,其他IP则可以访问。
(403 forbidden表示没有权限访问)。(如果有VPN或者代理的,则可以使用其他IP进行测试)
5、这时候再回去修改Nginx的配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
修改刚刚添加的location的内容,允许本机IP访问,其他IP不能访问
location ~^/admin.html{
root /root/access;
index index.html index.htm;
deny all; #拒绝所有IP访问
allow 61.141.96.68; #允许IP为61.141.96.68访问
}
6、检查Nginx的语法,确认没错误后进行重载服务
[root@localhost ~]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# nginx -s reload -c /etc/nginx/nginx.conf
7、在浏览器访问虚拟机,这时候本机是访问成功的。(如果有VPN或者代理的,则可以使用其他IP进行测试)