HTTP_HOST与SERVER_NAME [英] HTTP_HOST vs. SERVER_NAME

查看:1757
本文介绍了HTTP_HOST与SERVER_NAME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您会考虑使用一个比其他?为什么?

When would you consider using one over the other and why?

推荐答案

HTTP_HOST 从的HTTP请求头,这就是客户实际使用作为请求的目标主机。在 SERVER_NAME 在服务器配置中定义。使用哪一个取决于你需要什么。现在应该然而认识到,一个是可因此是不可靠的商务逻辑使用一个客户控制值,另一个是一个服务器控制值这是更可靠。但是,您需要确保有问题的Web服务器有 SERVER_NAME 配置正确。以Apache的HTTPD为例,这里有来自其文档:

The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config. Which one to use depends on what you need it for. You should now however realize that the one is a client-controlled value which may thus not be reliable for use in business logic and the other is a server-controlled value which is more reliable. You however need to ensure that the webserver in question has the SERVER_NAME correctly configured. Taking Apache HTTPD as an example, here's an extract from its documentation:

如果没有服务器名称被指定,那么服务器会尝试通过对IP地址进行反向查询来推断主机名。如果没有指定端口服务器名称,那么服务器将使用的端口从传入的请求。为了达到最佳的可靠性和predictability,你应该使用服务器名称指令指定一个明确的主机名和端口。

If no ServerName is specified, then the server attempts to deduce the hostname by performing a reverse lookup on the IP address. If no port is specified in the ServerName, then the server will use the port from the incoming request. For optimal reliability and predictability, you should specify an explicit hostname and port using the ServerName directive.

更新:检查后佩卡的回答你的问题其中包含一个链接到<一个HREF =htt​​p://stackoverflow.com/a/1461430> bobince的回答的PHP将总是返回 HTTP_HOST 的价值为 SERVER_NAME ,其中前违背了我自己的PHP 4.x版+ apache httpd的1.2.x的经验,从几年,我从Windows XP(apache httpd的2.2我目前的XAMPP环境炸毁一些灰尘。 1 PHP 5.2.8),开始它,创建了打印两个值PHP页面,创建使用Java测试应用程序的<$c$c>URLConnection修改主机头和测试告诉我,这确实是(错误地)的情况。


Update: after checking the answer of Pekka on your question which contains a link to bobince's answer that PHP would always return HTTP_HOST's value for SERVER_NAME, which goes against my own PHP 4.x + Apache HTTPD 1.2.x experiences from a couple of years ago, I blew some dust from my current XAMPP environment on Windows XP (Apache HTTPD 2.2.1 with PHP 5.2.8), started it, created a PHP page which prints the both values, created a Java test application using URLConnection to modify the Host header and tests taught me that this is indeed (incorrectly) the case.

在第一个怀疑PHP和一些 PHP错误报告挖关于这个问题,我了解到,问题的根源是在Web服务器中使用,它错误地返回的HTTP 主机头时, SERVER_NAME 请求。所以我挖成 apache httpd的bug报告使用的各种关键字就这一课题,我终于找到了的related错误。因为各地的apache httpd的1.3这种行为进行了介绍。您需要设置<一个href=\"http://httpd.apache.org/docs/2.2/mod/core.html#usecanonicalname\"><$c$c>UseCanonicalName指令&LT;虚拟主机&GT; 的条目中的服务器名称的httpd.conf (也是在的文档!)。

After first suspecting PHP and digging in some PHP bug reports regarding the subject, I learned that the root of the problem is in web server used, that it incorrectly returned HTTP Host header when SERVER_NAME was requested. So I dug into Apache HTTPD bug reports using various keywords regarding the subject and I finally found a related bug. This behaviour was introduced since around Apache HTTPD 1.3. You need to set UseCanonicalName directive to on in the <VirtualHost> entry of the ServerName in httpd.conf (also check the warning at the bottom of the document!).

<VirtualHost *>
    ServerName example.com
    UseCanonicalName on
</VirtualHost> 

这为我工作。

总结 SERVER_NAME 更可靠,但你的相关在服务器配置!

Summarized, SERVER_NAME is more reliable, but you're dependent on the server config!

这篇关于HTTP_HOST与SERVER_NAME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆