ProxyHTML to rewrite URL - apache

I've got 2 apache servers set up. One on port 80 and another on port 8077. I'm wanting to see everything on the server on 8077 via a reverse proxy. At the moment I've got:
ProxyPreserveHost Off
ProxyHTMLInterp On
ProxyPass /translate/ http://www.example.com:8077/
ProxyPassReverse /translate/ http://www.example.com:8077/
ProxyHTMLURLMap / /translate/
This allows me to get to the initial page of the site, but the links to images, css and other pages don't work.
For example the css in the html shows as
/css/style.css
where I actually want it to be
/translate/css/style.css
For it to pick up the file from the 8077 server. What can I do with the current setting to get that to work?

Ok, this is what I ended up doing to get it working
ProxyPass /translate/ http://www.example.com:8077/
ProxyPassReverse /translate/ http://www.example.com:8077/
ProxyHTMLURLMap http://www.example.com:8087 /translate/
<Location /translate/>
ProxyPassReverse /
SetOutputFilter proxy-html
ProxyHTMLURLMap http://www.example.com:8077 /translate/
ProxyHTMLURLMap / /translate/
ProxyHTMLURLMap /translate/ /translate/
RequestHeader unset Accept-Encoding
</Location>
This seems to work well. The ProxyHTMLURLMap http://www.example.com:8077 /translate/ line was only needed to translate some "referrer" based urls that caused some pages to end up trying to serve directly from the 8077 port server.

Related

Apache Reverse Proxy not redirecting URL with special charcaters

I have set up a reverse proxy using apache mod_proxy, and it is working fine for all the URLs I set up, except some URLs which contain special characters (specifically *)
I tried encoding the special characters, as well as using the nocanon flag. But none seems to work. I'm basically trying to run kibana behind a RP, and need to eliminate/replace the special character (use , instead of *) used by Kibana to query elasticsearch. Below is my reverse proxy configuration -
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
# works fine
ProxyPass /kibana http://10.19.225.20:5601/app/kibana
ProxyPassReverse /kibana http://10.19.225.20:5601/app/kibana
# works fine
ProxyPass /elasticsearch http://10.19.225.20:5601/elasticsearch
ProxyPassReverse /elasticsearch http://10.19.225.20:5601/elasticsearch
# Doesn't work
#ProxyPass /elasticsearch/.kibana/_mapping/*/field/_source http://10.19.225.20:5601/elasticsearch/%2Ekibana/%5Fmapping/%2A/
#ProxyPassReverse /elasticsearch/.kibana/_mapping/*/field/_source http://10.19.225.20:5601/elasticsearch/%2Ekibana/%5Fmapping/%2A/
# Doesn't work
#ProxyPass /elasticsearch/%2Ekibana/%5Fmapping/%2A/ http://10.19.225.20:5601/elasticsearch/%2Ekibana/%5Fmapping/index-pattern%2Csearch%2Cconfig/ nocanon
#ProxyPassReverse /elasticsearch/%2Ekibana/%5Fmapping/%2A/ http://10.19.225.20:5601/elasticsearch/%2Ekibana/%5Fmapping/index-pattern%2Csearch%2Cconfig/
# Doesn't work, just a test to use , char in target
#ProxyPass /elasticsearch/.kibana/index-pattern http://10.19.225.20:5601/elasticsearch/.kibana,kib2/index-pattern nocanon
#ProxyPassReverse /elasticsearch/.kibana/index-pattern http://10.19.225.20:5601/elasticsearch/.kibana,kib2/index-pattern
#RewriteEngine on
#RewriteRule ^/index-pattern /_search?fields=
# Doesn't work
ProxyPass /elasticsearch/kibana/%5Fmapping/%2A http://10.19.225.20:5601/elasticsearch/kibana/%5Fmapping/index%2Dpattern%2Csearch%2Cconfig nocanon
ProxyPassReverse /elasticsearch/kibana/%5Fmapping/%2A http://10.19.225.20:5601/elasticsearch/kibana/%5Fmapping/index%2Dpattern%2Csearch%2Cconfig
# Doesn't work
#ProxyPass /elasticsearch/.kibana/_mapping/* http://10.19.225.20:5601/elasticsearch/.kibana/_mapping/index-pattern,search,config
#ProxyPassReverse /elasticsearch/.kibana/_mapping/* http://10.19.225.20:5601/elasticsearch/.kibana/_mapping/index-pattern,search,config
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
In the commented lines, are the various things I tried but none seems to be working. Specifically I'm trying to replace the URL 'http://10.19.225.20/elasticsearch/kibana/_mapping/*/field/source?=some_number' generated by Kibana with http://10.19.225.20/elasticsearch/.kibana/_mapping/index-pattern,search,config/field/source?=some_number I think I need some URL Rewrite Rule, in addition with RP configuration, but not sure how to do it as I haven't used Rewrite Rules before.
Any help/suggestions would be appreciated.

Apache 2.2 reverse-proxy behind Nexus 3

The apache server is configure with following items :
<VirtualHost *:80>
...
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /jenkins/ http://localhost:8080/ timeout=300
ProxyHTMLURLMap http://localhost:8080 /jenkins
<Location /jenkins/>
ProxyPassReverse /
ProxyHTMLEnable On
ProxyHTMLExtended On
ProxyHTMLURLMap / /jenkins/ [L]
RequestHeader unset Accept-Encoding
</Location>
ProxyPass /nexus/ http://localhost:8081/ timeout=300
ProxyHTMLURLMap http://localhost:8081 /nexus
<Location /nexus/>
ProxyPassReverse /
ProxyHTMLEnable On
ProxyHTMLExtended On
ProxyHTMLURLMap / /nexus/ [L]
RequestHeader unset Accept-Encoding
</Location>
ProxyVia On
</IfModule>
Jenkins proxy works fine.
Nexus proxy load the page but cannot load resources. The HTML page ressources are not defined with the right path, instead of myhost.domain/nexus/static/... the URLs are myhost.domain/static/.... What's going wrong in the configuration ?
The solution is to switch Nexus context path configuration to '/nexus/'. In general, it seems that the nexus context path must match the proxy context path.

double proxy statements for apache

As you can see I am trying to get both / and /BowlingFacelets/faces/ to point to the same place.
The problem is that the JSF pages will add /BowlingFacelets/faces/ when I sumbit the forms. I however don't want the end user to have to type such a long statement to start the app. Is there a way to do this?
Note that this will work if I manually add
score.megahooked.com/BowlingFacelets/faces to the URL
ProxyPass / http://megahooked.com:8080/
ProxyPassReverse / http://megahooked.com:8080/
This will not work correctly
score.megahooked.com/createEvent.xhtml
since when submitting the new URL will be
score.megahooked.com/BowlingFacelets/faces/updateEvent.xhtml which is not found.
<VirtualHost *:80>
ServerName score.megahooked.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://megahooked.com:8080/BowlingFacelets/faces/
ProxyPassReverse / http://megahooked.com:8080/BowlingFacelets/faces/
ProxyPass /BowlingFacelets/faces/ http://megahooked.com:8080/BowlingFacelets/faces/
ProxyPassReverse /BowlingFacelets/faces/ http://megahooked.com:8080/BowlingFacelets/faces/
</VirtualHost>
The solution could be the using of mod_proxy_html. It can parse HTML documents and rewrite URLs inside. Here for example I make the application "restfrontend" (mapped to "/restfrontend") accessible as root application (mapped to "/")
ProxyHTMLEnable On
ProxyHTMLExtended On
ProxyHTMLMeta On
ProxyHTMLLogVerbose On
ProxyHTMLURLMap ^/restfrontend()$ $1 [LR]
ProxyHTMLURLMap /restfrontend/ /
ProxyPassReverseCookiePath /restfrontend /
ProxyHTMLDoctype HTML
I would strongly dis-advice to do it. This module is very hard to configure. Configuration of two possible paths in the same time will be a nightmare.
Much better solution is to deploy the application as ROOT directly in the Tomcat and add some redirects for additional URLs shortcuts (RewriteRule with [R=301] option).

Apache reverse proxy to https server

I'm trying to setup reverse proxy to some web service, so I can develop JavaScript Ajax application on localhost. I have managed to do it with Apache following way:
ProxyPass /serviceproxy/ http://someservice.com/
ProxyPassReverse /serviceproxy/ http://someservice.com/
ProxyHTMLURLMap http://someservice.com/ /serviceproxy/
<Location /serviceproxy>
ProxyPassReverse /
ProxyHTMLEnable On
ProxyHTMLURLMap / /serviceproxy/
ProxyHTMLURLMap http://someservice.com/
RequestHeader unset Accept-Encoding
</Location>
That works fine for http webpages, but not for https pages. If I set proxy to https://someservice.com/, Apache returns 500 Internal server error. I'm new to Apache configuration. How to make it work? Is it even possible?
thanks
You have to enable Ssl module (a2enmod ssl) Then in /etc/apache2/sites-available/default file include following lines
RequestHeader set X-Forwarded-Proto "https"
ServerName localhost
SSLProxyEngine On
RequestHeader set Front-End-Https "On"
Between line
ServerAdmin webmaster#localhost
and line
DocumentRoot /var/www
Hope it helps.

Apache reverse proxy: how to redirect relative URLs in external site back to itself?

I need to bypass cross-site scripting restrictions in order to show users a map when they click a link from an external site which I have loaded inside an iframe (external.com/onlyforme). I learned that the easiest way to do this is to set up a reverse proxy so that Apache would retrieve external.com/onlyforme when I access local.com/external, and make it so that it appears to be coming from my domain.
This mostly works, but when external.com/onlyforme/index.html tries to access external.com/onlyforme/site_media/script.js, this gets redirected to local.com/site_media/script.js, which is not what I want. Instead, I would like this to be redirected to the correct URL inside external.com/onlyforme, so that the external site works as expected.
How can I do that?
I have this in my httpd.conf, outside any other config statements:
ProxyRequests Off
ProxyPass /external/ http://www.external.com/onlyforme
ProxyPassReverse /external/ http://www.external.com/onlyforme
I am running Apache 2.2.
You need to add a couple of ProxyHTMLURLMap directives to the above, to inspect and rewrite any hard coded URL's in the returned HTML e.g.
ProxyRequests Off
ProxyPass /external/ http://www.external.com/onlyforme
ProxyHTMLURLMap http://www.external.com/onlyforme /external
<Location /external/>
ProxyPassReverse http://www.external.com/onlyforme
SetOutputFilter proxy-html
ProxyHTMLURLMap / /external/
ProxyHTMLURLMap /site_media /external/site_media/
</Location>
See also: http://wiki.uniformserver.com/index.php/Reverse_Proxy_Server:_mod_proxy_html
arober11's answer greatly helped solving my similar issue. I tried to narrow it down to the shortest set of rules possible, and there is my own configuration to have an Etherpad running at https://my-domain-name.wtf/pad :
<Location /pad>
ProxyPass http://localhost:9001 retry=0
# retry=0 => avoid 503's when restarting etherpad-lite
ProxyPassReverse http://localhost:9001
SetOutputFilter proxy-html
ProxyHTMLURLMap http://localhost:9001
</Location>
RewriteRule ^/pad$ /pad/ [R]