![NameVirtualHosts 和 IP 地址的配置](https://linux22.com/image/613850/NameVirtualHosts%20%E5%92%8C%20IP%20%E5%9C%B0%E5%9D%80%E7%9A%84%E9%85%8D%E7%BD%AE.png)
所以我有 site1.com 和 192.168.1.100
如果我使用 IP 地址,Apache 将无法为 site1.com 工作;如果我使用 site1.com,IP 地址将无法工作。您觉得如何?
我可以两者兼得吗?
我需要访问 IP 和 site1.com 并让它们指向不同的目录(不同的项目)
Site1.com
#NameVirtualHost site1.com:80
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/www/site1.com/current/web
ServerName site1.com
ServerAlias site1.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/site1.com/current/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/app_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/app_access.log combined
</VirtualHost>
IP地址
NameVirtualHost 192.168.1.100:80
<VirtualHost 192.168.1.100:80>
#<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/www/ip/current/
ServerName 192.168.1.100
ServerAlias 192.168.1.100
<Directory /home/www/ip/current/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/ip_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/ip_access.log combined
</VirtualHost>
更新了解决方案
Site1.com
#NameVirtualHost site1.com:80
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/www/site1.com/current/web
ServerName site1.com
ServerAlias site1.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/site1.com/current/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/app_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/app_access.log combined
</VirtualHost>
IP地址
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/www/ip/current/
ServerName 192.168.1.100
#ServerAlias 192.168.1.100
<Directory /home/www/ip/current/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/ip_error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/ip_access.log combined
</VirtualHost>
答案1
您的两个 VirtualHost 指令都需要与 NameVirtualHost 指令匹配。因此,您必须*:80
使用两个都虚拟主机和192.168.1.100:80
在名称 VirtualHost 中,或者在所有地方使用。
您也不需要设置与 相同的名称ServerName
。ServerAlias
是ServerAlias
除了 之外您想要分配给该虚拟主机的任何其他名称ServerName
。
答案2
将没有 ServerName 的虚拟主机设置为通用虚拟主机:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/www/ip/current/
<Directory>
...