First, I would like to say that the articles on Baidu are all nonsense!
There are some similarities and differences, but they do not explain the reasons. If they are not verified, they will be transferred to each other, wasting the audience time.
The following is a summary of my personal verification + official documents + official BUG list + official email list + sitepoint + webmasterworld + google:
Similarities:
When the following three conditions are met, the two will output the same information.
1. The server is port 80.
2. Set ServerName correctly in apache conf.
3. HTTP/1.1 Protocol specification
Differences:
1. General situation:
_ SERVER ["HTTP_HOST"] under the HTTP/1.1 protocol specification, information is output according to the client's HTTP request.
_ SERVER ["SERVER_NAME"] by default, the ServerName value in the configuration file httpd. conf of apache is directly output.
2. When the server is not port 80:
_ SERVER ["HTTP_HOST"] outputs the port number, for example, 111cn.net: 8080.
_ SERVER ["SERVER_NAME"] directly outputs the ServerName value
Therefore, it can be understood as: HTTP_HOST = SERVER_NAME: SERVER_PORT
3. When the ServerName in the configuration file httpd. conf is inconsistent with the domain name of the HTTP/1.0 request:
Httpd. conf is configured as follows:
The code is as follows: |
Copy code |
<Virtualhost *> ServerName 111cn.net ServerAlias www.111cn.net </Virtualhost> Client Access domain name www.111cn.net _ SERVER ["HTTP_HOST"] outputs www.111cn.net _ SERVER ["SERVER_NAME"] outputs 111cn.net |
Therefore, in actual programs, _ SERVER ["HTTP_HOST"] should be used as much as possible, which is relatively safe and reliable.