我有一个网站,其中定义了虚拟主机/etc/apache2/sites-enabled/
标题设置为always
像这样的选项:
Header always set X-Frame-Options DENY
如果我现在使用设置相同的标头.htaccess
在网站的根文件夹中,但没有always
那么该标头会在服务器的响应中返回两次。
设置在.htaccess
(在其他人中):
Header set X-Frame-Options DENY
服务器的响应:
HTTP/1.1 200 OK
Date: Mon, 02 May 2016 16:02:29 GMT
Server: Apache/2.4.10 (Debian)
X-Frame-Options: DENY
Cache-Control: no-cache, no-store, must-revalidate, private
Pragma: no-cache
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Last-Modified: Mon, 02 May 2016 15:03:42 GMT
Accept-Ranges: bytes
Content-Length: 0
X-Frame-Options: DENY
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, must-revalidate, private
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Apache 文档说,如果没有always
选项的默认值onsuccess
用来。但他们也说“... onsuccess 的默认值不会将操作限制为具有 2xx 状态代码的响应...” (http://httpd.apache.org/docs/current/en/mod/mod_headers.html#header http://httpd.apache.org/docs/current/en/mod/mod_headers.html#header).
但如果我不添加always
,那么像 301s 和 404s 这样的错误页面将不会设置标头。另一方面,如果我添加always
那么如果我使用默认值(即,onsuccess
) in .htaccess
。正如文档所述:“在某些情况下,在这两个条件下重复此指令是有意义的,因为相对于现有标头,always 不是 onsuccess 的超集”。设置标头两次对于 HTTP 响应并不总是有效,请参阅https://stackoverflow.com/a/4371395/641481 https://stackoverflow.com/a/4371395/641481。所以我自然想避免它。
我现在的问题是:我到底应该什么时候使用onsuccess
(即默认值)以及何时always
?我必须承认,即使在阅读了 Apache 文档几次之后,我仍然不能完全理解这一点。实际上,似乎总是使用always
导致正确/预期的行为。
我也不明白为什么 Apache 会在设置中将标头写入两次always
and onsuccess
。对我来说这似乎是错误的,但是这肯定有一个很好的理由,因为我认为 Apache 开发人员比我更了解 HTTP ;-)