简体   繁体   English

这些额外的HTTP标头来自哪里?

[英]Where are these extra HTTP headers coming from?

When I simply echo something out of php file, I do not send any headers intentionally, however - there are some default headers present anyway when I look at firebug response: 当我简单地用php文件回显一些内容时,我不会故意发送任何标题,但是当我查看firebug响应时,无论如何都会出现一些默认标题:

response headers: 响应标头:

HTTP/1.1 200 OK HTTP / 1.1 200好的
Server: nginx 服务器:nginx
Date: Thu, 23 Jun 2011 19:33:51 GMT 日期:2011年6月23日星期四19:33:51 GMT
Content-Type: text/html 内容类型:text / html
Transfer-Encoding: chunked 转移编码:分块
Connection: keep-alive 连接:保持活力
Vary: Accept-Encoding 变化:接受编码
X-Powered-By: PHP/5.3.6-6~dotdeb.1 X-Powered-By:PHP / 5.3.6-6~dotdeb.1
Expires: Thu, 19 Nov 1981 08:52:00 GMT 到期日:1981年11月19日星期四08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 缓存控制:无存储,无缓存,必须重新验证,后检查= 0,预检查= 0
Pragma: no-cache Pragma:没有缓存
Content-Encoding: gzip 内容编码:gzip

I'm curious - are these default response headers set by the server(nginx) or by PHP? 我很好奇 - 这些默认响应标头是由服务器(nginx)还是由PHP设置的?

I believe it is a combination of both... You can tell that "X-Powered-By: PHP/5.3.6-6~dotdeb.1" comes from PHP and "Server: nginx" comes from NGINX. 我相信它是两者的结合......你可以说“X-Powered-By:PHP / 5.3.6-6~dotdeb.1”来自PHP,“Server:nginx”来自NGINX。

You can alter the headers in PHP as follows: 您可以在PHP中更改标头,如下所示:

<?php
    header("HTTP/1.0 404 Not Found");
?>

The gzip header most definitely comes from NGINX as it is compressing the output (html) to the browser. gzip头文件肯定来自NGINX,因为它将输出(html)压缩到浏览器。 PHP can "add" to the headers by calling a function like the one above. PHP可以通过调用上面的函数来“添加”到头文件中。 Then the server combines it with the PHP headers and serves the request. 然后,服务器将其与PHP标头组合并提供请求。

It depends on your server whether or not the PHP headers take precedence over the server headers. 这取决于您的服务器PHP标头是否优先于服务器标头。

Hope this helps. 希望这可以帮助。

The majority are set by nginx, for example the Server, Date, Content-Encoding, and Connection. 大多数是由nginx设置的,例如Server,Date,Content-Encoding和Connection。 However, some other headers are set by PHP, and you can add others in PHP like this header("Name: Value"); 但是,其他一些标题是由PHP设置的,您可以像PHP这样添加其他header("Name: Value");

The X-Powered-By header is controlled by the value of the expose_php directive in php.ini: X-Powered-By标头由php.ini中的expose_php指令的值控制:

Decides whether PHP may expose the fact that it is installed on the server (eg by adding its signature to the Web server header). 决定PHP是否可以公开它安装在服务器上的事实(例如,通过将其签名添加到Web服务器头)。 It is no security threat in any way, but it makes it possible to determine whether you use PHP on your server or not. 它无论如何都不是安全威胁,但它可以确定您是否在服务器上使用PHP。

Most headers are sent by nginx. 大多数标头都是由nginx发送的。 To list the headers (to be) sent by PHP, use the function headers_list : 要列出PHP发送的标题(要),请使用函数headers_list

<?php
echo htmlentities(print_R(headers_list(), true));
?>

PHP automatically sets some of them, like Content-Type: text/html for the hello world page. PHP会自动设置其中一些Content-Type: text/html ,例如hello world页面的Content-Type: text/html nginx sets the ones that have to do with the socket, like Connection: keep-alive . nginx设置与套接字有关的那些,比如Connection: keep-alive

You'll find settings for connections in nginx's configuration. 您将在nginx的配置中找到连接的设置。 Content-wise, it's PHP. 内容方面,它是PHP。 You're allowed to override quite a few of them with the header() function in PHP, as well as add your own custom headers. 您可以使用PHP中的header()函数覆盖其中的一些,以及添加您自己的自定义标头。 http://php.net/manual/en/function.header.php http://php.net/manual/en/function.header.php

For example, you could set the Content-Type to application/json if you're planning to have PHP send out a JSON string. 例如,如果您计划让PHP发送JSON字符串,则可以将Content-Type设置为application/json

You can also overwrite any of the default server headers using the header() function. 您还可以使用header()函数覆盖任何默认服务器标header() For example, if you include in your PHP header('Server: ') this will reset the Server: header to be blank. 例如,如果您在PHP header('Server: ')包含header('Server: ')则会将Server:标头重置为空白。

What's still missing in the answers is the role of PHP: 答案中仍然缺少的是PHP的作用:

Some of the headers are indeed set by PHP itself, but the reason is not that easy to find. 有些标题确实是由PHP本身设置的,但原因并不容易找到。 It's the default session cache delimiter behavior explained here: http://www.php.net/manual/en/function.session-cache-limiter.php 这是此处解释的默认会话缓存定界符行为: http//www.php.net/manual/en/function.session-cache-limiter.php

What's afaik not in the docs is how to turn them off completely - simply pass some undefined value to it: 文档中没有的是如何完全关闭它们 - 只需将一些未定义的值传递给它:

session_cache_limiter(false);

You must to do this before you start your session. 您必须在开始会话之前执行此操作。 In case you are using the Zend Framework, you have to set this before your applications bootstrap() - otherwise it won't work. 如果您使用的是Zend Framework,则必须在应用程序bootstrap()之前设置它 - 否则它将无法工作。

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

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