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

发布于 2024-11-16 16:18:29 字数 466 浏览 0 评论 0原文

然而,当我只是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奈何桥上唱咆哮 2024-11-23 16:18:29

我相信它是两者的结合...你可以看出“X-Powered-By: PHP/5.3.6-6~dotdeb.1”来自 PHP,“Server: nginx”来自 NGINX。

您可以按如下方式更改 PHP 中的标头:

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

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:

<?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. 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.

口干舌燥 2024-11-23 16:18:29

大多数由 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");

老子叫无熙 2024-11-23 16:18:29

X-Powered-By 标头由 php 中的 expose_php 指令的值控制.ini:

决定 PHP 是否可以公开它安装在服务器上的事实(例如,通过将其签名添加到 Web 服务器标头)。它无论如何都不构成安全威胁,但它可以确定您是否在服务器上使用 PHP。

The X-Powered-By header is controlled by the value of the expose_php directive in php.ini:

Decides whether PHP may expose the fact that it is installed on the server (e.g. by adding its signature to the Web server header). It is no security threat in any way, but it makes it possible to determine whether you use PHP on your server or not.

通知家属抬走 2024-11-23 16:18:29

大多数标头由 nginx 发送。要列出 PHP 发送的标头,请使用函数 headers_list

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

Most headers are sent by nginx. To list the headers (to be) sent by PHP, use the function headers_list:

<?php
echo htmlentities(print_R(headers_list(), true));
?>
笑梦风尘 2024-11-23 16:18:29

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, like Connection: 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 to application/json if you're planning to have PHP send out a JSON string.

°如果伤别离去 2024-11-23 16:18:29

答案中仍然缺少的是 PHP 的作用:

一些标头确实是 PHP 本身设置的,但原因并不那么容易找到。这是默认会话缓存分隔符行为,解释如下: http://www .php.net/manual/en/function.session-cache-limiter.php

文档中没有说明如何完全关闭它们 - 只需向其传递一些未定义的值:

session_cache_limiter(false);

您必须在之前执行此操作你开始你的会话。如果您使用 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:

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.

厌味 2024-11-23 16:18:29

您还可以使用 header() 函数覆盖任何默认服务器标头。例如,如果您在 PHP header('Server: ') 中包含此内容,则会将 Server: 标头重置为空白。

You can also overwrite any of the default server headers using the header() function. For example, if you include in your PHP header('Server: ') this will reset the Server: header to be blank.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文