AWS 無法透過 Apache 設定標頭

AWS 無法透過 Apache 設定標頭

我正在嘗試從 Amazon Linux 2 AMI EC2 執行個體上的 Apache 發送 HTTP 回應標頭。我最初嘗試這樣做httpd.conf並嘗試了一些變化...

httpd.conf

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 上則不然,因此我假設該問題是特定於它的,並且想知道是否有任何我需要了解的特定於該平台的怪癖?

更多資訊

標頭模組肯定已載入。如果我運行sudo httpd -S(我必須用來sudo運行此命令,或者訪問 SSL 所需的文件時存在權限問題.pem,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)應用到它。

相關內容