flancer32/mage2_ext_bot_sess

Magento2: 防止机器人与爬虫创建会话。

安装次数: 9398

依赖: 0

建议: 0

安全: 0

星标: 20

关注者: 4

分支: 3

开放问题: 0

类型:magento2-module

0.1.6 2020-05-26 11:40 UTC

This package is auto-updated.

Last update: 2024-09-19 22:16:19 UTC


README

在Magento中,搜索引擎的机器人与爬虫存在问题——每当机器人请求时都会创建一个新的会话。因此,当任何机器人扫描Magento页面时,会话存储中会有大量的“死会话”——每个页面请求都会创建一个新的会话。此模块阻止搜索引擎的机器人与爬虫创建会话。

另一个问题是,如果会话存储在数据库中,则Magento不会清理用户过期的会话。此模块会进行清理

配置

转到 商店 / 配置 / 一般 / 网络 / 机器人会话设置

Configuration

  • 机器人签名:所有部分将被连接成一个正则表达式"/^alexa|^blitz.io|...|yandex/i",用于查找机器人。
  • 机器人会话最大生命周期:注意,Magento定时任务每小时运行清理任务

防止会话创建

此活动独立于会话存储(redis、db、files、...),并在安装后立即可用。如果访客的HTTP头中的User-Agent通过“机器人签名”配置选项进行过滤,则Magento不会创建新的会话。

会话清理

用户会话可以存储在Redis、数据库或作为文件

此模块仅清理保存在数据库或文件系统中的会话。

数据库会话

Magento将自身会话保存在数据库中(./app/etc/env.php

['session' => 
  [
    'save' => 'db',
  ]
];

控制台命令,用于从数据库中清理机器人现有会话和用户过期会话

$ ./bin/magento fl32:botsess:clean

文件系统会话

Magento将自身会话保存在文件系统中(./app/etc/env.php

['session' => 
  [
    'save' => 'files',
  ]
];

PHP垃圾回收器清理会话(见session.gc_maxlifetime)。在此模式下,Magento无法控制会话生命周期。使用此路由清理不活跃用户的文件会话:http://your.shop.com/fl32botsess/clean/files模板以创建cron作业的shell脚本。

这是针对不良实践的不良解决方案。完全不要使用文件来存储Magento会话。

日志记录

MAGENTO_ROOT/var/log/fl32.botsess.log中查看模块活动日志。

用户代理分析

如果Magento将会话保存在数据库中,则可以在CLI清理后获取活动会话的用户代理列表

$ ./bin/magento fl32:botsess:clean
Command 'fl32:botsess:clean' is started.
1: Mozilla/5.0 (Linux; Android 8.1.0; DRA-LX5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36
...
1583: Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1
2439: Re-re Studio (+http://2re.site/)
9138: 1C+Enterprise/8.3
Total '50501' sessions are found in DB.
'0' sessions are not defined as bot's.
'0' sessions are deleted as bot's.
'170' sessions are deleted as inactive users.
'0' sessions have a failures during analyze.
'50331' sessions are belong to active users.
Command 'fl32:botsess:clean' is executed.

因此,2439: Re-re Studio (+http://2re.site/)表示名为Re-re Studio (+http://2re.site/)的用户代理在2439个已保存会话中使用过。

安装

$ cd ${DIR_MAGE_ROOT}
$ composer require flancer32/mage2_ext_bot_sess
$ bin/magento module:enable Flancer32_BotSess
$ bin/magento setup:upgrade
$ bin/magento setup:di:compile
$ bin/magento setup:static-content:deploy
$ bin/magento cache:clean
$ # set filesystem permissions to your files

卸载

您需要为 https://repo.magento.com/ 获取认证密钥以卸载任何 Magento 2 模块。请访问您的 Magento 账户,在(我的资料/市场/访问密钥)部分生成一对密钥以连接到 Magento 2 仓库。然后按照这里的说明,将 composer 认证文件 auth.json 放置在您的 composer.json 文件旁边,并将 https://repo.magento.com/ 的认证密钥放入认证文件中

{
  "http-basic": {
    "repo.magento.com": {
      "username": "...",
      "password": "..."
    }
  }
}

然后运行以下命令以完全卸载 Flancer32_BotSess 模块

$ cd ${DIR_MAGE_ROOT}   
$ bin/magento module:uninstall Flancer32_BotSess
$ composer remove flancer32/mage2_ext_bot_sess
$ bin/magento setup:upgrade
$ bin/magento setup:di:compile
$ bin/magento setup:static-content:deploy
$ bin/magento cache:clean
$ # set filesystem permissions to your files

请耐心等待,卸载过程(bin/magento module:uninstall ...)大约需要 2-4 分钟。最后,请删除 auth.json 文件

$ rm ./auth.json