using list of variables in apache conf file - apache

Is there a way to pass list of variables and then process them one by on in conf file ?
<IfModule proxy_module>
<Proxy balancer://mycluster>
BalancerMember http://host1:4001
BalancerMember http://host2:5001
</Proxy>
ProxyPass /someuri/ balancer://mycluster/someuri/
ProxyPassReverse /someuri/ balancer://mycluster/someuri/
</IfModule>
Above works fine to load balance, but number of instances to load balance can increase 2 to n. And i want to pass that list of hosts from outside using mod_macro or something.
Something like below
1> set vars
hosts="host1:4001","host2:5001"
2>use it
<IfModule proxy_module>
<Proxy balancer://mycluster>
for(host in $hosts)
{
BalancerMember host
}
</Proxy>
ProxyPass /someuri/ balancer://mycluster/someuri/
ProxyPassReverse /someuri/ balancer://mycluster/someuri/
</IfModule>

This is not really possible, the closest thing that's available is mod_macro but that's for repeating large blocks, not expanding lists inside existing sections.
This kind of thing could be implemented a handful of ways, it just doesn't exist yet.

Related

how to config apache point to another index in different server?

I am configuring 3 tiers setup(webserver, database server, and application server). I want to separate everything into 3 three servers. I have already separated between the application server and the database server. Regarding to webserver and application server, I don't know how to config apache to point to my application server. I tried to share file and folder from application server to webserver already with samba share, but it still did not work. The problem is apache server can access the resource in the other server(application server).
If you everyone used to solved or faced this problem. Could you please help me?
Thank in advance.
As a part of an old assignment I had achieved something similar using below config.
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
<Proxy "balancer://mycluster">
BalancerMember "http://10.0.0.1:8001"
BalancerMember "http://10.0.0.1:8002"
BalancerMember "http://10.0.0.1:8003"
BalancerMember "http://10.0.0.1:8004"
BalancerMember "http://10.0.0.1:8005"
BalancerMember "http://10.0.0.1:8006"
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / "balancer://mycluster/" stickysession=BALANCEID
ProxyPassReverse / "balancer://mycluster/"
</VirtualHost>
For your case, I feel changing your Virtual Host as below must do the magic.
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
<Proxy "balancer://mycluster">
BalancerMember "http://192.168.2.35:8000"
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / "balancer://mycluster/" stickysession=BALANCEID
ProxyPassReverse / "balancer://mycluster/"
</VirtualHost>
Also ensure that you enable the lbmethod_byrequests_module in your apache.
You just need a basic reverse proxy configuration. The absolute basics are loading mod_proxy, mod_proxy_http, and using ProxyPass to match the URL's you want to pass to the backend system.

Apache load-balancer: direct to specific application based on URL

I have multiple applications deployed in Tomcat's webapps folder (app1.0, app1.1, app1.2 etc.). When I hit www.example.com:8080/app1.0, the corresponding application appears.
But how to do it on the load-balancing server? For instance, I have a website on which I can click a button (app1.0, app1.1, app1.2 etc.) and an URL pops up like: www.lb.com/app1.0/.../... How to direct to the app based on application version in URL? Use RewriteCond and regex and pass it to ProxyPass? I don't really how to script it, anyone could help? :)
Edit: This is what I done for the 2 apps for 1 Tomcat and 2 apps for 2 Tomcat, but I got 404 sometimes because the Tomcat that has another version has been chosen by the load-balancer.
<VirtualHost *:80>
#Add a http header to explicitly identify the node and be sticky
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
#Declare the http server pool
<Proxy "balancer://plf">
BalancerMember "http://worker1.com:8080" route=worker1
BalancerMember "http://worker2.com:8080" route=worker2
ProxySet stickysession=ROUTEID
ProxySet lbmethod=bybusyness
</Proxy>
#Common options
ProxyRequests Off
ProxyPreserveHost On
#Declare the redirection for the http requests
ProxyPassMatch "/app(.*)" "balancer://plf/app$1"
ProxyPassReverse "/app(.*)" "balancer://plf/app$1"
This is how I did it:
1) define a balancer proxy:
<Proxy balancer://portalcluster stickysession=JSESSIONID>
BalancerMember ajp://TOMCATSERVER1:8009 route=TOMCARSERVER1-0
BalancerMember ajp://TOMCATSERVER2:8009 route=TOMCATSERVER2-100
</Proxy>
2) proxy to it in your VirtualHost:
Listen 443
<Virtualhost *:443>
ServerName example.com
Alias /static /var/www/portalstatic
ProxyPass /static !
ProxyPass / balancer://portalcluster/
ProxyPassReverse / balancer://portalcluster/
</Virtualhost>
NB I removed a lot of configuration from these, that are not related to the question (logs, deny clauses, certificate directives, ...). This is just to illustrate the way I did the proxy.
NB2 I did leave the /static trick since this is usually something you will want to do. Static files must stay on the HTTP, and not send them from Tomcat all the time.

How to use an Apache load balancer with Bokeh, with HTTP and WS protocols?

I'm trying to get Apache to work with more than one Bokeh server. Everything works fine with one server behind a reverse-proxy, but when I try configuring a load balancer, I keep getting an internal server error. I know there is some information about load balancing in the Bokeh docs, but it's only explained for Nginx, and I'm having a hard time linking the dots.
I'm guessing it has something to do with the websocket protocol. I've read a bit about this, and I've seen examples for simple HTTP load balancers, but not for WS and HTTP load balancers.
Do I have to create 2 clusters, one for HTTP and one for WS? How can I work with WS and HTTP wihtin a load balancer?
I have included what I have for now in my Apache config. Please note that I have only one server per cluster for testing purposes.
<Proxy balancer://mycluster>
# Server 1
BalancerMember http://localhost:5100/foo
</Proxy>
<Proxy balancer://mycluster/ws>
# Server 1
BalancerMember ws://localhost:5100/foo/ws
</Proxy>
<VirtualHost *:80>
ServerName localhost
CustomLog "/var/log/apache2/access.log" combined
ErrorLog "/var/log/apache2/error.log"
ProxyPreserveHost On
<Location /foo>
ProxyPass balancer://mycluster
ProxyPassReverse balancer://mycluster
</Location>
<Location /foo/ws>
ProxyPass balancer://mycluster/ws
ProxyPassReverse balancer://mycluster/ws
</Location>
Alias /static /var/www/static
<Directory /var/www/static>
# directives to effect the static directory
Options +Indexes
</Directory>
</VirtualHost>

mod_proxy isn't using the port defined in my balancermembers

I'm trying to set up my apache load balancer to proxy to the same backend clusters to apps running on different ports. The cluster definitions are like this:
<Proxy balancer://wordpress-cluster>
BalancerMember http://192.168.2.10:80
BalancerMember http://192.168.2.11:80
</Proxy>
<Proxy balancer://corporate-cluster>
BalancerMember http://192.168.2.10:81
BalancerMember http://192.168.2.11:81
</Proxy>
In the load balancer, one of the vhost needs to talk to both, so in the vhost definition I have:
ProxyRequests Off
ProxyPreserveHost Off
SSLProxyEngine On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /feed balancer://wordpress-cluster/feed lbmethod=byrequests
ProxyPassReverse /feed balancer://wordpress-cluster/feed
ProxyPass / balancer://corporate-cluster/ lbmethod=byrequests
ProxyPassReverse / balancer://corporate-cluster/
when requesting for '/' , I get the content served from the port 80 app, not the port 81 app.
Would anyone know on what is happening? it looks like this might be a case of overzealous worker sharing, but shouldn't specifying the different ports prevent that?
Is there something else I should do?
Forgot to mention: this is using apache 2.2.4-1 on a centos box.
Thanks in advance!
Tim
OK, so I still don't know what happened. Did 2 steps to fix my problem:
1) split the cluster into 2 1-machine clusters and things worked:
<Proxy balancer://wordpress-cluster>
BalancerMember http://192.168.2.10:80
</Proxy>
<Proxy balancer://corporate-cluster>
BalancerMember http://192.168.2.11:81
</Proxy>
But of course that killed balancing and failover support... not good
Second option, create dns aliases to ensure that the balancermembers looked different (even though they point to the same boxes:
<Proxy balancer://wordpress-cluster>
BalancerMember http://192.168.2.10:80
BalancerMember http://192.168.2.11:80
</Proxy>
<Proxy balancer://corporate-cluster>
BalancerMember http://corp01:81
BalancerMember http://corp02:81
</Proxy>
And voila! The setup is now working for me :)

Apache mod-proxy load balancer maintenance

I have mod-proxy and mod-proxy-balancer setup as a load balancing reverse proxy. Something like this:
<Proxy balancer://example>
BalancerMember http://hostname:8000 keepalive=on
BalancerMember http://hostname:8001 keepalive=on
</Proxy>
ProxyPass / balancer://example/
ProxyPassReverse / balancer://example/
ProxyPreserveHost on
ProxyRequests Off
Is there a simple way to set this up to show a static maintenance page when all members of the balancer group are down? I've done that with a hardware load balancer previously and it was very useful.
Maybe you can use a hot standby. The example below is from the ProxyPass Directive section where it says "Setting up a hot-standby, that will only be used if no other members are available"
ProxyPass / balancer://hotcluster/
<Proxy balancer://hotcluster>
BalancerMember http://1.2.3.4:8009 loadfactor=1
BalancerMember http://1.2.3.5:8009 loadfactor=2
# The below is the hot standby
BalancerMember http://1.2.3.6:8009 status=+H
ProxySet lbmethod=bytraffic </Proxy>
As an alternative to RewriteRule you can do the same thing with appropriate ErrorDocument directives. We do something like this in which the proxy server itself hosts static error pages and the "hot-standby" host is http://localhost/some-app/.
Since your proxy seems to be the only page (probably in a VirtualHost), you can simply override error pages. Apache produces a 503 error, so this would look like:
# Document root is required because error documents use relative paths
DocumentRoot /var/www/html/
# Allow access to document root directory
<Directory /var/www/html/>
Order allow,deny
allow from all
</Directory>
# Actual change: If service is unavailable (no member available), show this page
ErrorDocument 503 /maintenance.html
If you want to use images inside the maintenance html, please not that you have to use absolute paths (e.g. /image.jpg) will load /var/www/html/image.jpg.