Nginx针对URL或目录访问控制
一、根据扩展名限制程序和文件访问
1、配置nginx,禁止解析指定目录下的指定程序,若要允许某个目录,在添加允许即可(须写在处理php前面)。
location ~ ^/images/.*\.(php|php5|sh|pl|py)$
{
deny all;
}
location ~ ^/static/.*\.(php|php5|sh|pl|py)$
{
deny all;
}
location ~ ^/data/(attachment|avatar).*\.(php|php5)$
{
deny all;
}2、nginx下配置禁止访问*.txt和*.doc文件,放置在server标签内:
location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /data/web;
rewrite ^(.*) https://www.shnne.com/ break; #可以重定向到某个URL;
}
}
location ~* \.(txt|doc)$ {
root /data/web;
deny all;
}3、禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) {
return 404;
}4、排除某个目录不受上面限制
location ~ \.well-known{
allow all;
}二、禁止访问指定目录下的所有文件和目录
1、禁止访问单个目录的命令如下:
location ~ ^/(static)/ {
deny all;
}
或
location ~ ^/static {
deny all;
}2、禁止访问多个目录的配置如下:
location ~ ^/(static|js) {
deny all;
}3、禁止访问目录并返回指定的http状态码,配置如下:
server {
listen 80;
server_name www.shnne.com;
location / {
root html;
index index.php index.html index.htm;
}
location /admin/ { return 404; } #访问admin目录返回404;
location /templates/ { return 403; } #访问templates目录返回403
location ~ ^/images/.*\.(php|php5|sh|pl|py)$
{
deny all;
}三、限制网站来源IP访问
1、禁止目录让外界访问,但允许某IP访问该目录且支持PHP解析,配置如下:
#在server标签内配置如下:
location ~ ^/admin/ {
allow 192.168.200.8;
deny all;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
#说明:该配置只允许192.168.200.8的IP访问admin目录location ~ ^/test/(test1).*\.(php|php5|txt)$
{
allow 192.168.200.8;
deny all;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}
}
#只允许该ip访问test1下的php文件,需要注意的是,在这个location下也得加入php解析相关的配置,否则php文件无法解析
或者如下:
location ~ .*admin.* {
allow 192.168.200.8;
deny all;
location ~* \.php$ {
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}2、限制IP或IP段访问,配置如下:
#添加在server标签内:
location / {
deny 192.168.0.4;
allow 192.168.1.0/16;
allow 10.0.0.0/24;
deny all;
}
说明:此限制是对某些IP做整个网站的限制访问。3、nginx做反向代理的时候也可以限制客户端IP,具体如下:
方法1:使用if来控制,配置如下:
if ( $remoteaddr = 10.0.0.7 ) {
return 403;
}
if ( $remoteaddr = 192.168.200.8 ) {
set $allow_access_root 'ture';
}
方法2:利用deny和allow只允许IP访问,配置如下:
location / {
root html/blog;
index index.php index.html index.htm;
allow 10.0.0.7;
deny all;
}
方法3:只拒绝某些IP访问,配置如下:
location / {
root html/blog;
index indx.php index.html index.htm;
deny 10.0.0.7;
allow all;
}4、禁止某ip段访问并向浏览器输出一段文字(若有乱码,请在server中添加:charset utf-8;):
if ($remote_addr ~* ^188\.163\.(.*?)\.(.*?)$){
return 666 "禁止访问啊!";
}注意事项:
deny一定要加一个IP,否者会直接跳转到403,不在往下执行了,如果403默认页在同一域名下,会造成死循环访问。
对于allow的IP段,从允许访问的段位从小到大排列,如127.0.0.0/24的下面才能是10.10.0.0/16,其中:
24表示子网掩码:255.255.255.0
16表示子网掩码:255.255.0.0
8表示子网掩码:255.0.0.0
以deny all; 结尾,表示除了上面允许的,其他的都禁止。如:
deny 192.168.1.1;
allow 127.0.0.0/24;
allow 192.168.0.0/16;
allow 10.10.0.0/8;
deny all;
四、配置nginx,禁止非法域名解析访问企业网站
方法1:让使用IP访问网站的用户,或恶意接卸域名的用户,收到501错误,配置如下:
server {
listen 80 default_server;
server_name _;
return 501;
}
方法2:通过301跳转主页,配置如下:
server {
listen 80 default_server;
server_name _;
rewrite ^(.*) http://www.shnne.com/$1 permanent;
}
方法3:发现某域名恶意解析到公司的服务器IP,在server标签里添加以下代码即可,若有多个server要多处添加。
if ($host !~ ^www/.tag/.com$) {
rewrite ^(.*) http://www.shnne.com$1 permanent;
}<< 上一篇
下一篇 >>
网友留言(0 条)