nginx訪問控制php
(1)能夠限制只讓某個ip訪問html
如:只讓127.0.0.1訪問admin.phpnginx
[root@wjh2 ~]# vi /usr/local/nginx/conf/vhosts/discuz.conf #打開虛擬主機配置文件#curl
加入如下配置:ide
location ~ .*admin\.php$ {測試
allow 127.0.0.1;url
deny all;unix
#auth_basic "qing zhu yi an quang";server
#auth_basic_user_file /usr/local/nginx/conf/.htpasswd;htm
include fastcgi_params;
fastcgi_pass unix:/tmp/abc.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /discuz/www$fastcgi_script_name;
}
curl測試結果:127.0.0.1訪問200 OK
其餘IP訪問訪問顯示403
(2)禁止某個IP或者IP段訪問站點的設置
[root@wjh2 ~]# vi /usr/local/nginx/conf/vhosts/discuz.conf #打開虛擬主機配置文件#
加入如下配置全局:
server
{
listen 80;
server_name www.123.com www.456.com www.1234.com;
#域名跳轉
if ($host != 'www.123.com')
{
rewrite ^/(.*)$ http://www.123.com/$1 permanent;
}
index index.html index.htm index.php;
root /discuz/www;
deny 127.0.0.1;
deny 192.168.0.0/24;
(3) 需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:
location /admin/
{
allow 192.168.133.1;
allow 127.0.0.1;
deny all;
}
(4) 能夠匹配正則
location ~ .*(abc|image)/.*\.php$ #只要匹配這兩個目錄,就不能解析php#
{
deny all;
}
(5)根據user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
deny all和return 403效果同樣
總結:若是隻有單獨的全局匹配或單獨的location精確匹配,他們都是以來源IP 逐一從上到下匹配,其中一條命令匹配成功,其餘匹配條件失效。
若是既有全局匹配又有location精確匹配狀況,以location精確匹配狀況爲主、以來源IP 逐一從上到下匹配,其中一條命令匹配成功,其餘匹配條件失效。