参考文章
目录
- 在一个IP下运行多个基于IP的网站
- 在多个IP下基于域名的主机
- 在不同IP下伺服相同内容(例如一个内网地址和一个外网地址)
- 在不同端口运行不同的站点
- 基于IP的虚拟主机
- 基于端口和IP的混合虚拟主机
- 基于域名和IP的混合虚拟主机
- 同时使用Virtual_host和mod_proxy
- 使用_default_虚拟主机
- 将基于域名的主机迁移到基于IP的主机
- 使用ServerPath指令
1.在一个IP下运行多个基于IP的网站
# Ensure that Apache listens on port 80
Listen 80
DocumentRoot "/www/example1"
ServerName www.example.com
# Other directives here
DocumentRoot "/www/example2"
ServerName www.example.org
# Other directives here
2.在多个IP下基于域名的主机
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.example.com
DocumentRoot "/www/mainserver"
DocumentRoot "/www/example1"
ServerName www.example.com
# Other directives here ...
DocumentRoot "/www/example2"
ServerName www.example.org
# Other directives here ...
3.在不同IP下伺服相同内容(例如一个内网地址和一个外网地址)
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.example.com
DocumentRoot "/www/mainserver"
DocumentRoot "/www/example1"
ServerName www.example.com
# Other directives here ...
DocumentRoot "/www/example2"
ServerName www.example.org
# Other directives here ...
4.在不同端口运行不同的站点
Listen 80
Listen 8080
ServerName www.example.com
DocumentRoot "/www/domain-80"
ServerName www.example.com
DocumentRoot "/www/domain-8080"
ServerName www.example.org
DocumentRoot "/www/otherdomain-80"
ServerName www.example.org
DocumentRoot "/www/otherdomain-8080"
5.基于IP的虚拟主机
Listen 80
DocumentRoot "/www/example1"
ServerName www.example.com
DocumentRoot "/www/example2"
ServerName www.example.org
6.基于端口和IP的混合虚拟主机
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
DocumentRoot "/www/example1-80"
ServerName www.example.com
DocumentRoot "/www/example1-8080"
ServerName www.example.com
DocumentRoot "/www/example2-80"
ServerName www.example.org
DocumentRoot "/www/example2-8080"
ServerName www.example.org
7.基于域名和IP的混合虚拟主机
Listen 80
DocumentRoot "/www/example1"
ServerName www.example.com
DocumentRoot "/www/example2"
ServerName www.example.org
DocumentRoot "/www/example3"
ServerName www.example.net
# IP-based
DocumentRoot "/www/example4"
ServerName www.example.edu
DocumentRoot "/www/example5"
ServerName www.example.gov
8.同时使用Virtual_host和mod_proxy
ProxyPreserveHost On
ProxyPass "/" "http://192.168.111.2/"
ProxyPassReverse "/" "http://192.168.111.2/"
ServerName hostname.example.com
9.使用_default_虚拟主机
_default_ vhosts for all ports
DocumentRoot "/www/default"
_default_ vhosts for different ports
DocumentRoot "/www/default80"
# ...
DocumentRoot "/www/default"
# ...
_default_ vhosts for one port
DocumentRoot "/www/default"
...
10.将基于域名的主机迁移到基于IP的主机
Listen 80
ServerName www.example.com
DocumentRoot "/www/example1"
DocumentRoot "/www/example2"
ServerName www.example.org
# ...
DocumentRoot "/www/example3"
ServerName www.example.net
ServerAlias *.example.net
# ...
11.使用ServerPath指令
# primary vhost
DocumentRoot "/www/subdomain"
RewriteEngine On
RewriteRule "." "/www/subdomain/index.html"
# ...
DocumentRoot "/www/subdomain/sub1"
ServerName www.sub1.domain.tld
ServerPath "/sub1/"
RewriteEngine On
RewriteRule "^(/sub1/.*)" "/www/subdomain$1"
# ...
DocumentRoot "/www/subdomain/sub2"
ServerName www.sub2.domain.tld
ServerPath "/sub2/"
RewriteEngine On
RewriteRule "^(/sub2/.*)" "/www/subdomain$1"
# ...