Mahadevan
Mahadevan

Reputation: 73

Conditional reverse proxy in apache

if request comes from public ip 50.12.95.78. Proxypass to http://192.168.1.100/quote url.

if request comes from other public ip . Proxypass to http://192.168.1.100/ url

What is the settings for it in apache? Reverse proxy wont work in if condition.

Upvotes: 0

Views: 3181

Answers (1)

Chris H
Chris H

Reputation: 36

You can set up a conditional reverse proxy in Apache httpd by utilizing mod_proxy and mod_rewrite.

For instance, if you originally had your reverse proxy set up with the following configuration:

ProxyPass        / http://192.168.1.100/
ProxyPassReverse / http://192.168.1.100/

It can be made conditional by using the proxy flag on a RewriteRule. An example configuration could look like this:

RewriteEngine On
ProxyPassInterpolateEnv On
RewriteCond "%{REMOTE_ADDR}" =50.12.95.78
RewriteRule (.*) http://192.168.1.100/quote$1 [P,E=proxy_pass_path:/quote]
RewriteRule (.*) http://192.168.1.100$1 [P]
ProxyPassReverse / http://192.168.1.100${proxy_pass_path}/ interpolate
  1. The first RewriteRule is applied and proxied only if the preceding RewriteCond directive is matched, i.e. if the Remote IP Address is 50.12.95.78.
    • The [P]roxy flag also prevents further RewriteRule directives from being applied.
    • The environmental variable proxy_pass_path is set to tell the ProxyPathReverse directive that the path has been modified.
  2. The second RewriteRule is automatically applied if the first did not evaluate.
  3. Finally, the ProxyPathReverse directive is modified to rewrite response headers based on the proxy_pass_path variable.

Going beyond the scope of your question, bear in mind that this kind of proxy will not implement any real form of security. IP addresses other than 50.12.95.78 will still be able to access http://192.168.1.100/quote simply by requesting http://<Proxy_Host>/quote.

Upvotes: 2

Related Questions