Apache:“始终设置标头"之间的区别和“标题集"? [英] Apache: difference between "Header always set" and "Header set"?

查看:23
本文介绍了Apache:“始终设置标头"之间的区别和“标题集"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. Apache 中Header always setHeader set 有什么区别?
  2. 也就是说,always 关键字在设置标头的情况下会发生什么变化?
  3. 我应该总是使用 always 设置我的标题吗?
  4. 有什么理由不这样做吗?
  1. What is the difference between Header always set and Header set in Apache?
  2. That is, what does the always keyword change about the circumstances under which the header is set?
  3. Should I always set my headers using always?
  4. Is there any reason not to?

背景

我见过...

Header always set X-Frame-Options DENY

...以及...

Header always set Access-Control-Allow-Headers "*"

...而且我有时听说 always 关键字的存在确保正确设置标题,或者最好在其中包含 always 关键字一般的.但是,对于为什么会出现这种情况,我一直没有找到明确、明确的答案.

...and I sometimes hear that the presence of the always keyword ensures that the header is properly set, or that it's simply better to include the always keyword in general. However, I have never found a clear, definitive answer for why that is the case.

我已经检查了 Apache docs for mod_headers,这里只是简单地提到了always:

当您的操作是现有标头的函数时,您可能需要指定条件 always,具体取决于原始标头设置在哪个内部表中.对应于 always 的表用于本地生成的错误响应,如以及成功的回应.另请注意,在某些情况下,在两个条件下重复此指令是有意义的,因为对于现有标头而言,始终不是 onsuccess 的超集:

When your action is a function of an existing header, you may need to specify a condition of always, depending on which internal table the original header was set in. The table that corresponds to always is used for locally generated error responses as well as successful responses. Note also that repeating this directive with both conditions makes sense in some scenarios because always is not a superset of onsuccess with respect to existing headers:

  • 您要向本地生成的非成功(非 2xx)响应添加标头,例如重定向,在这种情况下,最终响应中仅使用与 always 对应的表.
  • 您正在修改或删除由 CGI 脚本生成的标头,在这种情况下,CGI 脚本位于与 always 相对应的表中,而不是在默认表中.
  • 您正在修改或删除由服务器的某些部分生成的标头,但默认的 onsuccess 条件未找到该标头.

据我所知,这意味着 Header set always 确保即使在非 200 页上也设置了标题.但是,我使用 Header set 设置的 HTTP 标头似乎总是适用于我的 404 页面等.我在这里误解了什么吗?

As far as I can tell, this means that Header set always ensures that the header is set even on non-200 pages. However, my HTTP headers set with Header set have always seemed to apply just fine on my 404 pages and such. Am I misunderstanding something here?

FWIW,我发现像 总是"和总是"之间的区别是什么?和成功"在 Apache 的 Header 配置中?,但唯一的答案并没有真正为我解释清楚.

FWIW, I've found SO posts like What is the difference between "always" and "onsuccess" in Apache's Header config?, but the only answer there didn't really explain it clearly for me.

非常感谢,
迦勒

Thanks very much,
Caleb

推荐答案

Header always set 和 Apache 中的 Header set 有什么区别?

What is the difference between Header always set and Header set in Apache?

正如手册中引用的部分所说,如果没有总是",您的添加只会得到成功的响应.

As the quoted bit from the manual says, without 'always' your additions will only go out on succesful responses.

但这也包括通过 mod_proxy 和其他类似的处理程序成功地"转发错误,这些处理程序的行为大致类似于代理.是什么导致您发现不同意手册的 404?本地文件上的 404 肯定会像引用的位描述的那样运行.

But this also includes "successfully" forward errors via mod_proxy and perhaps other similar handlers that roughly act like proxies. What generates your 404s that you found to disagree with the manual? A 404 on a local file certainly behaves as the quoted bit describes.

也就是说,对于设置header的情况,always关键字有什么变化?

That is, what does the always keyword change about the circumstances under which the header is set?

Apache 的 API 保留了两个与每个请求相关联的列表,headers 和 err_headers.如果服务器在处理请求时遇到错误,则不使用前者.

Apache's API keeps two lists associated with each request, headers and err_headers. The former is not used if the server encounters an error processing the request the latter is.

我应该总是使用 always 设置我的标题吗?

Should I always set my headers using always?

这取决于它们的重要性.假设您正在设置与您期望为某些资源服务的内容相关的 Cache-Control 标头.现在假设您实际上正在提供 400 或 502 之类的服务.您可能不希望缓存它!

It depends on their significance. Let's say you were setting Cache-Control headers that were related to what you had expected to serve for some resource. Now let's say you were actually serving something like a 400 or 502. You might not want that cached!

有什么理由不这样做吗?

Is there any reason not to?

见上文.

-/-

您没有引用的手册中还有一点解释了错误代码的代理或 CGI,但没有解释 Apache 为其生成错误响应的代码:

There is also a bit in the manual you did not quote which explains the proxy or CGI of an error code but not for one which Apache is generating an error response for:

可选的条件参数决定了哪个内部表该指令将针对的响应标头.尽管名称, onsuccess 的默认值不限制操作为带有 2xx 状态代码的响应.

The optional condition argument determines which internal table of responses headers this directive will operate against. Despite the name, the default value of onsuccess does not limit an action to responses with a 2xx status code.

在此条件下设置的标头仍会在以下情况下使用,例如,请求被 CGI 成功代理或生成,即使它们已生成失败状态代码.

Headers set under this condition are still used when, for example, a request is successfully proxied or generated by CGI, even when they have generated a failing status code.

这篇关于Apache:“始终设置标头"之间的区别和“标题集"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆