目录 搜索
首页 版本说明 从1.3升级到2.0 编译时配置的改变 运行时配置的改变 杂项变化 第三方模块 从 2.0 升级到 2.2 编译时配置的改变 运行时配置的改变 杂项变化 第三方模块 Apache 2.1/2.2 版本的新特性 核心增强 模块增强 程序增强 针对模块开发者的变化 Apache 2.0 版本的新特性 核心的增强 模块的增强 Apache许可证 参考手册 编译与安装 针对心急者的概述 要求 下载 解压 配置源代码树 编译 安装 配置 测试 升级 启动 Apache是怎样启动的 启动时发生错误 随系统启动时启动 额外信息 停止与重新启动 简介 立即停止 优雅重启 立即重启 优雅停止 附录:信号和竞争条件 运行时配置指令 主配置文件 配置文件的语法 模块 指令的作用域 .htaccess文件 配置段 配置段(容器)的类型 文件系统和网络空间 虚拟主机 代理 允许使用哪些指令? 配置段的合并 内容缓冲 简介 缓冲概述 安全方面的考虑 文件句柄缓冲 内存缓冲 磁盘缓冲 服务器全局配置 服务器标识 文件定位 限制资源的使用 日志文件 安全警告 错误日志 访问日志 日志滚动 管道日志 虚拟主机 其他日志文件 从URL到文件系统的映射 相关模块和指令 DocumentRoot DocumentRoot以外的文件 用户目录 URL重定向 反向代理 重写引擎 File Not Found 安全方面的提示 保持不断更新和升级 ServerRoot目录的权限 服务器端包含 关于CGI 未指定为脚本的CGI 指定为脚本的CGI 其他动态内容的来源 系统设置的保护 默认配置下服务器文件的保护 观察日志文件 动态共享对象(DSO) 实现 用法概要 背景知识 优点和缺点 内容协商 关于内容协商 Apache中的内容协商 协商的方法 打乱品质值 透明内容协商的扩展 超链和名称转换说明 缓冲说明 更多信息 自定义错误响应 行为 配置 自定义错误响应与重定向 地址和端口绑定 概述 针对IPv6的特殊考虑 怎样与虚拟主机协同工作 多路处理模块(MPM) 简介 选择一个MPM 默认的MPM 环境变量 设置环境变量 使用环境变量 用于特殊目的的环境变量 示例 处理器的使用 什么是处理器? 例子 程序员注意事项 过滤器 Apache2中的过滤器 智能过虑 使用过滤器 CGI脚本的Suexec执行 开始之前 suEXEC的安全模型 配置和安装suEXEC 启用和禁用suEXEC 使用suEXEC 调试suEXEC 谨防Jabberwock:警告和举例 性能调整 硬件和操作系统 运行时的配置 编译时的配置 附录:踪迹的详细分析 URL重写指南 mod_rewrite简介 实践方案 URL的规划 内容的处理 对访问的限制 其他 虚拟主机文档 总述 虚拟主机支持 配置指令 基于主机名的虚拟主机 基于域名的虚拟主机和基于IP的虚拟主机比较 使用基于域名的虚拟主机 与旧版浏览器的兼容性 基于IP地址的虚拟主机 系统需求 如何配置Apache 设置多个守护进程 配置拥有多个虚拟主机的单一守护进程 动态配置大量虚拟主机 动机 概述 简单的动态虚拟主机 一个实际的个人主页系统 在同一个服务器上架设多个主机的虚拟系统 更为有效的基于IP地址的虚拟主机 使用老版本的Apache 使用mod_rewrite实现简单的动态虚拟主机 使用mod_rewrite的个人主页系统 使用独立的虚拟主机配置文件 虚拟主机的普通配置示例 在一个IP地址上运行多个基于域名的web站点 在多于一个IP的情况下使用基于域名的虚拟主机 在不同的IP的地址(比如一个内部和一个外部地址)上提供相同的内容 在不同的端口上运行不同的站点 建立基于IP的虚拟主机 混用基于端口和基于IP的虚拟主机 混用基于域名和基于IP的虚拟主机 将虚拟主机和代理模块一起使用 使用默认虚拟主机 将一个基于域名的虚拟主机移植为一个基于IP的虚拟主机 使用ServerPath指令 深入讨论虚拟主机的匹配 解析配置文件 虚拟主机匹配 小技巧 文件描述符限制 关于DNS和Apache 一个简单示例 拒绝服务 "主服务器"地址 避免这些问题的小技巧 附录:进一步的提示 常见问题 概述 SSL/TLS 加密 概述 文档 mod_ssl 绪论 密码技术 证书 安全套接字层(SSL) 参考 兼容性 配置指令 环境变量 自定义日志功能 如何... 加密方案和强制性高等级安全 客户认证和访问控制 常见问题解答 About The Module Installation Configuration Certificates The SSL Protocol mod_ssl Support 如何.../指南 概述 认证 相关模块和指令 简介 先决条件 启用认证 允许多人访问 可能存在的问题 其他认证方法 更多信息 CGI动态页面 简介 配置Apache以允许CGI 编写CGI程序 程序还是不能运行! 幕后是怎样操作的? CGI模块/库 更多信息 服务器端包含 简介 什么是SSI? 配置服务器以允许SSI 基本SSI指令 附加的例子 我还能设置其它什么? 执行命令 高级SSI技术 总结 .htaccess文件 .htaccess文件 工作原理和使用方法 (不)使用.htaccess文件的场合 指令的生效 认证举例 服务器端包含(SSI)举例 CGI举例 疑难解答 用户网站目录 用户网站目录 用UserDir设置文件路径 限定哪些用户可以使用此功能 启用对每个用户都有效的cgi目录 允许用户改变配置 对特定平台的说明 概述 Microsoft Windows 其他平台 在Microsoft Windows中使用Apache 对操作系统的要求 下载 Apache for Windows 安装 Apache for Windows 配置 Apache for Windows 以服务方式运行 Apache for Windows 作为控制台程序运行Apache 测试安装 编译Windows下的Apache 系统要求 命令行编译 Developer Studio集成开发环境的工作区编译 项目组件 在Novell NetWare平台上使用Apache Requirements Downloading Apache for NetWare Installing Apache for NetWare Running Apache for NetWare Configuring Apache for NetWare Compiling Apache for NetWare 在HP-UX中运行Apache The Apache EBCDIC Port Overview of the Apache EBCDIC Port Design Goals Technical Solution Porting Notes Document Storage Notes Apache Modules' Status Third Party Modules' Status 服务器与支持程序 概述 httpd 语法 选项 ab 语法 选项 Bugs apachectl 语法 选项 apxs 语法 选项 举例 configure 语法 选项 环境变量 dbmmanage 语法 选项 Bugs htcacheclean 语法 选项 返回值 htdbm 语法 选项 Bugs 返回值 举例 安全方面的考虑 限制 htdigest 语法 选项 htpasswd 语法 选项 返回值 举例 安全方面的考虑 限制 logresolve 语法 选项 rotatelogs 语法 选项 Portability suexec 语法 选项 其他程序 log_server_status split-logfile 杂项文档 概述 相关标准 HTTP推荐标准 HTML推荐标准 认证 语言/国家代码 Apache 模块 描述模块的术语 说明 状态 源代码文件 模块标识符 兼容性 描述指令的术语 说明 语法 默认值(Default) 作用域(Context) 覆盖项(Override) 状态 模块(Module) 兼容性(Compatibility) Apache核心(Core)特性 AcceptFilter AcceptPathInfo AccessFileName AddDefaultCharset AddOutputFilterByType AllowEncodedSlashes AllowOverride AuthName AuthType CGIMapExtension ContentDigest DefaultType <Directory> <DirectoryMatch> DocumentRoot EnableMMAP EnableSendfile ErrorDocument ErrorLog FileETag <Files> <FilesMatch> ForceType HostnameLookups <IfDefine> <IfModule> Include KeepAlive KeepAliveTimeout <Limit> <LimitExcept> LimitInternalRecursion LimitRequestBody LimitRequestFields LimitRequestFieldSize LimitRequestLine LimitXMLRequestBody <Location> <LocationMatch> LogLevel MaxKeepAliveRequests NameVirtualHost Options Require RLimitCPU RLimitMEM RLimitNPROC Satisfy ScriptInterpreterSource ServerAdmin ServerAlias ServerName ServerPath ServerRoot ServerSignature ServerTokens SetHandler SetInputFilter SetOutputFilter TimeOut TraceEnable UseCanonicalName UseCanonicalPhysicalPort <VirtualHost> Apache MPM 公共指令 AcceptMutex CoreDumpDirectory EnableExceptionHook GracefulShutdownTimeout Group Listen ListenBackLog LockFile MaxClients MaxMemFree MaxRequestsPerChild MaxSpareThreads MinSpareThreads PidFile ReceiveBufferSize ScoreBoardFile SendBufferSize ServerLimit StartServers StartThreads ThreadLimit ThreadsPerChild ThreadStackSize User Apache MPM beos MaxRequestsPerThread CoreDumpDirectory Group Listen ListenBacklog MaxClients MaxMemFree MaxSpareThreads MinSpareThreads PidFile ReceiveBufferSize ScoreBoardFile SendBufferSize StartThreads User Apache MPM event AcceptMutex CoreDumpDirectory EnableExceptionHook Group Listen ListenBacklog LockFile MaxClients MaxMemFree MaxRequestsPerChild MaxSpareThreads MinSpareThreads PidFile ScoreBoardFile SendBufferSize ServerLimit StartServers ThreadLimit ThreadsPerChild ThreadStackSize User Apache MPM netware MaxThreads Listen ListenBacklog MaxMemFree MaxRequestsPerChild MaxSpareThreads MinSpareThreads ReceiveBufferSize SendBufferSize StartThreads ThreadStackSize Apache MPM os2 Group Listen ListenBacklog MaxRequestsPerChild MaxSpareThreads MinSpareThreads PidFile ReceiveBufferSize SendBufferSize StartServers User Apache MPM prefork 工作方式 MaxSpareServers MinSpareServers AcceptMutex CoreDumpDirectory EnableExceptionHook Group Listen ListenBacklog LockFile MaxClients MaxMemFree MaxRequestsPerChild PidFile ReceiveBufferSize ScoreBoardFile SendBufferSize ServerLimit StartServers User Apache MPM winnt Win32DisableAcceptEx CoreDumpDirectory Listen ListenBacklog MaxMemFree MaxRequestsPerChild PidFile ReceiveBufferSize ScoreBoardFile SendBufferSize ThreadLimit ThreadsPerChild ThreadStackSize Apache MPM worker 工作方式 AcceptMutex CoreDumpDirectory EnableExceptionHook Group Listen ListenBacklog LockFile MaxClients MaxMemFree MaxRequestsPerChild MaxSpareThreads MinSpareThreads PidFile ReceiveBufferSize ScoreBoardFile SendBufferSize ServerLimit StartServers ThreadLimit ThreadsPerChild ThreadStackSize User Apache Module mod_actions Action指令 Script指令 Apache Module mod_alias 处理顺序 Alias AliasMatch Redirect RedirectMatch RedirectPermanent RedirectTemp ScriptAlias ScriptAliasMatch Apache Module mod_asis 用法 Apache Module mod_auth_basic AuthBasicAuthoritative AuthBasicProvider Apache Module mod_auth_digest 使用摘要认证 配合 MS Internet Explorer 6 工作 AuthDigestAlgorithm AuthDigestDomain AuthDigestNcCheck AuthDigestNonceFormat AuthDigestNonceLifetime AuthDigestProvider AuthDigestQop AuthDigestShmemSize Apache Module mod_authn_alias 示例 <AuthnProviderAlias> Apache Module mod_authn_anon 示例 Anonymous Anonymous_LogEmail Anonymous_MustGiveEmail Anonymous_NoUserID Anonymous_VerifyEmail Apache Module mod_authn_dbd 配置示例 AuthDBDUserPWQuery AuthDBDUserRealmQuery Apache Module mod_authn_dbm AuthDBMType AuthDBMUserFile Apache Module mod_authn_default AuthDefaultAuthoritative Apache Module mod_authn_file AuthUserFile Apache Module mod_authnz_ldap Contents Operation The require Directives 举例 Using TLS Using SSL Using Microsoft FrontPage with mod_authnz_ldap AuthLDAPBindDN AuthLDAPBindPassword AuthLDAPCharsetConfig AuthLDAPCompareDNOnServer AuthLDAPDereferenceAliases AuthLDAPGroupAttribute AuthLDAPGroupAttributeIsDN AuthLDAPRemoteUserIsDN AuthLDAPUrl AuthzLDAPAuthoritative Apache Module mod_authz_dbm AuthDBMGroupFile AuthzDBMAuthoritative AuthzDBMType Apache Module mod_authz_default AuthzDefaultAuthoritative Apache Module mod_authz_groupfile AuthGroupFile AuthzGroupFileAuthoritative Apache Module mod_authz_host Allow Deny Order Apache Module mod_authz_owner 配置示例 AuthzOwnerAuthoritative Apache Module mod_authz_user AuthzUserAuthoritative Apache Module mod_autoindex Autoindex Request Query Arguments AddAlt AddAltByEncoding AddAltByType AddDescription AddIcon AddIconByEncoding AddIconByType DefaultIcon HeaderName IndexIgnore IndexOptions IndexOrderDefault IndexStyleSheet ReadmeName Apache Module mod_cache Related Modules and Directives 配置示例 CacheDefaultExpire CacheDisable CacheEnable CacheIgnoreCacheControl CacheIgnoreHeaders CacheIgnoreNoLastMod CacheLastModifiedFactor CacheMaxExpire CacheStoreNoStore CacheStorePrivate Apache Module mod_cern_meta MetaDir MetaFiles MetaSuffix Apache Module mod_cgi CGI 环境变量 CGI 脚本的调试 ScriptLog ScriptLogBuffer ScriptLogLength Apache Module mod_cgid ScriptSock ScriptLog ScriptLogBuffer ScriptLogLength Apache Module mod_charset_lite Common Problems CharsetDefault CharsetOptions CharsetSourceEnc Apache Module mod_dav Enabling WebDAV Security Issues Complex Configurations Dav DavDepthInfinity DavMinTimeout Apache Module mod_dav_fs DavLockDB Apache Module mod_dav_lock DavGenericLockDB Apache Module mod_dbd Connection Pooling Apache DBD API SQL Prepared Statements DBDExptime DBDKeep DBDMax DBDMin DBDParams DBDPersist DBDPrepareSQL DBDriver Apache Module mod_deflate 配置举例 启用压缩 代理服务器 DeflateBufferSize DeflateCompressionLevel DeflateFilterNote DeflateMemLevel DeflateWindowSize Apache Module mod_dir DirectoryIndex DirectorySlash Apache Module mod_disk_cache CacheDirLength CacheDirLevels CacheMaxFileSize CacheMinFileSize CacheRoot Apache Module mod_dumpio 启用dumpio支持 DumpIOInput DumpIOOutput Apache Module mod_echo ProtocolEcho Apache Module mod_env PassEnv SetEnv UnsetEnv Apache Module mod_example Compiling the example module Using the mod_example Module Example Apache Module mod_expires 交替间隔语法 ExpiresActive ExpiresByType ExpiresDefault Apache Module mod_ext_filter 举例 ExtFilterDefine ExtFilterOptions Apache Module mod_file_cache Using mod_file_cache CacheFile MMapFile Apache Module mod_filter Smart Filtering Filter Declarations Configuring the Chain Examples Protocol Handling FilterChain FilterDeclare FilterProtocol FilterProvider FilterTrace Apache Module mod_headers 处理顺序 前处理和后处理 举例 Header RequestHeader Apache Module mod_ident IdentityCheck IdentityCheckTimeout Apache Module mod_imagemap New Features Imagemap File Example Mapfile Referencing your mapfile ImapBase ImapDefault ImapMenu Apache Module mod_include Enabling Server-Side Includes PATH_INFO with Server Side Includes Basic Elements Include Variables Variable Substitution Flow Control Elements SSIEndTag SSIErrorMsg SSIStartTag SSITimeFormat SSIUndefinedEcho XBitHack Apache Module mod_info 安全问题 选择哪些信息可以被显示 已知的局限 AddModuleInfo Apache Module mod_isapi 用法 附加注释 程序员注记 ISAPIAppendLogToErrors ISAPIAppendLogToQuery ISAPICacheFile ISAPIFakeAsync ISAPILogNotSupported ISAPIReadAheadBuffer Apache Module mod_ldap 示例配置 LDAP 连接池 LDAP 缓冲 使用SSL/TLS SSL/TLS 证书 LDAPCacheEntries LDAPCacheTTL LDAPConnectionTimeout LDAPOpCacheEntries LDAPOpCacheTTL LDAPSharedCacheFile LDAPSharedCacheSize LDAPTrustedClientCert LDAPTrustedGlobalCert LDAPTrustedMode LDAPVerifyServerCert Apache Module mod_log_config 定制日志文件格式 安全考虑 BufferedLogs CookieLog CustomLog LogFormat TransferLog Apache Module mod_log_forensic 定制日志文件格式 安全考虑 ForensicLog Apache Module mod_logio 定制日志文件格式 Apache Module mod_mem_cache MCacheMaxObjectCount MCacheMaxObjectSize MCacheMaxStreamingBuffer MCacheMinObjectSize MCacheRemovalAlgorithm MCacheSize Apache Module mod_mime 带多扩展名的文件 内容编码 字符集和语言 AddCharset AddEncoding AddHandler AddInputFilter AddLanguage AddOutputFilter AddType DefaultLanguage ModMimeUsePathInfo MultiviewsMatch RemoveCharset RemoveEncoding RemoveHandler RemoveInputFilter RemoveLanguage RemoveOutputFilter RemoveType TypesConfig Apache Module mod_mime_magic "Magic文件"的格式 性能问题 注意 MimeMagicFile Apache Module mod_negotiation 类型表 MultiViews CacheNegotiatedDocs ForceLanguagePriority LanguagePriority Apache Module mod_nw_ssl NWSSLTrustedCerts NWSSLUpgradeable SecureListen Apache Module mod_proxy 正向和反向代理 简单示例 控制对代理服务器的访问 缓慢启动 局域网代理 协议调整 请求体 AllowCONNECT NoProxy <Proxy> ProxyBadHeader ProxyBlock ProxyDomain ProxyErrorOverride ProxyIOBufferSize <ProxyMatch> ProxyMaxForwards ProxyPass ProxyPassReverse ProxyPassReverseCookieDomain ProxyPassReverseCookiePath ProxyPreserveHost ProxyReceiveBufferSize ProxyRemote ProxyRemoteMatch ProxyRequests ProxyTimeout ProxyVia Apache Module mod_proxy_ajp Overview of the protocol Basic Packet Structure Request Packet Structure Response Packet Structure Apache Module mod_proxy_balancer Load balancer scheduler algorithm Request Counting Algorithm Weighted Traffic Counting Algorithm Enabling Balancer Manager Support Apache Module mod_proxy_connect Apache Module mod_proxy_ftp 为什么xxx类型的文件不能从FTP下载? 如何强制文件xxx使用FTP的ASCII形式下载? 我如何使用FTP上传? 我如何能访问我自己home目录以外的FTP文件? 我如何才能在浏览器的URL框中隐藏FTP的明文密码? Apache Module mod_proxy_http Apache Module mod_rewrite 特殊字符的引用 环境变量 实用方案 RewriteBase RewriteCond RewriteEngine RewriteLock RewriteLog RewriteLogLevel RewriteMap RewriteOptions RewriteRule Apache Module mod_setenvif BrowserMatch BrowserMatchNoCase SetEnvIf SetEnvIfNoCase Apache Module mod_so 为Windows创建可加载模块 LoadFile LoadModule Apache Module mod_speling CheckSpelling Apache Module mod_ssl 环境变量 Custom Log Formats SSLCACertificateFile SSLCACertificatePath SSLCADNRequestFile SSLCADNRequestPath SSLCARevocationFile SSLCARevocationPath SSLCertificateChainFile SSLCertificateFile SSLCertificateKeyFile SSLCipherSuite SSLCryptoDevice SSLEngine SSLHonorCipherOrder SSLMutex SSLOptions SSLPassPhraseDialog SSLProtocol SSLProxyCACertificateFile SSLProxyCACertificatePath SSLProxyCARevocationFile SSLProxyCARevocationPath SSLProxyCipherSuite SSLProxyEngine SSLProxyMachineCertificateFile SSLProxyMachineCertificatePath SSLProxyProtocol SSLProxyVerify SSLProxyVerifyDepth SSLRandomSeed SSLRequire SSLRequireSSL SSLSessionCache SSLSessionCacheTimeout SSLUserName SSLVerifyClient SSLVerifyDepth Apache Module mod_status Enabling Status Support 自动更新 Machine Readable Status File ExtendedStatus Apache Module mod_suexec SuexecUserGroup Apache Module mod_unique_id Theory Apache Module mod_userdir UserDir Apache Module mod_usertrack Logging 2-digit or 4-digit dates for cookies? CookieDomain CookieExpires CookieName CookieStyle CookieTracking Apache Module mod_version <IfVersion> Apache Module mod_vhost_alias 目录名称的转换 示例 VirtualDocumentRoot VirtualDocumentRootIP VirtualScriptAlias VirtualScriptAliasIP 开发者文档 Overview Topics External Resources Apache API notes Basic concepts How handlers work Resource allocation and resource pools Configuration Debugging Memory Allocation in APR Available debugging options Allowable Combinations Activating Debugging Options Documenting Apache 2.0 Apache 2.0 Hook Functions Creating a hook function Hooking the hook Converting Modules from Apache 1.3 to Apache 2.0 The easier changes ... The messier changes... Request Processing in Apache 2.0 The Request Processing Cycle The Request Parsing Phase The Security Phase The Preparation Phase The Handler Phase How Filters Work in Apache 2.0 Filter Types How are filters inserted? Asis Explanations 词汇和索引 词汇表 模块索引 指令索引 指令速查 译者声明
文字

虚拟主机示例

本文档试图解释一些在设置虚拟主机时经常问及的问题。这些示例向你展示了如何在一个服务器上通过基于域名的或是基于IP的虚拟主机来部署多个web站点。另一份关于如何在一个代理服务器后构建基于多个服务器的站点的说明文档也很快就会出来。

在一个IP地址上运行多个基于域名的web站点

您的服务器有只一个IP地址,而在DNS中有很多域名(CNAMES)映射到这个机器。您而您想要在这个机器上运行www.example.comwww.example.org两个站点。

注意

在您的Apache服务器配置中创建一个虚拟主机并不会自动在您的DNS中对主机名做相应更新。您必须自己在DNS中添加域名来指向您的IP地址。否则别人是无法看到您的web站点的。您可以在您的hosts文件中添加这一条目来进行测试,但这种方法仅适用于那些有这些hosts文件的机器来使用。

服务器配置

# 确保Apache在监听80端口
Listen 80

# 为虚拟主机在所有IP地址上监听
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com

# 你可以在这里添加其他指令

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org

# 你可以在这里添加其他指令

</VirtualHost>

因为星号匹配所有IP地址,所以主服务器不接收任何请求。因为www.example.com首先出现在配置文件中,所以它拥有最高优先级,可以认为是默认服务器。这意味着如果一个请求不能与某个ServerName指令相匹配,它将会由第一个<VirtualHost>段所伺服。

注意

如果您愿意,您可以用确定的IP地址来取代"*"。在这种情况下,VirtualHost的参数必须NameVirtualHost的参数相符:

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
# 其他 ...

然而,当您的IP地址无法确定的时候,使用"*"是很方便的,比如说,您的ISP给您配置的是动态IP地址,而您又使用了某种动态域名解析系统时。因为"*"匹配任何IP地址,所以在这种情况下,不论IP地址如何变化,您都不需要另外进行配置。

上述配置就是您在绝大多数情况下使用基于域名的虚拟主机时将要用到的。事实上,仅在一种情况下这样的配置不会让您满意:您想为不同的IP地址或是端口提供不同的内容。

在多于一个IP的情况下使用基于域名的虚拟主机。

注意

在这里讨论的任何技术都可以推广到使用任意数量的IP地址。

服务器有两个IP地址。一个(172.20.30.40)用于主服务器server.domain.com ,另外一个(172.20.30.50)用于构建两个或多个虚拟主机。

服务器配置

Listen 80

# "主"服务器运行于:172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver

# 这是另外一个IP地址
NameVirtualHost 172.20.30.50

<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example.com

# 你可以在这里添加其他指令 ...

</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example.org

# 你可以在这里添加其他指令 ...

</VirtualHost>

任何不是针对172.20.30.50的请求都将由主服务器来伺服。而提交给172.20.30.50却没有主机名或没有"Host:"头的请求,都将由www.example.com伺服。

在不同的IP的地址(比如一个内部和一个外部地址)上提供相同的内容

服务器有两个IP地址(192.168.1.1172.20.30.40)。这个机器位于内部(局域网)网络和外部(广域网)之间。在外部,域名server.example.com指向外部地址(172.20.30.40),而在内部则指向内部地址(192.168.1.1)。

服务器可以为来自内部和外部的请求提供同样的内容,您只需要一个<VirtualHost>配置段就可以了。

服务器配置

NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40

<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
</VirtualHost>

现在,从不同的网络提交的请求都会由同一个<VirtualHost>段来伺服。

注意

在内网中,您可以使用server这个名字来代替server.example.com这个全名。

跟上面一样,在上述的例子里,您可以用"*"来代替具体的IP地址,这样就可以对所有的地址都返回相同的内容了。

在不同的端口上运行不同的站点

如果您想让同一个IP的不同端口伺服多个域名。您可以借助在NameVirtualHost指令中定义端口的方法来达到这个目的。如果您想使用不带"name:port"的<VirtualHost name:port>或是直接用Listen指令,您的配置将无法生效。

服务器配置

Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example.com
DocumentRoot /www/domain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example.com
DocumentRoot /www/domain-8080
</VirtualHost>

<VirtualHost 172.20.30.40:80>
ServerName www.example.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

建立基于IP的虚拟主机

一个有两个IP地址(172.20.30.40172.20.30.50)分别对应域名www.example.comwww.example.org的配置如下:

服务器配置

Listen 80

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example.org
</VirtualHost>

如果存在主服务器,那么对没有出现在任一个<VirtualHost>段中的请求(比如,对localhost的请求)都会由主服务器来伺服。

混用基于端口和基于IP的虚拟主机

如果您的服务器有两个IP地址(172.20.30.40172.20.30.50)分别对应域名www.example.comwww.example.org 。对每个域名,您都希望在80端口和8080端口发布您的网站。您可以这样配置:

服务器配置

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example.org
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example.org
</VirtualHost>

混用基于域名和基于IP的虚拟主机

您想在一些地址上配置基于域名的虚拟主机而在另外一些地址上配置基于IP的虚拟主机。

服务器配置

Listen 80

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example.com
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example.org
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>

<VirtualHost 172.20.30.60>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost>

<Virtual_host>mod_proxy模块一起使用

下面的例子允许一个前端机器代理一个运行在其他机器上的虚拟主机。在如下示例中,在192.168.111.2机器上配置了一个同名的虚拟主机。这样,万一在同一台机器上代理了多个主机名,ProxyPreserveHost On 指令能确保指定的主机名顺利通过代理。

<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://192.168.111.2
ProxyPassReverse / http://192.168.111.2/
ServerName hostname.example.com
</VirtualHost>

使用"_default_"虚拟主机

为所有端口配置"_default_"虚拟主机

这样配置可以捕获所有指向没指定的IP地址和端口的请求。比如:一个没被任何虚拟主机使用的地址/端口对。

服务器配置

<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>

这样一个使用通配符端口的默认虚拟主机可以有效的防止请求被主服务器接收。

如果一个地址/端口对已经被一个基于域名的虚拟主机使用,那么"_default_"虚拟主机决不会处理发向这个地址/端口对的请求。如果一个"Host:"请求头中包含未知信息,或者干脆就没有,那么它会被第一个基于域名的虚拟主机(也就是在配置文件中最先出现的使用了那个地址/端口对的虚拟主机)处理。

您可以用AliasMatchRewriteRule来重写任何请求,使它指向一个简单信息页面(或脚本)。

为不同的端口配置"_default_"虚拟主机

与第一种一样,但我们想让服务器侦听很多端口而第二个"_default_"虚拟主机单独侦听80端口。

服务器配置

<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>

<VirtualHost _default_:*>
DocumentRoot /www/default
# ...
</VirtualHost>

侦听80端口的"_default_"虚拟主机(必须出现在所有使用通配符端口的虚拟主机之前)会捕获所有发向一个未指定的IP地址的请求。主服务器将不会用于伺服任何请求。

为单独一个端口配置"_default_"虚拟主机

如果我们只想在80端口上建立唯一的一个"_default_"虚拟主机,我们应该这样配置:

服务器配置

<VirtualHost _default_:80>
DocumentRoot /www/default
...
</VirtualHost>

发向一个未指定地址的80端口的请求将会由这个虚拟主机伺服;而发向未设定地址的其他端口的请求则由主服务器伺服。

将一个基于域名的虚拟主机移植为一个基于IP的虚拟主机

如果一个具有www.example.org域名的虚拟主机(就是基于域名配置示例中的第二个)得到了自己的IP地址。为了避免一些域名服务器或代理服务器在移植期间仍对这个域名做老的解析,我们可以采用一种过渡方法:同时提供新旧两个IP地址的解析。

达到这个目的很简单。因为我们只要简单的把新地址(172.20.30.50)加入VirtualHost指令就行了。

服务器配置

Listen 80
ServerName www.example.com
DocumentRoot /www/example1

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example.org
# ...
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example.net
ServerAlias *.example.net
# ...
</VirtualHost>

现在这个虚拟主机就可以用新地址(表现为一个基于IP的虚拟主机)和旧地址(表现为一个基于域名的虚拟主机)同时进行访问了。

使用ServerPath指令

如果我们在同一个服务器上运行了两个基于域名的虚拟主机。为了匹配正确的虚拟主机,客户端必须发送正确的"Host:"头。而旧的使用HTTP/1.0的客户端无法发送这样的头,这样Apache就无法辨别客户端想要连接哪个虚拟主机(会用主虚拟主机来伺服这个请求)。为了尽量提供向下兼容性,我们可以提供一个主虚拟主机来返回一个页面,在页面中加入指向基于域名的虚拟主机的URL前缀的链接。

服务器配置

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
# 主虚拟主机
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html
# ...
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1
# ...
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/subdomain/sub2
ServerName www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1
# ...
</VirtualHost>

由于ServerPath指令的作用,发送到http://www.sub1.domain.tld/sub1/的请求总会被sub1-vhost所伺服。
如果客户端发送了正确的"Host:"头,发送到http://www.sub1.domain.tld/的请求只会被sub1-vhost所伺服。如果没有发送"Host:"头,客户端将会得到从主虚拟主机发送的信息页面。

请注意,这里还有一点小问题:如果客户端没有发送"Host:"头,发送到http://www.sub2.domain.tld/sub1/的请求还是会被sub1-vhost所伺服。

RewriteRule指令用以确保正确发送了"Host:"头的客户端可以任意使用这两种URL变量,比如说:使用或不使用URL前缀。

上一篇: 下一篇: