这些额外的 HTTP 标头从哪里来?
然而,当我只是从 php 文件中回显某些内容时,我不会故意发送任何标头 - 当我查看 firebug 响应时,无论如何都会存在一些默认标头:
响应标头:
HTTP/1.1 200 正常
服务器:nginx
日期:2011 年 6 月 23 日星期四 19:33:51 GMT
内容类型:text/html
传输编码:分块
连接:保持活动
变化:接受编码
X-Powered-By: PHP/5.3.6-6~dotdeb.1
到期时间:1981 年 11 月 19 日星期四 08:52:00 GMT
缓存控制:无存储、无缓存、必须重新验证、后检查=0、预检查=0
编译指示:无缓存
内容编码:gzip
我很好奇 - 这些默认响应标头是由服务器(nginx)还是由 PHP 设置的?
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:
response headers:
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 23 Jun 2011 19:33:51 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.6-6~dotdeb.1
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
I'm curious - are these default response headers set by the server(nginx) or by PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信它是两者的结合...你可以看出“X-Powered-By: PHP/5.3.6-6~dotdeb.1”来自 PHP,“Server: nginx”来自 NGINX。
您可以按如下方式更改 PHP 中的标头:
gzip 标头肯定来自 NGINX,因为它将输出 (html) 压缩到浏览器。 PHP 可以通过调用像上面这样的函数来“添加”到标头。然后服务器将其与 PHP 标头结合起来并处理请求。
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.
You can alter the headers in PHP as follows:
The gzip header most definitely comes from NGINX as it is compressing the output (html) to the browser. PHP can "add" to the headers by calling a function like the one above. Then the server combines it with the PHP headers and serves the request.
It depends on your server whether or not the PHP headers take precedence over the server headers.
Hope this helps.
大多数由 nginx 设置,例如服务器、日期、内容编码和连接。但是,其他一些标头是由 PHP 设置的,您可以在 PHP 中添加其他标头,如下所示
header("Name: Value");
The majority are set by nginx, for example the Server, Date, Content-Encoding, and Connection. However, some other headers are set by PHP, and you can add others in PHP like this
header("Name: Value");
X-Powered-By
标头由 php 中的 expose_php 指令的值控制.ini:The
X-Powered-By
header is controlled by the value of the expose_php directive in php.ini:大多数标头由 nginx 发送。要列出 PHP 发送的标头,请使用函数
headers_list
:
Most headers are sent by nginx. To list the headers (to be) sent by PHP, use the function
headers_list
:PHP 会自动设置其中一些,例如 hello world 页面的
Content-Type: text/html
。 nginx 设置与套接字有关的设置,例如Connection: keep-alive
。您将在 nginx 配置中找到连接设置。从内容来看,它是 PHP。您可以使用 PHP 中的
header()
函数覆盖其中的相当一部分,以及添加您自己的自定义标头。http://php.net/manual/en/function.header.php
例如,如果您计划让 PHP 发送 JSON 字符串,则可以将
Content-Type
设置为application/json
。PHP automatically sets some of them, like
Content-Type: text/html
for the hello world page. nginx sets the ones that have to do with the socket, likeConnection: keep-alive
.You'll find settings for connections in nginx's configuration. Content-wise, it's 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.http://php.net/manual/en/function.header.php
For example, you could set the
Content-Type
toapplication/json
if you're planning to have PHP send out a JSON string.答案中仍然缺少的是 PHP 的作用:
一些标头确实是 PHP 本身设置的,但原因并不那么容易找到。这是默认会话缓存分隔符行为,解释如下: http://www .php.net/manual/en/function.session-cache-limiter.php
文档中没有说明如何完全关闭它们 - 只需向其传递一些未定义的值:
您必须在之前执行此操作你开始你的会话。如果您使用 Zend Framework,则必须在应用程序 bootstrap() 之前设置此项 - 否则它将无法工作。
What's still missing in the answers is the role of PHP:
Some of the headers are indeed set by PHP itself, but the reason is not that easy to find. It's the default session cache delimiter behavior explained here: 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:
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.
您还可以使用
header()
函数覆盖任何默认服务器标头。例如,如果您在 PHPheader('Server: ')
中包含此内容,则会将 Server: 标头重置为空白。You can also overwrite any of the default server headers using the
header()
function. For example, if you include in your PHPheader('Server: ')
this will reset the Server: header to be blank.