首页 > 其他 > 详细

LNMP - nginx禁止指定user_agent

时间:2015-11-01 19:42:16      阅读:338      评论:0      收藏:0      [点我收藏+]

user_agent用来识别访问者的操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好的代码
比如我们的服务器网站,会被一些搜索引擎的爬虫程序访问,这对服务器压力造成了一定的影响。我们就可以根据爬虫的user_agent标示,来禁止掉它访问网站。

1、修改配置文件

[root@bogon ~]# vim /usr/local/nginx/conf/vhosts/test.conf

server
{
    listen 80;
    server_name www.test.com www.aaa.com;
    if ($host != ‘www.test.com‘)
    {
     rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }
    index index.html index.htm index.php;
    root /data/www;
    access_log /tmp/access.log combined_realip;

    #deny 127.0.0.1;
    #deny 192.168.0.0/24;

    if ($http_user_agent ~* ‘curl|baidu|111111‘)
    {
     return 403;
    }

    location ~ .*admin\.php$ {
        #auth_basic "caimz auth";
     #auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
     allow 127.0.0.1;
     deny all;
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$
    {
     access_log off;
     expires 15d;
     valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;
     if ($invalid_referer)
     {
          return 403;
     }
    }
    location ~ \.(js|css)
    {
     access_log off;
     expires 2h;    
    }
    location ~ (static|cache)
    {
     access_log off;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

技术分享

下面是不区分大小写匹配,上面是区分大小写匹配。

技术分享

2、检测配置文件和重新加载配置文件

[root@bogon ~]# /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@bogon ~]# /usr/local/nginx/sbin/nginx -s reload

3、测试

技术分享

技术分享

添加不区分大小写匹配的

~* 

检查配置文件和重新加载配置文件继续访问

技术分享都禁止了。

LNMP - nginx禁止指定user_agent

原文:http://caimengzhi.blog.51cto.com/9787265/1708410

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!