LNMP-Nginx访问控制
Nginx访问控制是针对站点特定网页进行IP限制,列如公司WEB网站通常会有一个管理后台页面不想让除公司内部人员的IP进行访问页面,只允许内部人员访问使用,就可以针对IP做限制,但是前提需要公司的人员IP是固定的,否则做限制意义不大。
一、配置
1:编写测试conf文件
[root@host ~]# vim /usr/local/nginx/conf/vhosts/test.conf
说明:对特地ip进行限制访问登录admin.php网页,在 location ~ .*admin\.php$模板下进行编写,allow定义白名单ip,deny定义黑名单IP。
加入以下内容
location ~ .*admin\.php$
{
allow 127.0.0.1; 表示允许127.0.0.1地址访问
deny all;
auth_basic "ghs auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
2:curl指定192.168.2.254访问测试结果
[root@host ~]# curl -x192.168.2.254:80 -I http://www.test.com/admin.php
HTTP/1.1 403 Forbidden
Server: nginx/1.6.2
Date: Wed, 21 Jun 2017 07:55:51 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
192.168.2.254访问http://www.test.com/admin.php拒绝403
3:如果是对整个网站访问限制就需要早全局下写入配置,如下
[root@host ~]# vim /usr/local/nginx/conf/vhosts/test.conf
说明:允许192.168.2.254访问,拒绝所有;拒绝一个网段可以deny后面写IP网段192.168.1.0/24
加入以下红色字体内容
server
{
listen 80;
server_name www.test.com www.ghs.com www.aaa.com;
index index.html index.htm index.php;
root /data/www;
access_log /tmp/access.log ghs;
allow 192.168.2.254;
deny all;
4:curl指定127.0.0.1访问测试结果
[root@host~]# curl -x127.0.0.1:80 -I http://www.test.com/forum.php
HTTP/1.1 403 Forbidden
Server: nginx/1.6.2
Date: Wed, 21 Jun 2017 07:55:22 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
显示403状态码127.0.0.1地址拒绝访问
2:curl指定192.168.2.254白名单ip访问测试结果
[root@host ~]# curl -x192.168.2.254:80 -I http://www.test.com/forum.php
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 21 Jun 2017 07:55:02 GMT
Content-Type: text/html; charset=gbk
Connection: keep-alive
X-Powered-By: PHP/5.4.37
显示200状态码192.168.2.254地址允许访问
记录每一天有趣的事情!!

浙公网安备 33010602011771号