当前位置: 首页 > 工具软件 > Simple HTTPD > 使用案例 >

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

周涵畅
2023-12-01

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

 类似资料:

相关阅读

相关文章

相关问答