对于做国内站的我来说,我不希望国外蜘蛛来访问我的网站,特别是个别垃圾蜘蛛,它们访问特别频繁。这些垃圾流量多了之后,严重浪费服务器的带宽和资源。通过判断user agent,在nginx中禁用这些蜘蛛可以节省一些流量,也可以防止一些恶意的访问。
方法一:修改nginx.conf,禁止网络爬虫的user_agent,返回403。
1、进入nginx的配置目录,例如cd /usr/local/nginx/conf
2、添加agent_deny.conf配置文件 vim agent_deny.conf
server层加入以下内容:
#禁止Scrapy等爬虫工具的抓取 if ($http_user_agent ~* "Scrapy|Sogou web spider|Baiduspider") { return 403; } #禁止指定UA及UA为空的访问 if ($http_user_agent ~ "FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|YisouSpider|HttpClient|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms|^$" ) { return 403; } #禁止非GET|HEAD|POST方式的抓取 if ($request_method !~ ^(GET|HEAD|POST)$) { return 403; }
还有加一些针对特殊的user_agent的访问
if ($http_user_agent ~ "Mozilla/4.0\ \(compatible;\ MSIE\ 6.0;\ Windows\ NT\ 5.1;\ SV1;\ .NET\ CLR\ 1.1.4322;\ .NET\ CLR\ 2.0.50727\)") { return 404; }
然后测试一下 设置是否成功,curl的-A 可以让我们随意指定自己这次访问所宣称的自己的浏览器信息
#curl -I -A "BaiduSpider" www.test.com (模拟浏览器头信息)
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 09 Feb 2015 03:37:20 GMT
Content-Type: text/html; charset=UTF-8 Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.19 Vary: Accept-Encoding, Cookie
Cache-Control: max-age=3, must-revalidate
WP-Super-Cache: Served supercache file from PHP
#curl -I -A "JikeSpider" www.test.com
HTTP/1.1 403 Forbidden
Server: nginx Date: Mon, 09 Feb 2015 03:37:44 GMT
Content-Type: text/html
Content-Length: 162 Connection: keep-alive
到这里,nginx通过判断User-Agent屏蔽蜘蛛访问网站就已经完成,可以根据实际情况对agent_deny.conf中的蜘蛛进行增加、删除或者修改。
方法2:网站更目录下增加Robots.txt,放在站点根目录下。
在http://tool.chinaz.com/robots/站点可以针对现在的搜索引擎按照想要的规则生成robots.txt文件。
知识扩展:
robots.txt是搜索引擎中访问网站的时候要查看的第一个文件。robots.txt文件告诉蜘蛛程序在服务器上什么文件是可以被查看的。
-----------------------------------------------------------------------------------------------------------------------------
Nginx也可实现根据访问源的设备类型进行判断并跳转到不同的tomcat或其它项目中
vim /usr/local/nginx/conf/conf.d/mobile.conf
upstream mobileserver { server 10.0.10.48:8089 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.49:8089 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.50:8089 max_fails=3 fail_timeout=60 weight=1; } upstream computerserver { server 10.0.10.48:8080 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.49:8080 max_fails=3 fail_timeout=60 weight=1; server 10.0.10.50:8080 max_fails=3 fail_timeout=60 weight=1; } server { listen 80; server_name house.wjoyxt.com; rewrite_log on; if ($request_uri ~ " ") { return 444; } location / { #以下三行为重新定义或者添加发往后端服务器的请求头,nginx会在把请求转向后台real-server前把http报头中的ip地址进行替换(在使用反向代理时经常用,目的是为了使后端服务器获取客户端的真实IP地址) proxy_set_header Host $host; #如果不想改变请求头“Host”的值,可以这样来设置:proxy_set_header Host $http_host;但是,如果客户端请求头中没有携带这个头部,那么传递到后端服务器的请求也不含这个头部。这种情况下,更好的方式是使用$host变量——它的值在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名 proxy_set_header X-Real-IP $remote_addr; #把真实的客户端ip发送给后端的web服务器 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #把真实的客户端ip发送给后端的web服务器
access_log /data/logs/nginx/mobile.access.log main; error_log /data/logs/nginx/mobile.error.log; set $client ""; #如果是IPhone设备、iPad设备、iPod设备、苹果其它非PC设备、苹果PC设备 if ( $http_user_agent ~* "(iPhone|iPad|iPod|iOS|Android|Windows Phone|Symbian)") { set $client "1"; } if ($client = '1') { proxy_pass http://mobileserver; break; } if (!-f $request_filename) { proxy_pass http://computerserver; break; } } location ~ \.php$ { proxy_pass http://127.0.0.1; } location ~* \.(html|shtml|htm|inc|log)$ { expires 1m; } location ~* ^.+\.(jpg|jpeg|gif|swf|mpeg|mpg|mov|flv|asf|wmv|avi|ico)$ { expires 15d; } }
相关推荐
nginx判读user_agent并转发请求配置文件
nginx_tcp_proxy_module-master.zip
介绍nginx的命令try_file实现本地资源不存在时就跳转到其他流程回源
nginx_limit_access_module - 可通过指定的 HTTP POST 接口中的值来拒绝请求。 示例配置: http { limit_access_zone zone=one:5m bucket_number=10007 type=ip; server { listen 80; server_name ...
nginx_tcp_proxy_module-master 是nginx的TCP模块
以下代码写入 server {….} 内 代码如下: if ($http_user_agent ~ ^$) { return 503; } if ($http_user_agent ~* “Mozilla/4.0\... 您可能感兴趣的文章:Nginx中配置过滤爬虫的User-Agent的简单方法Nginx防盗链根据UA屏
nginx_upstream_check_module模块,2019年11月6日18:11:19下载的
Nginx所需环境的安装,及nginx安装,nginx相关功能开启,后端节点健康检查插件安装,静态资源缓存插件,缩略图插件安装等。
nginx_upstream_check_module-master,淘宝技术团队开发的,适用1.11
ngx_cache_purge 是 nginx 模块,此模块可以清理 nginx 的 FastCGI、proxy、 SCGI 和 uWSGI 的缓存。配置指令(相同位置语法)fastcgi_cache_purgesyntax: fastcgi_cache_purge on|off|<method> [from all|<ip> [.....
nginx_1.7.11.3_Gryphon主要用于媒体直播服务器用,和FFmpeg+nginx+实例视频
nginx_http_push_module 基于nginx的web push实现。
nginx后端节点健康检查模块插件,非常好用,淘宝开发。
nginx优化之request_time 和upstream_response_time差别笔者在根据nginx的accesslog中$request_time
官方nginx 镜像不带主动健康,本镜像将 nginx_upstream_check健康检查 打包到了镜像中。
包含yamdi-1.9.tar.gz nginx_mod_h264_streaming-2.2.7.tar.gz nginx-1.8.1
linux nginx nginx_upstream_jvm_route
适用于windows,于2020年11月27日编译,已编译nginx-http-flv-module直播推流模块,感谢@winshining在github上的开源项目。
ngx_sqlite 是嵌入 sqlite 数据库的 nginx 模块。通过强大的nginx server,可以使用http协议访问sqlite数据库。环境- sqlite 3- nginx-1.6.3 安装```sh $ git clone https://github.com/rryqszq4/ngx_sqlite.git...