We are trying to configure apache to forward requests to different servers hosting different Application servers.
We want to achieve the following.
www.mydomain.com/server1 --->forward to ---> 172.30.34.50:8082 (AP1 jboss)
www.mydomain.com/server2 --->forward to ---> 172.30.34.51:8082 (AP2 jboss)
Our current configuration:
mod_jk.conf:
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
httpd.conf
JkMount /cliente1* ajp13unsecure
RewriteEngine on
RewriteLog logs/apache-mod_rewrite.log
RewriteLogLevel 3
RewriteRule ^/cliente1(/)?([^/]*)/?$ /$2 [L,PT] (tried w/o PT with no success)
The problem is that when mod_jk
forwards the request, it gets to the application servers like this: 172.30.34.50:8082/server1
Which is not a valid resource in the application server, it should go to root ( 172.30.34.50:8082
)
We tried using mod_rewrite
, but it triggers before mod_jk
, so when apache try to match mod_jk
rule, it doesn't satisfy the condition anymore. And the request is not forwarded.
How can we get mod_rewrite
to trigger right before mod_jk
does the forwarding so the application servers get the correct URI.
Or is there a way to configure mod_jk
to forward request without the context?
How can we get
mod_rewrite
to trigger right beforemod_jk
does the forwarding so the application servers get the correct URI.
Not too sure why I have noticed an uptick in people asking about mod_jk
, but in my experience mod_proxy
works better & is easier to understand for doing what you are attempting to do.
To enable mod_proxy
in Apache do the following; assuming you are on Ubuntu/Debian:
sudo a2enmod proxy proxy_http
Then restart Apache like this; again assuming you are on Ubuntu/Debian:
sudo service apache2 restart
That done, this is a simple setup that should work within your Apache config. Winging it based on your settings:
# Settings for adding a trailing slash to the URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(server1|server2)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
# Settings for Apache Reverse Proxying
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass /server1 http://172.30.34.50:8082/
ProxyPassReverse /server1 http://172.30.34.50:8082/
ProxyPass /server2 http://172.30.34.51:8082/
ProxyPassReverse /server2 http://172.30.34.51:8082/
</IfModule>
The initial mod_rewrite
settings add a trailing slash to URLs which I found I needed to do in cases where a path fragment—like /server1
and /server2
—were going through a reverse proxy.
Also note I have /server1
and /server2
set but they might need to have a slash added to them like this /server1/
and this /server2/
. Experiment to see what works best.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.