[LNMP]Nginx防盗链与访问控制

本文涉及的产品
访问控制,不限时长
简介:

防盗链

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@plinuxos ~] # vi /usr/local/nginx/conf/vhost/default.conf 
server
{
     listen 80 default_server;  
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log juispan;
     location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
     {
         expires 7d;
         valid_referers none blocked server_names  *.aaa.com ;
         if  ($invalid_referer) {
             return  403;
         }
     access_log off;
     }
}

2、检查与重载

1
2
3
4
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration  file  /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration  file  /usr/local/nginx/conf/nginx .conf  test  is successful
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@plinuxos ~] # curl -x127.0.0.1:80 aaa.com/pic001.gif -I
HTTP /1 .1 200 OK
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 21:51:35 GMT
Content-Type: image /gif
Content-Length: 66698
Last-Modified: Sat, 12 Aug 2017 03:29:18 GMT
Connection: keep-alive
ETag:  "598e760e-1048a"
Expires: Mon, 21 Aug 2017 21:51:35 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
 
[root@plinuxos ~] # curl -e "http://www.hao123.com" -x127.0.0.1:80 aaa.com/pic001.gif -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 21:52:18 GMT
Content-Type: text /html
Content-Length: 169
Connection: keep-alive


访问控制

限制目录

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@plinuxos ~] # vi /usr/local/nginx/conf/vhost/default.conf 
server
{
     listen 80 default_server;  
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log juispan;
     location  /admin/
     {
         allow 127.0.0.1;
         deny all;
     }
}

2、检查与重载

1
2
3
4
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration  file  /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration  file  /usr/local/nginx/conf/nginx .conf  test  is successful
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@plinuxos ~] # mkdir /data/wwwroot/default/admin
[root@plinuxos ~] # echo "test" > /data/wwwroot/default/admin/1.html
[root@plinuxos ~] # curl -x127.0.0.1:80 aaa.com/admin/1.html -I
HTTP /1 .1 200 OK
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 22:13:08 GMT
Content-Type: text /html
Content-Length: 5
Last-Modified: Mon, 14 Aug 2017 22:03:03 GMT
Connection: keep-alive
ETag:  "59921e17-5"
Accept-Ranges: bytes
 
[root@plinuxos ~] # curl -x122.112.253.88:80 aaa.com/admin/1.html -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 22:13:13 GMT
Content-Type: text /html
Content-Length: 169
Connection: keep-alive


限制文件

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@plinuxos ~] # vi /usr/local/nginx/conf/vhost/default.conf 
server
{
     listen 80 default_server;  
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log juispan;
     location ~ .*(upload|image)/.*\.php$
     {
         deny all;
     }
}

2、检查与重载

1
2
3
4
5
6
[root@plinuxos ~] # mkdir /data/wwwroot/default/upload
[root@plinuxos ~] # echo "test" > /data/wwwroot/default/upload/1.php
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration  file  /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration  file  /usr/local/nginx/conf/nginx .conf  test  is successful
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
[root@plinuxos ~] # curl -x127.0.0.1:80 aaa.com/upload/1.php -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 22:19:25 GMT
Content-Type: text /html
Content-Length: 169
Connection: keep-alive


限制user-agent

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@plinuxos ~] # vi /usr/local/nginx/conf/vhost/default.conf 
server
{
     listen 80 default_server;  
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log juispan;
     if  ($http_user_agent ~*  'Spider/3.0|YoudaoBot|Tomato' ##星号忽略大小写
     {
          return  403;
     }
}

2、检查与重载

1
2
3
4
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -t
nginx: the configuration  file  /usr/local/nginx/conf/nginx .conf syntax is ok
nginx: configuration  file  /usr/local/nginx/conf/nginx .conf  test  is successful
[root@plinuxos ~] # /usr/local/nginx/sbin/nginx -s reload

3、测试效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@plinuxos ~] # curl -A "apple" -x127.0.0.1:80 aaa.com/upload/1.php -I
HTTP /1 .1 200 OK
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 22:31:09 GMT
Content-Type: application /octet-stream
Content-Length: 5
Last-Modified: Mon, 14 Aug 2017 22:17:17 GMT
Connection: keep-alive
ETag:  "5992216d-5"
Accept-Ranges: bytes
 
[root@plinuxos ~] # curl -A "tomato" -x127.0.0.1:80 aaa.com/upload/1.php -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .12.1
Date: Mon, 14 Aug 2017 22:30:26 GMT
Content-Type: text /html
Content-Length: 169
Connection: keep-alive











本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1956278 ,如需转载请自行联系原作者
相关实践学习
云安全基础课 - 访问控制概述
课程大纲 课程目标和内容介绍视频时长 访问控制概述视频时长 身份标识和认证技术视频时长 授权机制视频时长 访问控制的常见攻击视频时长
相关文章
|
2月前
|
运维 应用服务中间件 Shell
LNMP详解(十六)——Nginx日志切割
LNMP详解(十六)——Nginx日志切割
40 5
|
2月前
|
运维 应用服务中间件 Linux
LNMP详解(十三)——Nginx子页面详解
LNMP详解(十三)——Nginx子页面详解
30 3
|
2月前
|
Web App开发 编解码 运维
LNMP详解(十二)——Nginx URL重写实战
LNMP详解(十二)——Nginx URL重写实战
32 2
|
2月前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
97 2
|
2月前
|
运维 监控 应用服务中间件
LNMP详解(十五)——Nginx日志分析实战
LNMP详解(十五)——Nginx日志分析实战
47 0
|
2月前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
60 0
|
2月前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
90 2
|
2月前
|
缓存 运维 前端开发
LNMP详解(十)——Nginx负载分担实战
LNMP详解(十)——Nginx负载分担实战
26 1
|
2月前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
37 1
|
23天前
|
安全 Ubuntu 应用服务中间件
NGINX环境下实现Web网站访问控制的实战指南
在NGINX中设置基于IP的访问控制可提升网站安全性。步骤包括安装NGINX、备份配置文件、编辑`/etc/nginx/sites-available/default`,添加`allow`和`deny`指令限制特定IP访问,如`allow 192.168.1.100; deny all;`,然后测试配置并重启服务。成功后,仅允许的IP能访问网站,否则会收到403错误。这为Web安全提供基础保障,还可扩展实现更多高级控制策略。【6月更文挑战第20天】
86 3