Setup Apache as Reverse Proxy for Tomcat Server
Setup Scenario
Tomcat is running on port 8080 and I have configured two sample applications running with following urls.- http://localhost:8080/sample
- http://localhost:8080/calendar
- http://example.com
>> http://localhost:8080/demo1/ - http://example.net
>> http://localhost:8080/demo2/ - http://domain.com/demo1/
>> http://localhost:8080/demo1/ - http://domain.com/demo2/
>> http://localhost:8080/demo2/
Let’s start configuration
1. Enable Mod Proxy Apache Module
By default this module is enabled in Apache for users who installed using rpm packages. If you don’t have enabled edit your Apache configuration /etc/httpd/conf/httpd.conf or for Apache 2.4 /etc/httpd/conf.modules.d/00-proxy.conf file and uncomment following lines or put in file.LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so
2. Configure Apache Virtual Hosts
Now will start working with virtual host. We are creating three virtual hosts as below. You create only which is required with needed modifications. Edit Apache main configuration file and start with the configuration.Setup VirtualHost-1:
To forward all requests sent to example.com to backend tomcat server corresponding application like:- http://example.com
>> http://localhost:8080/demo1/
<VirtualHost *:80> ServerName example.com ProxyRequests On ProxyPass / http://localhost:8080/demo1/ ProxyPassReverse / http://localhost:8080/demo1/ <Location "/sample"> Order allow,deny Allow from all </Location> </VirtualHost>
Setup VirtualHost-2:
To forward all requests sent to example.net to backend tomcat server corresponding application like:- http://example.net
>> http://localhost:8080/demo2/
<VirtualHost *:80> ServerName example.net ProxyRequests On ProxyPass / http://localhost:8080/demo2/ ProxyPassReverse / http://localhost:8080/demo2/ <Location "/"> Order allow,deny Allow from all </Location> </VirtualHost>
Setup VirtualHost-3:
To forward all requests sent to sub directory /demo1/ or /demo2 on http://domain.com to back-end tomcat corresponding applications like:- http://domain.com/demo1/
>> http://localhost:8080/demo1/ - http://domain.com/demo2/
>> http://localhost:8080/demo2/
<VirtualHost *:80> ServerName domain.com ProxyRequests On ProxyPass /demo1 http://localhost:8080/demo1/ ProxyPassReverse /demo1 http://localhost:8080/demo1/ ProxyPass /demo2 http://localhost:8080/demo2/ ProxyPassReverse /demo2 http://localhost:8080/demo2/ <Location "/demo1"> Order allow,deny Allow from all </Location> <Location "/demo2"> Order allow,deny Allow from all </Location> </VirtualHost>
3. Restart Apache and Test
After making all necessary changes restart Apache service using following command and access your sites in web browser. Make sure you are getting proper pages from tomcat.# service httpd restart
Source : http://tecadmin.net
No comments:
Post a Comment