![Apache 服务器:IP 地址路由到域而不是 DocumentRoot](https://linux22.com/image/643507/Apache%20%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%9AIP%20%E5%9C%B0%E5%9D%80%E8%B7%AF%E7%94%B1%E5%88%B0%E5%9F%9F%E8%80%8C%E4%B8%8D%E6%98%AF%20DocumentRoot.png)
我的服务器上只有一个网站。我希望在 Web 浏览器中使用 IP 地址作为 URL 时打开不同的目录。但是,即使我将 DocumentRoot 设置为不同的目录,IP 地址仍会打开我的 domain.com。
如果我删除该 domain.com 的 .conf 文件,则 IP 地址将按预期打开 DocumentRoot。
这是我的配置:
conf.d/domain.com.conf 文件
<VirtualHost *:80>
DocumentRoot /var/www/html/domaincom
ServerName domain.com
ServerAlias www.domain.com
<Directory "/var/www/html/domaincom">
AllowOverride All
</Directory>
</VirtualHost>
conf/httpd.conf 文件
ServerTokens OS
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 60
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
Listen 80
LoadModule auth_basic_module modules/mod_auth_basic.so
# ...
LoadModule version_module modules/mod_version.so
User apache
Group apache
ServerAdmin root@localhost
UseCanonicalName Off
DocumentRoot "/var/www/html"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
<IfModule mod_userdir.c>
UserDir disabled
</IfModule>
DirectoryIndex index.html index.html.var
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
TypesConfig /etc/mime.types
DefaultType text/plain
MIMEMagicFile conf/magic
</IfModule>
HostnameLookups Off
ErrorLog logs/error_log
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
# ...
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log combined
ServerSignature On
Alias /icons/ "/var/www/icons/"
<Directory "/var/www/icons">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<IfModule mod_dav_fs.c>
# Location of the WebDAV lock database.
DAVLockDB /var/lib/dav/lockdb
</IfModule>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
AddIconByType (TXT,/icons/text.gif) text/*
# ...
AddIconByType (VID,/icons/movie.gif) video/*
AddIcon /icons/binary.gif .bin .exe
# ...
AddIcon /icons/blank.gif ^^BLANKICON^^
DefaultIcon /icons/unknown.gif
ReadmeName README.html
HeaderName HEADER.html
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
AddLanguage ca .ca
# ...
AddLanguage zh-TW .zh-tw
LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
ForceLanguagePriority Prefer Fallback
AddDefaultCharset UTF-8
AddType application/x-compress .Z
# ...
AddType application/x-pkcs7-crl .crl
AddHandler type-map var
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
Alias /error/ "/var/www/error/"
<IfModule mod_negotiation.c>
<IfModule mod_include.c>
<Directory "/var/www/error">
AllowOverride None
Options IncludesNoExec
AddOutputFilter Includes html
AddHandler type-map var
Order allow,deny
Allow from all
LanguagePriority en es de fr
ForceLanguagePriority Prefer Fallback
</Directory>
</IfModule>
</IfModule>
BrowserMatch "Mozilla/2" nokeepalive
# ...
BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully
NameVirtualHost *:80
Include conf.d/*.conf
这个配置可能有什么问题?或者,如果这是默认行为,我该如何按照声明的方式使用它?
答案1
由于您正在定义NameVirtualHost *:80
唯一httpd.conf
的 VirtualHost 定义,因此在通过 IP 连接时会使用该定义。
将以下内容放入/etc/httpd/conf/httpd.conf
(末尾)或新文件/etc/httpd/conf.d/000-default.conf
:
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName servername.host.com
ServerAlias *.servername.host.com
<Directory "/var/www/html">
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
然后重新启动 Apache 并查看是否能解决问题。
要牢记的重要一点是,您想要作为默认的 VirtualHost 定义必须在任何其他 VirtualHost 定义之前定义。