Drupal10 信任主机设置

涂红伟, 2022年12月12日 10:51:02

Drupal10安装完成后,在后台状态报告页面可以看到有一条错误提示:“信任主机设置”,未启用 settings.php中的trusted_host_patterns 设置未配置。这可能导致安全漏洞。强烈建立 您配置此项。如图所示:

Image
Drupal10 信任主机设置


根据drupal官方文档”信任主机设置“介绍,信任主机(trusted_host_patterns)是drupal的依赖组件symfony用来防止HTTP主机标头欺骗的安全机制,简单地说如果HTTP请求的主机头与drupal的settings.php文件中定义的不匹配,Drupal将使用HTTP400进行响应,并显示一条消息”提供的主机名对此服务器无效“(The provided host name is not valid for this server)。

信用主机设置方法:

向drupal站点配置文件settings.php,默认路径为”drupal站点\web\sites\default\settings.php“ 添加:

$settings['trusted_host_patterns'] = [
 '^www\.atuwe\.com$',
 '^atuwe\.com$',
];
Image
Drupal10 信任主机设置

上述代码即可将 www.atuwe.com 和 atuwe.com 域名设置为drupal的信任主机域名。

Image
Drupal10 信任主机设置

如果你的域名为其他后缀,例如 www.atuwe.net 和 atuwe.net,代码则修改为:

$settings['trusted_host_patterns'] = [
 '^www\.atuwe\.net$',
 '^atuwe\.net$',
];

站点绑定多个域名的信任主机设置,如绑定 www.atuwe.com、atuwe.com、www.abc.net、abc.net、www.def.com、def.com,代码为:

$settings['trusted_host_patterns'] = [
 '^www\.atuwe\.com$',
 '^atuwe\.com$',
 '^www\.abc\.net$',
 '^abc\.net$',
 '^www\.def\.com$',
 '^def\.com$',
];

以上为drupal10线上环境的信任主机设置方法,一般情况下本地开发环境无需配置,对于有强迫症的用户可添加以下代码:  
以本地drupal实例访问地址为 localhost和127.0.0.1为例:

$settings['trusted_host_patterns'] = [
 '^localhost$',
 '127\.0\.0\.1',
];

注:由于drupal8/9和drupal10架构相同,此配置同样适用于drupal8和drupal9。

标签