环境:ubuntu 12.04 + apache2 + tomcat
1.首先在tomcat中配置好jsp站点,映射端口8080
2.启用apache2的反向代理
apache2的配置文件都保存在/etc/apache2/中,启用反向代理既可以用a2enmod实现,也可以用ln手动设置
ln创建链接方式:
ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
ln -s /etc/apache2/mods-available/proxy.conf /etc/apache2/mods-enabled/proxy.conf
ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
a2enmod方式:
a2enmod proxy
a2enmod proxy_http
3.修改proxy.conf
vi /etc/apache2/mods-enabled/proxy.conf
配置为
<Proxy>
Order deny,allow
Allow from all
</Proxy>
4.修改apche2中的站点配置
apache2的站点配置默认保存在/etc/apache2/sites-enabled/文件夹中,默认是default
将站点localhost:80/8080映射到tomcat,文件配置如下:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ProxyRequests Off
<Location /8080>
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
</Location>
#ProxyPass / http://localhost:8080/
#ProxyPassReverse / http://localhost:8080/
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
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 ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/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>
5.重启apache2,测试
打开http://localhost/ 显示apache页面
打开http://localhost/8080 显示tomcat页面
如果有多个目录要映射,并且映射到不同站点的目录有父子关系的,要注意配置文件的顺序,如:
<Location /8080>
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
</Location>
<Location />
ProxyPass http://website/
ProxyPassReverse http://website/
</Location>
使用Apache2反向代理不仅能让同一端口jsp和php共存,还能实现负载均衡等功能。
此文章由 http://www.ositren.com 收集整理 ,地址为:
http://www.ositren.com/htmls/67899.html