简体   繁体   English

服务器如何设置HTTP响应头?

[英]How do servers set HTTP response headers?

I sense I'm going to end up embarrassed for asking such a simple question, but I've been researching for days and can't any useful information. 我觉得我最后会因为问这么简单的问题而感到尴尬,但我已经研究了几天而且没有任何有用的信息。

What determines the HTTP response header that a server sends? 什么决定了服务器发送的HTTP响应头? If I control the server (if we need concreteness, let's say Apache), then what file can I edit to change the response header? 如果我控制服务器(如果我们需要具体性,让我们说Apache),那么我可以编辑哪个文件来更改响应头? For example, to set it to include Content-Length instead of Transfer-Encoding: chunked ? 例如,要将其设置为包含Content-Length而不是Transfer-Encoding: chunked

I'm aware that PHP and Java Servlets can be used to manipulate headers. 我知道PHP和Java Servlet可以用来操作头文件。 The existence and content of response headers is fundamental to HTTP, though, so there ought to exist a way to edit these without using outside technology, no? 响应头的存在和内容是HTTP的基础,所以应该存在一种不使用外部技术来编辑这些的方法,不是吗?

Certain headers are set automatically. 某些标题会自动设置。 They are part of the HTTP spec and the server takes care of them for you. 它们是HTTP规范的一部分,服务器会为您处理它们。 That's what a web server is for and why it differs from, say, an FTP server or a fileshare. 这就是Web服务器的用途以及它与FTP服务器或文件共享的不同之处。 For example Content-Length is easily calculated by the webserver and needs to be set so the server just does it. 例如,内容长度很容易由网络服务器计算,需要设置,以便服务器就可以完成。

Certain other headers are set based on config. 某些其他标头是基于配置设置的。 Apache usually loads a main config file (often called httpd.conf or apache2.conf) but then, to save this file getting into a big unwieldy mess it often loads other files from within that. Apache通常加载一个主配置文件(通常称为httpd.conf或apache2.conf)但是,为了保存这个文件进入一个大的笨拙的混乱,它经常加载其中的其他文件。 Those files are just text files with lines of configuration text to change behaviour of the server. 这些文件只是带有配置文本行的文本文件,用于更改服务器的行为。 Other web servers may use XML configuration files and may have a GUI to control the config (eg IIS) 其他Web服务器可能使用XML配置文件,并且可能具有GUI来控制配置(例如IIS)

So, for some of the headers, you might not explicitly set the header value but you basically configure the server and it then uses that config to figure out the appropriate headers to send. 因此,对于某些标头,您可能没有显式设置标头值,但您基本上配置了服务器,然后使用该配置来确定要发送的相应标头。 For example you can configure the server to gzip certain files (eg text files but not jpgs which are already compressed). 例如,您可以将服务器配置为gzip某些文件(例如文本文件,但不是已压缩的jpgs)。 In Apache this is handled by the mod_deflate module and the config options it gives you. 在Apache中,这由mod_deflate模块及其为您提供的配置选项处理。 Once the appropriate config is added to the server config, the server will do the necessarily processing (eg gzip the file or not depending on type) and then automatically add the headers. 将适当的配置添加到服务器配置后,服务器将执行必要的处理(例如,根据类型对文件进行gzip),然后自动添加标头。 So an Apache module is basically something that changes how the server works and this may or may not the also set headers. 因此,Apache模块基本上可以改变服务器的工作方式,这可能也可能不会设置标头。 Another example is for sending caching headers to tell the browser how long to cache files for. 另一个例子是发送缓存头以告诉浏览器缓存文件的时间。 This is controlled by adding the mod_expiries module and all the config options it allows. 这是通过添加mod_expiries模块及其允许的所有配置选项来控制的。 While some of these headers could be hardcoded (eg Cache-Control) others depend on Apache doing calculations (eg Expires) so better to use the module to do this for you based on your config. 虽然其中一些标题可能是硬编码的(例如Cache-Control),但其他标题依赖于Apache进行计算(例如Expires),因此最好根据您的配置使用模块为您执行此操作。

And finally you can explicitly set headers in your server (in Apache this is done using the mod_headers module). 最后,您可以在服务器中显式设置标头(在Apache中,这是使用mod_headers模块完成的)。 This is useful for new features added to browsers for example (eg HSTS, CSP or HPKP) where the server doesn't need to do anything but just add the header and the client (eg the web browser) knows what to do with them. 这对于添加到浏览器的新功能(例如HSTS,CSP或HPKP)很有用,其中服务器不需要做任何事情,只需添加标题,客户端(例如Web浏览器)知道如何处理它们。 You could add a JonahHuron header for example by adding this config to httpd.conf: 例如,您可以通过将此配置添加到httpd.conf来添加JonahHuron标头:

Header always set JonahHuron "Some Value"

As to whether that header is used depends entirely on the program receiving the response. 至于是否使用该标头完全取决于接收响应的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM