2013-10-12 10 views
8

vorrei bloccare tutti i http_user_agents che identificano come i bot, ma consentire Googlebot quando ho messo il seguente codice:blocco tutti i bot ad eccezione di pochi con Nginx

map $http_user_agent $bad_bot { 
default 1; 
~*^Lynx 0; # Let Lynx go through 
~*^google); 
libwww-perl      1; 
~(?i)(libwww|Wget|LWP::Simple|BBBike|java|crawl|spider|bot) 1; 
} 

tuttavia, questo blocco l'accesso a Googlebot ancora.

risposta

14

Basta controllare $http_user_agent contro la tua lista bad_bot $ e tornare HTTP 403 se è nella tua lista nera:

location/{ 
    if ($http_user_agent ~ (libwww|Wget|LWP|damnBot|BBBike|java|spider|crawl)) { 
     return 403; 
    } 
} 

Nota:~ nel if block esegue corrispondenza case-sensitive. Se vuoi rendere la tua lista nera senza distinzione tra maiuscole e minuscole, usa ~* invece di ~.

20

Ecco la mia logica per nginx

map $http_user_agent $limit_bots { 
    default 0; 
    ~*(google|bing|yandex|msnbot) 1; 
    ~*(AltaVista|Googlebot|Slurp|BlackWidow|Bot|ChinaClaw|Custo|DISCo|Download|Demon|eCatch|EirGrabber|EmailSiphon|EmailWolf|SuperHTTP|Surfbot|WebWhacker) 1; 
    ~*(Express|WebPictures|ExtractorPro|EyeNetIE|FlashGet|GetRight|GetWeb!|Go!Zilla|Go-Ahead-Got-It|GrabNet|Grafula|HMView|Go!Zilla|Go-Ahead-Got-It) 1; 
    ~*(rafula|HMView|HTTrack|Stripper|Sucker|Indy|InterGET|Ninja|JetCar|Spider|larbin|LeechFTP|Downloader|tool|Navroad|NearSite|NetAnts|tAkeOut|WWWOFFLE) 1; 
    ~*(GrabNet|NetSpider|Vampire|NetZIP|Octopus|Offline|PageGrabber|Foto|pavuk|pcBrowser|RealDownload|ReGet|SiteSnagger|SmartDownload|SuperBot|WebSpider) 1; 
    ~*(Teleport|VoidEYE|Collector|WebAuto|WebCopier|WebFetch|WebGo|WebLeacher|WebReaper|WebSauger|eXtractor|Quester|WebStripper|WebZIP|Wget|Widow|Zeus) 1; 
    ~*(Twengabot|htmlparser|libwww|Python|perl|urllib|scan|Curl|email|PycURL|Pyth|PyQ|WebCollector|WebCopy|webcraw) 1; 
} 

location/{ 
    if ($limit_bots = 1) { 
    return 403; 
    } 
} 
+7

persone devono tenere presente che questo metodo consente di disattivare Google di indicizzare il tuo sito web tramite cattura la parola "google" e la parola "bot" (perché HTTP_USER_AGENT di Google ha avuto la parola bot – bmiskie

+0

@bmiskie Un buon chiarimento, grazie. Nel mio caso, è esattamente quello che voglio fare! – emc