一、服務(wù)器全局限IP
#vi
nginx.conf
allow 10.57.22.12; #允許的IP
deny all;
二、站點(diǎn)限IP
#vi
vhosts.conf
站點(diǎn)全局限IP:
location / {
index index.html index.htm index.php;
allow 10.57.22.12;
deny all;
location ~ \.php$ {
if ( $fastcgi_script_name ~ \..*\/.*php )
{return 403;}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/htdocs$fastcgi_script_name;
include fastcgi_params;
}
}
站點(diǎn)IP限目錄:
location / {
index index.html index.htm index.php;
}
location ^~ /test/ {
allow 10.57.22.12;
deny all;
if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/htdocs$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/htdocs$fastcgi_script_name;
include fastcgi_params;
}
注意事項(xiàng):
1. deny 一定要加一個(gè)ip,否則直接跳轉(zhuǎn)到403,不往下執(zhí)行了;如果403默認(rèn)頁(yè)是同一域名下,會(huì)造成死循環(huán)訪問(wèn);
2. allow的ip段
從允許訪問(wèn)的段位從小到大排列,如127.0.0.0/24 下面才能是10.10.0.0/16
24表示子網(wǎng)掩碼:255.255.255.0
16表示子網(wǎng)掩碼:255.255.0.0
8表示子網(wǎng)掩碼:255.0.0.0
3. deny all;結(jié)尾 表示除了上面allow的其他都禁止
如:
deny 192.168.1.1;
allow
127.0.0.0/24;
allo w
192.168.0.0/16;
allow
10.10.0.0/16;
deny
all;
虛擬主機(jī)可以通過(guò)自定義偽靜態(tài) 進(jìn)行設(shè)置
添加 deny 192.168.1.1; //禁止192.168.1.1訪問(wèn)
allow 127.0.0.0/24; //永許127.0.0段訪問(wèn)
allow 192.168.0.0/16; //永許192.168.段訪問(wèn)
allow 10.10.0.0/16; //永許10.10.段訪問(wèn)
deny all; //禁止所有訪問(wèn)