laravel 簡單限制搜尋引擎爬蟲頻率

JeffreyBool發表於2022-05-27

第一步安裝爬蟲校測庫

composer require jaybizzle/laravel-crawler-detect

開啟 App\Providers\RouteServiceProvider.php 檔案

...
 protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });

        // 增加內容,檢測機器人關鍵程式碼
        RateLimiter::for('global', function (Request $request) {
            $key = $request->userAgent();
            if(Crawler::isCrawler()) {
                $key = "crawler";
            }

            // return Limit::perMinute(60)->by($key ?: $request->ip());

            return Crawler::isCrawler()
                ? Limit::perMinute(10)->by($key)
                : Limit::perMinute(100)->by($request->ip());
        });
    }
...

新增全域性中介軟體 App\Http\Kernel.php

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            //            CollapseWhitespace::class,
            'throttle:global', // 這是我們上面的中介軟體,加到 web 路由全域性中介軟體裡
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
本作品採用《CC 協議》,轉載必須註明作者和本文連結
by JeffreyBool blog :point_right: link

相關文章