什么时候可以未定义 HTTP Host 标头?

When could the HTTP Host header be undefined?(什么时候可以未定义 HTTP Host 标头?)
本文介绍了什么时候可以未定义 HTTP Host 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据定义 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 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)