对于做国内网站来说,我不希望国外蜘蛛来访问我的网站,这些垃圾流量多了之后,严重浪费服务器的带宽和资源。通过判断user agent,在nginx中禁用这些蜘蛛可以节省一些流量,也可以防止一些恶意的访问。

解决方案
修改nginx.conf,禁止网络爬虫的user_agent,返回403。

1、 进入nginx的配置目录,例如cd /usr/local/nginx/conf
2、添加agent_deny.conf配置文件 vim agent_deny.conf

#禁止Scrapy等爬虫工具的抓取 Baiduspider
if ($http_user_agent ~* "Scrapy|Sogou web spider") {
  return 403;
}

#禁止指定UA及UA为空的访问
if ($http_user_agent ~ "Scrapy|SemrushBot|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;
}

在server层的网站配置中include这个配置文件进来,nginx -t 测试配置格式是否通过,再重启nginx服务

然后测试一下
用curl命令模拟浏览器头信息, 设置是否成功,curl的-A 可以让我们随意指定自己这次访问所宣称的自己的浏览器信息

#curl -I -A “BaiduSpider” www.test.com

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 09 Feb 2022 09: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 2022 09: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文件。

https://stackoverflow.com/questions/47374678/how-to-detect-ecmascript-version

var ecmaScriptInfo = (function() {
                  // () => { is not allowed
  function getESEdition() {
    var array = [];
    switch (true) {
      case !Array.isArray:
        return 3;
      case !window.Promise:
        return 5;
      case !array.includes:
        return 6;
      case !''.padStart:
        return 7;
      case !Promise.prototype.finally:
        return 8;
      case !window.BigInt:
        return 9;
      case !Promise.allSettled:
        return 10;
      case !''.replaceAll:
        return 11;
      case !array.at:
        return 12;
      default:
        return 13;
    }
  }

  function getESYear(edition) {
    return {
      3: 1999,
      5: 2009
    }[edition] || (2009 + edition); // nullish coalescing (??) is not allowed
  }
  
  var edition = getESEdition();
  var year = getESYear(edition);

  return {
    edition: edition, // usually shortened [edition,]
    year: year,       // usually shortened [year,]
    text: 'Edition: '+ edition +' | Year: '+ year
       // `Edition: ${edition} | Year: ${year}` is not allowed
  }
})();

console.log(ecmaScriptInfo.edition);
console.log(ecmaScriptInfo.year);
console.log(ecmaScriptInfo.text);

我在不知道这个方法之前都是这样用:

var ids = [];
$("[name='checkStatus[]']:checked").each(function (i, v) {
                    ids[]= $(v).val()
                })
ids = ids.get()
idsStr = ids.join(',')

现在可以这样写了:

var ids = $("[name='checkStatus[]']:checked").map(function (i, v) {
                    return $(v).val()
                }).get()
//[1,2]
idsStr = ids.join(',')
//'1,2'

出现报错首先更新acme程序,命令:

acme.sh --upgrade

如果更新到最新还是申请不了,查看以下解决方法:

出现 Sleeping for 10 seconds and retrying 循环提示:
删除.acme.sh/ca 目录

出现手动DNS申请证书,无法验证域名有效性
检查是否以前在这个服务器申请过证书,并且使用了指定的--server服务器,这次如果未指明--server就会原用上次保存过的,如果这个服务地址现在无法使用了,可能导致验证不成功问题
解决方法是删除.acme.sh/目录下 相关的域名证书申请目录,以及相关的服务器地址配置(不确定,如果有的话),再重新DNS下单申请试试

申请多域名证书报错:
如果使用 -d www.xx.com,www.xx.cn 的命令,--renew 时出现:

 'www.xx.com,www.xx.cn' is not an issued domain, skipping

解决方法是把域名拆开:

acme.sh --renew -d www.xx.com -d www.xx.cn  [...]