By admin | Wed, 06/30/2021 - 11:04

Drupal http client使用代理抓取墙外数据

$settings['http_client_config']['proxy']['http'] = 'http://206.189.127.27:8080';
$settings['http_client_config']['proxy']['https'] = 'http://206.189.127.27:8080';
$settings['http_client_config']['verify'] = false;
$settings['http_client_config']['proxy']['no'] = ['127.0.0.1', 'localhost'];

错误显示设置和调试设置

$config['devel.settings']['devel_dumper'] = 'var_dumper';
$config['system.logging']['error_level'] = 'verbose';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

proxy

$settings['reverse_proxy'] = TRUE;
$settings['reverse_proxy_addresses'] = [  $_SERVER['REMOTE_ADDR'],];

跳转

$bad_bots = [
    'YisouSpider',
    'MQQBrowser',
    'Mb2345Browser',
    'LieBaoFast',
    'UCBrowser'
];
foreach ($bad_bots as $bad_bot) {
  if (str_contains($_SERVER['HTTP_USER_AGENT'], $bad_bot)) {
    header('HTTP/1.0 403 Forbidden');
    print $bad_bot . ' Not allowed, bad visit!!';
    exit;
  }
}

屏蔽IP(这样效率更高,不要经过数据库,也不要改nginx/apache配置)

## 屏蔽IP
#
$bad_ip = [
  '194.87.68.143',
];
if (in_array(getRealUserIp(), $bad_ip)) {
  header('HTTP/1.0 403 Forbidden');
  print 'bad visit'; exit;
}

 function getRealUserIp(){
    switch(true){
      case (!empty($_SERVER['HTTP_X_REAL_IP'])) : return $_SERVER['HTTP_X_REAL_IP'];
      case (!empty($_SERVER['HTTP_CLIENT_IP'])) : return $_SERVER['HTTP_CLIENT_IP'];
      case (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) : return $_SERVER['HTTP_X_FORWARDED_FOR'];
      default : return $_SERVER['REMOTE_ADDR'];
    }
 }

强制HTTPS(Drupal 7)

$_SERVER['HTTPS'] = 'on';  
$_SERVER['SERVER_PORT'] = 443;