一、根据扩展名限制程序和文件访问

利用nginx配置禁止访问上传资源目录下的PHP、Shell、Perl、Python程序文件。

配置nginx,禁止解析指定目录下的指定程序。

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;
}

对上述目录的限制必须写在nginx处理PHP服务配置的前面,如下:

放置在server标签内:

    server {
listen 80;
server_name www.dmtest.com;
location / {
root html;
index index.php index.html index.htm;
} 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;
} ......
......
}

nginx下配置禁止访问*.txt和*.doc文件

配置如下:

放置在server标签内:

        location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /data/www/www;
#rewrite ... #可以重定向到某个URL;
break;
}
}
location ~* \.(txt|doc)$ {
root /data/www/www;
deny all;
}

二、禁止访问指定目录下的所有文件和目录

配置禁止党文指定的单个或多个目录。

禁止访问单个目录的命令如下:

放置在server标签内:

       location ~ ^/(static)/ {
deny all;
} location ~ ^/static {
deny all;
}

禁止访问多个目录的配置如下:

        location ~ ^/(static|js) {
deny all;
}

禁止访问目录并返回指定的http状态码,配置如下:

放置在server标签内:

    server {
listen 80;
server_name www.dmtest.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访问

禁止目录让外界访问,但允许某IP访问该目录且支持PHP解析,配置如下:

在server标签内配置如下:

        location ~ ^/mysql_loging/ {
allow 192.168.0.4;
deny all;
} location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
} 说明:该配置只允许192.168.0.4IP访问mysql_loging目录

限制IP或IP段访问,配置如下:

添加在server标签内:

        location / {
deny 192.168.0.4;
allow 192.168.1.0/16;
allow 10.0.0.0/24;
deny all;
}

说明:此限制是对某些IP做整个网站的限制访问。

nginx做反向代理的时候也可以限制客户端IP,具体如下:

方法1:使用if来控制,配置如下:

if ( $remoteaddr = 10.0.0.7 ) {
return 403;
} if ( $remoteaddr = 218.247.17.130 ) {
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;
}

注意事项:

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.dmtest.com/$1 permanent;
}

方法3:发现某域名恶意解析到公司的服务器IP,在server标签里添加以下代码即可,若有多个server要多处添加。

if ($host !~ ^www/.dmtest/.com$) {
rewrite ^(.*) http://www.dmtest.com.com$1 permanent;
}

nginx站点目录及文件URL访问控制的更多相关文章

  1. [nginx]站点目录及文件访问控制

    nginx.conf配置文件 http ->多个server -> 多个location ->可限制目录和文件访问(根据i扩展名限制或者rewrite.) 根据目录或扩展名,禁止用户 ...

  2. Nginx的站点目录及文件URL的访问控制

    1.根据扩展名限制程序和文件访问: web2.0时代,绝大多数网站都是以用户为中心的,这些产品有一些共同点,就是不允许用户发布内容到服务器,还允许用户发图片甚至附件上传到服务器上,给用户开启了上传的功 ...

  3. Nginx优化之日志优化,URL访问控制,防盗链,及站点文件目录优化

    Nginx日志相关优化与安全 日志切割脚本如下: #!/bin #日志切割脚本 Date=`date +%Y%m%d` Bdir="/usr/local/nginx" Nginxl ...

  4. nginx——优化 Nginx 站点目录

    1. 禁止解析指定目录下的指定程序 location ~ ^/data/.*.(php|php5|sh|pl|py)$ { # 根据实际来禁止哪些目录下的程序,且该配置必须写在 Nginx 解析 PH ...

  5. Nginx 站点设置目录列表显示

    Nginx 站点目录列表显示. 可以编辑添加在 server { } 模块 或者 location { } 模块下. autoindex on; # 开启目录文件列表 autoindex_exact_ ...

  6. tomcat相关配置技巧梳理 (修改站点目录、多项目部署、限制ip访问、大文件上传超时等)

    tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomcat端口(可参考:分享一例测试环境下nginx+tomcat的视频业务部署记录)2)to ...

  7. nginx其他目录下上传站点

    1.查看主配置文件 [root@bogon ~]# cat /etc/nginx/nginx.conf user root root; worker_processes auto; worker_rl ...

  8. nginx+php使用open_basedir限制站点目录防止跨站

    以下三种设置方法均需要PHP版本为5.3或者以上.方法1)在Nginx配置文件中加入 fastcgi_param PHP_VALUE "open_basedir=$document_root ...

  9. Nginx+Php中限制站点目录防止跨站的配置方案记录

    Nginx+Php中限制站点目录防止跨站的配置方案记录(使用open_basedir)-------------------方法1)在Nginx配置文件中加入: 1 fastcgi_param  PH ...

随机推荐

  1. JPA @MappedSuperclass注解的使用说明(转)

    (2011-11-07 11:37:30) 转载▼ http://blog.sina.com.cn/s/blog_7085382f0100uk4p.html 标签: 杂谈   基于代码复用和模型分离的 ...

  2. python3的encode()和decode()

    python3的encode()和decode() 在python3的内存中. 在程序运行阶段. 使⽤用的是unicode编码. 因为unicode是万国码. 什么内容都可以进行显示. 那么在数据传输 ...

  3. PHP&Java 调用C#的WCF

    步骤一:用C#声明WCF [ServiceContract] public interface IService1 { [OperationContract] void DoWork(); [Oper ...

  4. linux打包文件,压缩文件

    1.打包: linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进行压缩. 1.命令格 ...

  5. 《从0到1学习Flink》—— Flink 配置文件详解

    前面文章我们已经知道 Flink 是什么东西了,安装好 Flink 后,我们再来看下安装路径下的配置文件吧. 安装目录下主要有 flink-conf.yaml 配置.日志的配置文件.zk 配置.Fli ...

  6. 使用java来压缩图片

    使用java来压缩图片,简单几句,清清爽爽 使用0.3的压缩比得到的结果如下(从2.8M压缩到268K,且图片的清晰度看不出明显差别): package carlspringtest; import ...

  7. C++拾遗(七)——关联容器

    关联容器(Associative containers)支持通过键来高效地查找和读取元素.两个基本的关联容器类型是 map 和set.map 的元素以键-值(key-value)对的形式组织:键用作元 ...

  8. (三)我的JavaScript系列:不同调用方式的this指向

    人生自是有情痴,此恨不关风与月 今天所写的内容,是对之前的内容的总结和扩展.老实说,对于自己之前的一些杜撰和臆测,我并不是很满意.所以这篇博文,我希望能来点干货. 不同调用方式的this指向 在Jav ...

  9. $'\r': command not found 或者 syntax error: unexpected end of file 或者 syntax error near unexpected token `$'\r''

    执行shell脚本如果报如下错误: syntax error near unexpected token `$'\r'' syntax error: unexpected end of file $' ...

  10. PHP生成类似类似优酷、腾讯视频等其他视频链的ID

    不知道你注意了没有,类似优酷.腾讯视频等其他视频链接似乎类似这样的 http://v.youku.com/v_show/id_XNjA5MjE5OTM2.html 注意id_xxx那段,是不是看不懂了 ...