文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Apache+Tocat虚拟主机的配置Apache+(Apache+Tomc..

Apache+Tocat虚拟主机的配置Apache+(Apache+Tomc..

时间:2006-11-24  来源:lxbing113

Apache Virtual Host

one of the most important facilities in Apache is the ability to run 'Virtual Host',the term Virtual Host refers to the practice of maintaining more than one server on one machine,it is essential way to deploy multiple web services - eache of them with different host name and urls,Example, many companies desired to share a web server of containning multiple domains for different applications(wwww.example1.com for a applicaton,and www.example2.com for the other application),without desiring the vistor to know any extra path information(IP,port etc),because of high security request.by this way,you don't need to by more new machine for every application,it can highly improve optimization of the server.

In this issues we explain how to go about setting up a virtual host on your machine, what you need to do to get the hostname working, and how to configure Apache.Of course, we assume that you have already installing and running Tomcat and Apache servers in your computer.There has two method to implement 'Virtual Host', name-based or IP-based virtual hosts.
Apache was one of the first servers to support IP-based virtual hosts right out of the box. Versions 1.1 and later of Apache support both, IP-based and name-based virtual hosts (vhosts). The latter variant of virtual hosts is sometimes also called host-based or non-IP virtual hosts.

Host-based

my system environment for this configuration is:

 server  version
 gentoo linux  2.6
 Apache
 2.0
 Tomcat  5.0 and 5.5


Your server has a single IP address, and multiple aliases (CNAMES) point to this machine in DNS. You want to run a web server for www.example1.com and www.example2.org on this machine.
Note
Creating virtual host configurations on your Apache server does not magically cause
DNS entries to be created for those host names. You must have the names in DNS, resolving
to your IP address, or nobody else will be able to see your web site. You can put entries
in your hosts file for local testing, but that will work only from the machine with those
hosts entries.
To defined hostname in DNS for test, I config the file /etc/hosts. and add
192.168.0.136 www.lxbing.com
192.168.0.136 www.example.com
192.168.0.136 www.example1.com
Now there are there applications in my tomcat server /opt/tomcat/webapps, /xwiki,/example, and /example2, I want to visite them via apache. frist, we must to config /opt/tomcat/config/server.xml,add three node to point to different applications, please pay attention to the "proxyName" and "proxyPort" properties in the node,they will map with apache configuration soon,after configuration, the file looks like this:
 <Connector port="8080" maxHttpHeaderSize="8192" 
maxThreads="150" minSpareThreads="25" maxSpareThreads="75" proxyName="www.example1.com" proxyPort="80"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
<Connector port="8081"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75" proxyName="www.lxbing.com" proxyPort="80"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000" useURIValidationHack="false"
disableUploadTimeout="true" />
<Connector port="8082"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75" proxyName="www.example.com" proxyPort="80"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000" useURIValidationHack="false"
disableUploadTimeout="true" />
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />

Apache provide a mode called mod_proxy,which can enable apache to service like a proxy server.thus, we can deliver the specific request to Tomcat server. it needn't to be configed like web connector mod_jk, in order to active this,we should do some configuration as follow:

  1. modify Apache configuration to contains mod_proxy module,if you create it from source,the easy way is add "--enable-module=proxy" to command line "./configure".
  2. if you don't install mod_proxy module, you can upload it using command below,in the file /etc/apache2/httpd.conf
 
LoadModule alias_module modules/mod_alias.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
   3. add the directive below in the file /etc/apache2/httpd.conf, it pass the
      specific request to tomcat server.
NameVirtualHost *:80
<VirtualHost *:80>
<Directory "/var/www/localhost/htdocs/">
Order allow,deny
Allow from all
</Directory>
ServerName www.example.com
ServerName www.lxbing.com
ServerName www.example2.com
DocumentRoot "/var/www/localhost/htdocs/"
ProxyPass /xwiki http://192.168.0.136:8081/xwiki
ProxyPassReverse /xwiki http://192.168.0.136:8081/xwiki
ProxyPass /example http://192.168.0.136:8082/example
ProxyPassReverse /example http://192.168.0.136:8082/example
ProxyPass /example2 http://192.168.0.136:8080/example2
ProxyPassReverse /example2 http://192.168.0.136:8080/example2
</VirtualHost>

ProxyPass told apache to delive url-request (example:http://192.168.0.136/xwiki) to tomcat,which is listenning different port,corresponding to different applications in tomcat. Above is my configuration in my apache and tomcat for testing, and below is offical configuration, it defined different virtual-host in individual node:

Server configuration
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>

At beginning, I config follow the offical document,but it allways only listen the frist vistual host. so I config them in a single visturalhost node.it works well. when you configging, please take care for to port, and the NameVirtualHost key word. NamevirtualHost must be same as VirtualHost,and don't to loss "NameVirtualHost" keyword.
NameVirtualHost *:80
<VirtualHost *:80>
or
NameVirtualHost www.****.com
<VirtualHost www.****.com>

After finish these config ,reboot apache and tomcat. you can visit the applications via proxy server apache.like this http://192.168.0.136/xwiki, it autom turn to wwww.lxbing.com/xwiki. it works well.

multiple proxy-servers for handling remote request for application in individual pc

if there are multiple application in differnt computers, and a single Apache in a computer communicate to DNS, a request for a application in the other computer, what can we do? in this case, we have deliver the request from one proxy server to the other proxy server, to bring it into effect ,we must config the subproxy server frist, then config main proxy server to listen subproxy server. the mothod of configuration for subproxy is the similar with whan I references before. Now we are focus on the configuration of main proxy server.
In this example of totoring, we have three servers,main proxy server:192.168.0.9 with apache communicating to DNS, subproxy server: 192.168.0.136 with a application xwiki (we do the proxy before with it),192.168.0.4 with a application tsalex.

  1. defined the domain name in the DNS server in 192.168.09 server. config file //192.168.0.9/etc/hosts like this
192.168.0.4 www.alexcaro.com 
192.168.0.9 www.xiancaro.com
192.168.0.136 www.lxbing.com
  1. config file 00_defined_vhost.conf in file //192.168.0.9/etc/apache2/vhosts.d, of course, apache in main server must contains the necessary module reference as above,if not, upload it. After configuration,the virtualhost node looks like follow:
NameVirtualHost *:80
<VirtualHost *:80>
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<Directory /var/www/localhost/htdocs>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ProxyRequests Off
ProxyVia On
ProxyPass /tsalex http://www.alexcaro.com/tsalex
ProxyPass /xwiki http://www.lxbing.com/xwiki
DocumentRoot /var/www/localhost/htdocs
DirectoryIndex index.php index.html index.htm
LogLevel warn
HostNameLookups off
</VirtualHost>


when a request to main proxy server apache in server 192.168.0.9 with a contents /tsalex, the main proxy server delived it to subproxy server apache 192.168.0.4 (domain name www.alexcaro.com) and reply http://www.alexcaro.com/tsalex to the request. if a request with content is /xwiki, it will be turned to subproxy server in 192.168.0.136 and return http://www.lxbing.com/xwiki.

mod_proy module

This module implements a proxy/gateway for Apache. It implements proxying capability for FTP, CONNECT (for SSL), HTTP/0.9, HTTP/1.0, and HTTP/1.1. The module can be configured to connect to other proxy modules for these and other protocols.


Apache's proxy features are divided into several modules in addition to mod_proxy: mod_proxy_http, mod_proxy_ftp and mod_proxy_connect. Thus, if you want to use one or more of the particular proxy functions, load mod_proxy and the appropriate module(s) into the server (either statically at compile-time or dynamically via the LoadModule directive).


In addition, extended features are provided by other modules. Caching is provided by mod_cache and related modules. The ability to contact remote servers using the SSL/TLS protocol is provided by the SSLProxy* directives of mod_ssl. These additional modules will need to be loaded and configured to take advantage of these features.
http://httpd.apache.org/docs/2.0/mod/mod_proxy.html
http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/mod_proxy.html

IP-based

I am configuration for testing only using Host-based, and the other way is Ip-based alse can implement virtual host. As the term IP-based indicates, the server must have a different IP address for each IP-based virtual host. This can be achieved by the machine having several physical network connections, or by use of virtual interfaces which are supported by most modern operating systems (see offical documentation for details, these are frequently called "ip aliases", and the "ifconfig" command is most commonly used to set them up).if you want to know more about that, please see:
http://httpd.apache.org/docs/2.0/vhosts/ip-based.html
http://www.apacheweek.com/features/vhost
http://apache.active-venture.com/vhosts/ip-based.html

mod_jk

Virtual host is a way to connect apache with tomcat,and there is a other way we reference above - mod_jk,the connecting of application /tsalex in server 192.168.0.4 is made with mod_jk(made by alexandria group in xiancaro).mod_jk is a replacement to the elderly mod_jserv. It is a completely new Tomcat-Apache plug-in that handles the communication between Tomcat and Apache. if any one want to know more about it ,please see
offical mod_jk documnt

for finishing the configuration,I accept some help from others ,thanks to haitao jiang,jim and members of alexandria. the documents below you can consult, if you have interesting in doing the configuration work


http://lamp.linux.gov.cn/Apache/ApacheMenu/index.html
http://tomcat.jaxwiki.org/proxy-howto.html
http://httpd.apache.org/docs/1.3/vhosts/ip-based.html
http://httpd.apache.org/docs/2.0/vhosts/examples.html

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载