我们假设一个场景。
- 域名:xyz.com
- 第三方dns服务器解析的域名:ns1.xxx.com
- 域名绑定IP地址:123.123.123.123
- apache2 安装在 123.123.123.123 上
我的 /etc/httpd/conf/httpd.conf 应该是这样的:
config1:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerName 123.123.123.123:80
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot "/var/www/html"
</VirtualHost>
config2:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerName xyz.com:80
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot "/var/www/html"
</VirtualHost>
config3:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerName localhost:80
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot "/var/www/html"
</VirtualHost>
config4:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerName 127.0.0.1:80
<VirtualHost *:80>
ServerName www.xyz.com
DocumentRoot "/var/www/html"
</VirtualHost>
哪个配置文件适合我的示例?
答案1
Apache 的 ServerName 指令执行与以下相同的基本功能nginx 的 server_name 指令,也就是说,它会与Host
传入 HTTP 请求中的参数进行比较,如果它与选择的虚拟主机配置匹配。重要的是,它仅用于选择虚拟主机 - 如果网络服务器无法解析网络服务器正在侦听的网络接口的端口或 IP 地址的请求 - 即 ServerName
仅用于选择虚拟主机,如果这<VirtualHost *:80>
没有明确解析为虚拟主机/配置。
来自文档
描述:服务器用来识别自身的主机名和端口
语法:ServerName [scheme://]domain-name|ip-address[:port]
根据您的示例,ServerName 指令将在 Apache 虚拟主机配置中使用,如下所示:
<VirtualHost *:80>
ServerName xyz.com
...
...
# ...
</VirtualHost>