限制訪問的方法:
有時候我們通過訪問日誌,能看到有幾個IP會不停的嘗試***網站,需要去禁掉這幾個IP;或者去限定某些IP可以訪問,其他IP均不許訪問。比如:管理中心,只要管理員訪問,其他IP一律禁止訪問。
訪問控制比"用戶認證"要更安全。
配置方法:
編輯虛擬主機配置文件
[root@LampLinux ~]# vim /usr/local/nginx/conf/vhosts/test.conf
修改下面配置(藍色爲刪除,紅色爲新增):
location ~ .*admin\.php$ {
#auth_basic "LampLinux auth";
#auth_basic_user_file /usr/local/nginx/conf/.htpasswd; (刪除auth相關配置)
allow 127.0.0.1;
deny all; # 只允許127.0.0.1訪問,其他全部拒絕。
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
檢查並重加載
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -t
[root@LampLinux ~]# /usr/local/nginx/sbin/nginx -s reload
測驗:
[root@LampLinux ~]# curl -x127.0.0.1:80 www.test.com/admin.php -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 12 Aug 2015 11:04:19 GMT
...
127.0.0.1訪問管理中心可以通行。
[root@LampLinux ~]# curl -x192.168.137.11:80 www.test.com/admin.php -I
HTTP/1.1 403 Forbidden
Server: nginx/1.6.2
Date: Wed, 12 Aug 2015 11:05:09 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
192.168.137.11訪問管理中心無法讀取。
[root@LampLinux ~]# curl -x192.168.137.11:80 www.test.com/forum.php -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 12 Aug 2015 11:06:33 GMT
...
192.168.137.11訪問其他頁面不受限制。
僅僅是對admin.php做了403限制。
補充:
我們也可以針對目錄做訪問限制:
location ~ /abc/
{
allow .....;
deny .....;
}
還可以針對全局,比如說整個網站不允許127.0.0.1訪問,其他的都允許,
我們需要在整個網站配置裏面(即location上面一段結尾)加上deny 127.0.0.1;就可以了。
如果想要把192.168.137.*的這個網段都拒絕掉,接着寫入deny 192.168.137.0/24;就可以了。
配置內容如圖: