AWS 无法通过 Apache 设置标头

AWS 无法通过 Apache 设置标头

我正在尝试从 Amazon Linux 2 AMI EC2 实例上的 Apache 发送 HTTP 响应标头。我最初尝试这样做,httpd.conf并尝试了一些变体...

httpd配置文件

Header always set X-Frame-Options "SAMEORIGIN"

但是标题没有设置。所以我想也许它们在后面的某个地方被重置了,但至少应该.htaccess能够设置它们,所以我尝试了几种变化...

.htaccess

Header set ApacheFoo ApacheBar
Header add Strict-Transport-Security "max-age=31104000; includeSubdomains"

但同样,这两个标题都没有设置。

但是,如果我使用 PHP 来设置标题,它可以正常工作......

索引.php:

<?php
header("phpfoo: phpbar");
die(print_r(headers_list(), 1));

我的phpfoo: phpbar标头显示在 Chrome 的 DevTools 中,但尚未设置在 Apache 中直接设置的任何标头。

.htaccess解决方案在我自己的本地 Windows+Apache 设置上运行良好,但在 AWS 上运行不佳,因此我假设该问题特定于它,并且想知道是否有任何特定于该平台的怪癖我需要了解?

更多信息

Headers 模块肯定已加载。如果我运行sudo httpd -S(我必须使用sudo来运行此命令,否则存在与访问.pemSSL 所需的文件有关的权限问题,Apache 启动正常,因此我认为如果赋予其权限,这不是问题),这是输出(主机特定信息用星号替换):

VirtualHost configuration:
*:80                   *.com (/etc/httpd/conf/httpd.conf:44)
*:443                  is a NameVirtualHost
         default server ip-*-*-*-*.us-east-2.compute.internal (/etc/httpd/conf.d/ssl.conf:56)
         port 443 namevhost ip-*-*-*-*.us-east-2.compute.internal (/etc/httpd/conf.d/ssl.conf:56)
         port 443 namevhost *.com (/etc/httpd/conf/httpd-le-ssl.conf:2)
                 alias www.*.com
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex authdigest-opaque: using_defaults
Mutex watchdog-callback: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex authdigest-client: using_defaults
Mutex lua-ivm-shm: using_defaults
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex authn-socache: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex cache-socache: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

答案1

到目前为止,我最好也是唯一的答案是:放弃 Amazon Linux 2。

在尝试设置 Nginx 反向代理并失败后,希望它可以设置标头并解决问题(Amazon Linux 2 似乎缺少make构建所需的实用程序mod_rpaf- 这是最后一根稻草),我使用 Ubuntu 创建了一个新实例并开始了漫长的设置过程我在 Amazon Linux 2 上设置了所有内容,然而在 Ubuntu 上操作相对更容易,更少混乱,并且总体来说有很多有用的教程可供使用。

最后,我最终能够将 Nginx 添加到堆栈中作为奖励,让它反向代理到 Apache,现在在 Apache 中设置标头完全没有问题。

对于遇到此问题或考虑使用 AWS 运行 Apache Web 服务器的其他人,我根据此经验给出的建议是:不要使用 Amazon Linux 2!

不仅从 Apache 设置标头似乎根本不起作用(这是启用一些不依赖于 PHP 始终工作的现代安全功能的关键),而且总体而言,使用 Ubuntu 版本一切都更加容易和灵活。Ubuntu 版本没有很好的 AWS 文档,但互联网的其余部分已经涵盖了您,您可以将 Amazon Linux 2 教程中特定于 AWS 的部分(安全组、弹性 IP 和路由 53)应用于它。

相关内容