Apache多站点实现原理
apache安装服务
httpd.exe -k install
httpd.exe -k uninstall
很多人常常看到一台服务器上跑多个站点,不同的域名访问不同的站点,就会有个疑惑:访问的时候并没有在地址栏中加入端口号,多个域名都是解析到这个服务器的IP地址,服务器怎么能够正确的分辨和解析呢?
其实还是对http协议不够了解,看下图一切一目了然
如何实现www.abc.com能映射到服务器的8000端口呢? 注意这里是不带端口号的,www.abc.com:8000
,这样太丑陋
对应的在apache中有rewite和proxy模块,它们就是为了完成这些任务, 一台服务上我们配置多个 vhost,必然需要端口号,
比如:
a.com :80
b.com :8080
实际访问的时候www.a.com自动转发到80端口,www.b.com则自动转发到8080端口。
但是实际上利用80端口可以配置多个vhost,这已经够了
#定义vhost组
NameVirtualHost *:80
网站 1:
<VirtualHost *:80>
ServerName shazuihuo.com
ServerAdmin wancy86@sina.com
DocumentRoot /var/www/shazuihuo/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/demo/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
网站 2:
<VirtualHost *:80>
ServerName tuitu.cn
ServerAdmin wancy86@sina.com
DocumentRoot /var/www/tuitu/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/demo/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
这样一个服务器的80
端口上就跑起两个站点.
虚拟主机只能使用域名访问
没事找了个虚拟主机来试用,尝试用给的IP地址访问怎么都访问不了,一直都显示
后来仔细一想,虚拟主机是需要依赖域名的,不然没办法区分vhost,后来找到测试的域名果然可以正常访问了。