user_agent用来识别访问者的操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好的代码
比如我们的服务器网站,会被一些搜索引擎的爬虫程序访问,这对服务器压力造成了一定的影响。我们就可以根据爬虫的user_agent标示,来禁止掉它访问网站。
1、修改配置文件
[[email protected] ~]# 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、检测配置文件和重新加载配置文件
[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload
3、测试
添加不区分大小写匹配的
~*
检查配置文件和重新加载配置文件继续访问
都禁止了。