Dynamic hostname and port proxying with apache - apache

Is it possible to use apache to proxy a hostname and port dynamically like so:
/<PORT>/<HOSTNAME> -> http://<HOSTNAME>.domain.local:<PORT>
I've tried the using ProxyPassMatch:
ProxyPassMatch "^/([0-9]+)/(host-[0-9]+)$" "http://$2.domain.local:$1"
But apache throws a syntax error AH00526. This is using apache 2.4.7.

From Apache Docs:
The URL argument must be parsable as a URL before regexp substitutions
(as well as after). This limits the matches you can use.
The only workaround I can think of is to use mod_rewrite with [P] flag:
RewriteEngine On
RewriteRule "^/([0-9]+)/(host-[0-9]+)$" "http://$2.domain.local:$1" [P]
(But this comes with performance penalty, and also keep in mind that with such dynamic proxying you can not use ProxyPassReverse to adjust the URL in HTTP redirect responses)

Related

Restricting one URL from Apcahe httpd reverse proxy rules

I have Apache server serving as a reverse proxy for one of Angular application.
Defined Reverse proxy rules in ssl.conf like below
ProxyPass http://10.10.10.101/xyz/abc
ProxyPassReverse http://10.10.10.101/xyz/abc
And also defined Rewrite conditions
RewriteCond %{REQUEST_URI} !^/xyz/abc(.*)$
But in my code i have one file coming with same URL like /xyz/abc/header.html.
I want to restrict that URL not to go through reverse proxy rule.
How can i restrict it, can some one help me out with this issue?
To make exceptions to proxy you just append "!" at the end, like:
ProxyPass /xyz/abc/header.html !

Redirect Without changing URL Apache

I want to redirect one URL to another without changing the Browser URL
www.example.com/abc/(.*).xml should redirect to www.example.com/abc/xyz/index.htm?file=$1
But the Browser should display www.example.com/abc/(.*).xml
You can use a RewriteRule:
RewriteEngine On
RewriteRule /abc/(.*)\.xml$ /abc/xyz/index.htm?file=$1 [L]
Make sure you have mod_rewrite enabled and put this either in your VirtualHost config, or in a .htaccess file in your DocumentRoot
As Constantine posted on the accepted solution, the [P] flag is dangerous as it converts the server as a proxy.
See [this]: https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever?noredirect=1&lq=1
P = Proxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-server fetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad.

mod_rewrite Cannot redirect without rewriting url

I'm trying to redirect to another server application running on a different port of the same box with apache HTTPD as the frontend server running on port 80. I'm using the rewrite engine and the following syntax to redirect calls:
RewriteRule ^/?products/(.*)$ http://www.example.com:9000/$1 [L]
This works great, but the url changes to port 9000 in the browser. Whenever I change the [L] to a [P] for proxy I get a 404, not found error in the browser.
How do I fix this so that the url doesn't change in the browser, but that it still works?
You need to make sure you have mod_proxy loaded, otherwise the P flag won't work.
You can also use the ProxyPass directive as part of mod_proxy in your vhost config.

Reverse Proxy in CakePHP?

I've got a CakePHP application, and the following directives in my httpd.conf
ProxyRequests off
ProxyPass /forum/ http://somesite.com/phpbb3
ProxyPass /gallery/ http://someothersite.com/gallery3
<Location /forum/>
ProxyPassReverse /
</Location>
<Location /gallery/>
ProxyPassReverse /
</Location>
Without CakePHP this works fine - but because CakePHP is using it's own redirection logic from routes.php and other sources, it seems to override any proxy settings, so any call to "/community" on my server follows the default pathway of looking for a Controller called CommunityController.
My issue here is that I want to have one server that serves muliple applications, but keep it seamless to the user - so a complete PHPBB application can for instance run within the "/forum" directory as if it were a controller in CakePHP.
Has anyone done this before, and can it be done? Why does mod_rewrite and/or the routes.php file override my mod_proxy directives??
Perhaps instead of using mod_proxy, you could use mod_rewrite to create a RewriteRule directive with the [P] (proxy) flag in conjunction with the [L] (last rule) flag.
'proxy|P' (force proxy):
This flag
forces the substitution part to be
internally sent as a proxy request and
immediately (rewrite processing stops
here) put through the proxy module.
You must make sure that the
substitution string is a valid URI
(typically starting with
http://hostname) which can be handled
by the Apache proxy module. If not,
you will get an error from the proxy
module. Use this flag to achieve a
more powerful implementation of the
ProxyPass directive, to map remote
content into the namespace of the
local server.
Note: mod_proxy must be enabled in
order to use this flag.
'last|L' (last rule):
Stop the
rewriting process here and don't apply
any more rewrite rules. This
corresponds to the Perl last command
or the break command in C. Use this
flag to prevent the currently
rewritten URL from being rewritten
further by following rules. For
example, use it to rewrite the
root-path URL ('/') to a real one,
e.g., '/e/www/'.

How do I send users to a different "site" in Apache while also using mod_proxy?

I have a web site that I administer that uses Apache 2.0 on the front with Tomcat 6 on the back-end (with mod_proxy proxying the connection between the two).
I want to be able to use the same VirtualHost to also access the phpMyAdmin application running on the box. So, for example, www.mywebsite.com will pass all traffic to Tomcat, but www.mywebsite.com/myadmin (with obvious security) will pass traffic to phpMyAdmin.
How about using mod_rewrite instead of mod_proxy?
You can use the P modifier to send certain request (i.e. all those that aren't to /phpmyadmin*) through a proxy. This actually uses mod_proxy internally.
Something like this (not tested):
RewriteCond %{REQUEST_URI} !^/phpmyadmin
RewriteRule ^.*$ http://tomcat/$0 [P,L]