Apache-2.2

Apache 伺服器:IP 地址路由到域而不是 DocumentRoot

  • October 10, 2014

我的伺服器中只有一個網站。我的願望是在網路瀏覽器中使用 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

這種配置可能有什麼問題?或者,如果這是預設行為,我該如何按照我聲明的方式工作?

由於您在通過 IP 連接時使用的唯一 VirtualHost 定義NameVirtualHost *:80中進行了定義。httpd.conf

將以下內容放入/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 定義之前定義。

引用自:https://serverfault.com/questions/634978