问题描述
根据定义 HTTP/1.1 的 RFC 2616,Host:
标头是必需的.
According to RFC 2616, which defines HTTP/1.1, the Host:
header is mandatory.
客户端必须在所有 HTTP/1.1 请求消息中包含 Host 标头字段.
A client MUST include a Host header field in all HTTP/1.1 request messages .
但 PHP 手册 暗示它可能是空的:
But the PHP manual implies that it could be empty:
'HTTP_HOST':Host 的内容:当前请求的标头,如果有的话.
'HTTP_HOST': Contents of the Host: header from the current request, if there is one.
在哪些情况下,这个标头以及 $_SERVER['HTTP_HOST']
可能为空?我的应用程序能否依赖于它的存在?
In which situations could this header, and thus $_SERVER['HTTP_HOST']
, be empty? Could my application depend on its being there?
推荐答案
在 HTTP 1.0 中可以为空.如果没有指定主机头,虚拟主机将根本无法工作,因此将使用 Web 服务器中的默认虚拟主机.
It can be empty in HTTP 1.0. If no host header is specified, virtual hosting won't work at all, so the default vhost in your web server will be used.
我自己测试过这个;在 Nginx 下的 PHP 中,$_SERVER['HTTP_HOST']
变量被设置为虚拟主机的名称,在我的例子中是 _
.但这也取决于你在 Nginx 中的 fastcgi_params 配置.
I just tested this myself; in PHP under Nginx the $_SERVER['HTTP_HOST']
variable got set to the name of the virtual host, which is _
in my case. But that also depends on your fastcgi_params configuration in Nginx.
在共享主机上这并不重要,因为默认虚拟主机将设置为托管公司的某些信息页面,因此您的脚本将不会运行.不过,对于您自己的服务器来说,记住这一点可能是件好事.
On shared hosting this is not important since the default vhost will be set to some information page from the hosting company, and so your script will not be run. Could be a good thing to keep in mind for your own server though.
这篇关于什么时候可以未定义 HTTP Host 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!