[Apache httpd] A simple example of reverse proxy, balancer and rewrite configuration

Download a fresh apache httpd binary installer, current latest version 2.4, and installed it.

Enable modules in httpd.conf

Need do following modifications in httpd.conf

...
Define SRVROOT "<where your apache is located>"
...
## choose the port you use to listen to the request
Listen 9000
...
# enable proxy module  
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
# enable rewrite module
LoadModule rewrite_module modules/mod_rewrite.so
# enable balancer module
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
...
Include conf/extra/httpd-vhosts.conf

Config reverse proxy and balancer in httpd-vhosts.conf

<VirtualHost *:9000>
    ServerAdmin neo.wangbo@gmail.com
    DocumentRoot "${SRVROOT}/docs/localhost"
    ServerName localhost
    ServerAlias localhost
	
	LogLevel warn rewrite:trace2
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
	
	<Proxy "balancer://localcluster"> 
		BalancerMember "http://localhost:8001" 
		BalancerMember "http://localhost:8002"
	</Proxy>
	
	<Location "/balancer-manager">
		SetHandler balancer-manager
		Require host localhost
	</Location>
	
	ProxyPreserveHost On
	ProxyRequests Off
	ProxyPass / "balancer://localcluster/"
	ProxyPassReverse / "balancer://localcluster/"
</VirtualHost>

Then start httpd.

Testing

I have a weblogic server running at the same vm with apache. And 2 managed servers are listening respectively to 8001 and 8002.
Access http://localhost:9000, the request should reach weblogic

Add rewrite in httpd-vhosts.conf

WebLogic application URL is /webex, I am going to use rewrite to filter the URL. If the request URL does not start with /webex the request would be rejected.
Add following part,

<VirtualHost *:9000>
    ServerAdmin neo.wangbo@gmail.com
    DocumentRoot "${SRVROOT}/docs/localhost"
    ServerName localhost
    ServerAlias localhost
	
	LogLevel warn rewrite:trace2
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common

	#################################################
	# Filter URL: if not match /webex/... reject
	RewriteEngine on
	RewriteCond %{REQUEST_URI} !^/webex(/?|/.*?)$
	RewriteRule .* - [F,L]
	#################################################
	
	<Proxy "balancer://localcluster"> 
		BalancerMember "http://localhost:8001" 
		BalancerMember "http://localhost:8002"
	</Proxy>
	
	<Location "/balancer-manager">
		SetHandler balancer-manager
		Require host localhost
	</Location>
	
	ProxyPreserveHost On
	ProxyRequests Off
	ProxyPass / "balancer://localcluster/"
	ProxyPassReverse / "balancer://localcluster/"
</VirtualHost>

Reference

Reverse proxy
https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
Proxy balancer
https://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html
Rewrite
https://httpd.apache.org/docs/2.4/rewrite/intro.html
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值