Apache configuration proxy pass - apache

I want to configure a server to listen to localhost adress with proxy pass.
Fist, I added a proxy pass configuration like that:
ProxyPass /test http://localhost:5959/ nocanon
ProxyPassReverse /test http://localhost:5959/
ProxyPassReverse /test http://domain_name.net/
The index is working fine, but the problem is when i click on some button to be redirected to another page. I expected the redirection would be like : http://domaine_name.net/test/anotherpage.php but it didn't work.
I tried another configuration but it didn't work
ProxyPass /test/ http://localhost:5959/test/
ProxyPassReverse /test/ http://localhost:5959/test/
ProxyPassReverse /test/ http://domain_name.net/test/
How can I resolve this issue ?

Do not put 2 ProxyPassReverse directives. Only one. And it should match the ProxyPass destination. So your first ProxyPassReverse only is required.
ProxyPass /test http://localhost:5959/ nocanon
ProxyPassReverse /test http://localhost:5959/
From the documentation for ProxyPassReverse, (https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassreverse):
This directive lets Apache httpd adjust the URL in the Location,
Content-Location and URI headers on HTTP redirect responses. [...]
You said in your question:
I expected the redirection would be like :
http://domaine_name.net/test/anotherpage.php but it didn't work.
this is normal. A proxy hides the final destination server to the client. If you want to redirect the client, do not use a proxy. Use mod_rewrite (RewriteRule and others).

Related

How to put Qbittorrent webui port behind a https Apache2 server?

So basically, i have a apache2 server with https where i run some application
I am tring to using mod_proxy to proxy all traffic to url example.com/qb to [::1]:qb-webui-port.
So in /etc/apache2/mods-enabled/proxy.conf, i wrote:
ProxyRequests Off
<proxy *>
AddDefaultCharset off
Order Allow,Deny
Allow from all
</proxy>
ProxyPass /transmission http://[::1]:9091/transmission
ProxyPassReverse /transmission http://[::1]:9091/transmission
ProxyVia On
ProxyPass /qb http://[::1]:8112
ProxyPassReverse /qb http://[::1]:8112
The above is my similiar configuration for Transmission, i intended to do the same trick to Qbittorrent.
But it only returned plain html from example.com/qb.
In the firefox console i noticed that there were some request towards example.com/css, example.com/script etc.
This make me confused.
Can anyone provide some insights on this one?
Thx.
You missed the trailing slash on your addresses.
Here's my config file. I added a RewriteRule in case I enter the URL without the trailing slash. With these configs I haven't needed to modify anything else to get qb reverse proxy working.
I use the "/torrent/ subdir to access my qbittorrent webUI and it's listening on the 8080 port, so you should modify this in order to get your installation fully functional.
# Para qbittorrent
RewriteEngine on
RewriteRule ^/torrent$ "/torrent/$1" [R]
ProxyPass /torrent/ http://127.0.0.1:8080/
ProxyPassReverse /torrent/ http://127.0.0.1:8080/

ProxyPassReverse does not keep correct protocol while redirecting

I have set up a Virtuoso server that serves Linked Data using content negotiation. The server is served through a reverse proxy Apache server and may be queried using http or https.
http://public.server.org/myapp --> http://private.local.domain:1234/
https://public.server.org/myapp --> http://private.local.domain:1234/
The Virtuoso server then performs content negotiation and redirects to /describe?...
I have no problem when accessing the public server through http. The redirection takes place and content is retrieved.
However, when I access the public server though https, the redirection sends me to http://public.server.org/describe?... (that is HTTP, not HTTPS).
I'm expecting to be redirected to https://public.server.org/describe?... (with the same protocol as the original query).
My configuration is:
<VirtualHost xxx.yyy.zzz.ttt:80>
ServerName public.server.org
ProxyPass /myapp http://localhost:8890/myapp
ProxyPassReverse /myapp http://localhost:8890/myapp
ProxyRequests Off
<Location /describe>
ProxyPass http://localhost:8890/describe
ProxyPassReverse /describe
</Location>
</VirtualHost>
<VirtualHost xxx.yyy.zzz.ttt:443>
ServerName public.server.org
ProxyPass /myapp http://localhost:8890/myapp
ProxyPassReverse /myapp http://localhost:8890/myapp
ProxyRequests Off
<Location /describe>
ProxyPass http://localhost:8890/describe
ProxyPassReverse /describe
</Location>
</VirtualHost>
Is it possible for apache to correctly reverse the proxy in order to maintain the original query protocol while redirecting?
After debugging with dumpio and Apache error logs, I think I found the problem.
What happened?
First, my configuration was incorrect. At another place I did not transcribe here, I had a ProxyPreserveHost On directive that was active. Hence, the configuration worked but for wrong reasons.
Apache was keeping the Host and Virtuoso used this host. Hence, when sending a redirect to /describe..., Virtuoso was redirecting to http://public.server.org/describe... instead of my expected http://localhost:8890/describe...
Hence, the redirection was not captured by the ProxyPassReverse directives and passed unchanged to the client (and it worked). The problem being that the redirection was always done through http, regardless of the original query scheme.
Solution
I decided to drop the ProxyPreserveHost On directive and rely on a correct ProxyPassReverse directive.
For an unknown reason, I could not figure out the correct setting inside the Location, hence I used the settings:
<VirtualHost xxx.yyy.zzz.ttt:443>
ServerName public.server.org
ProxyPass /myapp http://localhost:8890/myapp
ProxyPassReverse /myapp http://localhost:8890/myapp
ProxyRequests Off
ProxyPreserveHost Off # To avoid problems if it is set On elsewhere.
ProxyPass /describe http://localhost:8890/describe
ProxyPassReverse /describe http://localhost:8890/describe
</VirtualHost>
Note: I only changed the https settings as the http ones were functioning somehow (hence the http virtualhost still uses ProxyPreserveHost On and no ProxyPassReverse

Configure Apache with multiple ProxyPass

i am trying to configure my apache server as proxy to serve two internal services , one listening on 8080 and should receive traffic on specific URL and the other listening on 8077 and should receive all other http traffic
I deployed and configured apache on the same server where these two services running and it is listening to 443 along with all SSL configuration and it is working fine
also I enabled the proxy_module, proxy_http_module and proxy_http2_module
What I want to achieve
if the requested URL is /webhook1 --> pass it to EP1 http://localhost:8080 and
any other requested URL should be passed to EP2 http://localhost:8077
My Current Configuration towards the first service
ProxyPass /webhook1 http://localhost:8080
ProxyPassReverse /webhook1 http://localhost:8080
Now I want to define another proxy pass to be something like
ProxyPass / http://localhost:8077
ProxyPassReverse / http://localhost:8077
putting both configuration together is not working , appreciate your help in how to configure apache to achieve my requirement
Thank you in advance
Put the ProxyPass rules in the correct order as required
if you want to evaluate /webhook1 rule and send it to 8080, else send the traffic to 8077 the rules should be on the following order
ProxyPass /webhook1 http://localhost:8080
ProxyPassReverse /webhook1 http://localhost:8080
ProxyPass / http://localhost:8077
ProxyPassReverse / http://localhost:8077
You may write ssl.conf file under /etc/apache2/sites-enabled/ as follows:-
RewriteEngine on
ProxyPass /webhook1 http://127.0.0.1:8080/
ProxyPassReverse /webhook1 http://127.0.0.1:8080/
RewriteRule ^/$ /webhook1/ [R,L]
RewriteEngine on
ProxyPass / http://127.0.0.1:8087/
ProxyPassReverse / http://127.0.0.1:8087/
RewriteRule ^/$ /EP2/ [R,L]
It will automatically redirects to HTTPS if ssl certificate is configured in apache2.

Do I need to use ProxyPassReverse if I'm using mod rewrite?

I am using mod rewrite to mask the context root of my application. For example,
RewriteRule ^/directory/(.*) balancer://appcluster/directory/$1 [P]
The appcluster looks like this:
<Proxy balancer://appcluster>
BalancerMember http://localhost:8080/App route=app_01 keepalive=On loadfactor=1 ttl=300 min=3 smax=5 max=15
ProxySet lbmethod=byrequests stickysession=JSESSIONID|jsessionid timeout=120 nofailover=On
</Proxy>
Do I need to use ProxyPassReverse at all? I used to use it because my old webserver code looked like this:
ProxyPass /App balancer://appcluster lbmethod=byrequests stickysession=JSESSIONID|jsessionid timeout=120 nofailover=On
ProxyPassReverse /App http://localhost:9013/App
The ProxyPassReverse is used to change the headers sent by the app (appcluster) to Apache, before Apache sends it the browser. For example, if the app sits at http://localhost:9013/, and it tries to redirect the browser to, say, /new_location/, then it will respond with a redirect and location header of http://localhost:9013/new_location/, and Apache will take this and send it off to the browser. Problem is, the browser (assuming it's somewhere else) then tries to send a request to http://localhost:9013/new_location/, and gets an error.
What ProxyPassReverse does is intercepts those headers, and rewrites them so that they match what the Apache server that's doing the proxying looks like. So if my apache server is hosting http://myhost.com/ and I have a ProxyPass that points / to http://localhost:9013/App, if the application sitting at localhost:9013 returns a redirect to http://localhost:9013/App/new_location/, I'll need to use ProxyPassReverse so that it gets rewritten to http://myhost.com/new_location/ by Apache before sending the request back to the browser.
If you aren't issuing redirects, it's not going to be an issue, but it doesn't hurt to have it there in case a 301/302 redirect is returned. As far as mod_rewrite, the RewriteRule applies to the request going to the App, and not the response coming from the App. So they are mutually exclusive events.

apache reverse proxy changes url

i tried to configure apache's reverse proxy on a server to redirect the requests from /hotm to http://gateway.messenger.hotmail.com
typing the addres xxx.xxx.xxx.xxx/hotm in a browser, the request is redirected correctly, but the url in the address bar changes to "http://gateway.messenger.hotmail.com".
it's possible to configure the proxy on apache so that the address does not change?
[edit]
this is the httpd.conf fragment
ProxyRequests Off
ProxyPass /hotm http://gateway.messenger.hotmail.com
ProxyPassReverse /hotm http://gateway.messenger.hotmail.com
<Directory /var/www/html/hotm>
Order Allow,deny
Allow from all
</Directory>
Redirect Permanent /hotm http://gateway.messenger.hotmail.com
You must also set the ProxyPassReverse directive, typically to the same url as your ProxyPass value.