如果某個 IP 存取 Apache 中的 HTTP 標頭,如何刪除它?

如果某個 IP 存取 Apache 中的 HTTP 標頭,如何刪除它?

當特定 IP 位址造訪我的網站時,如何取消設定單一/多個 HTTP 標頭?因為我的 CSP 配置阻止了某些本地頁面的正確載入。例如,如果我有 phpMyAdmin,但我無法在本機上使用它,因為設定了 CSP。

答案1

您可以使用下列設定根據 IP 位址取消設定標頭:

# For single ip address. Change the IP address to your needs
<Directory "/path/to/the/folder/which/needs/to/have/this/feature">
<If "%{REMOTE_ADDR} == '127.0.0.1'">
Header always unset HeaderName
</If>
</Directory>

# For multiple IP addresses, remove the top one, and use this one, and change the IP addresses
# to your needs, and add more '||' to use more IP addresses.
<Directory "/path/to/the/folder/which/needs/to/have/this/feature">
<If "%{REMOTE_ADDR} == '127.0.0.1' || %{REMOTE_ADDR} == '1.2.3.4'">
Header always unset HeaderName
</If>
</Directory>

確保 mod_headers 已啟用。欲了解更多詳情,請檢查https://httpd.apache.org/docs/2.4/mod/core.html#ifhttps://httpd.apache.org/docs/2.4/expr.html

您可以根據需要進行配置,但要小心,並確保您沒有為除您自己之外的所有人停用 CSP 之類的標頭。

答案2

當該Header指令不起作用時,請改用該RequestHeader指令。下面是一個範例X-Forwarded-For,當 Apache 用作反向代理時,如何防止標頭欺騙,但Header不會產生任何效果:

RequestHeader unset X-Forwarded-For
RemoteIPHeader X-Forwarded-For

相關內容