ProxyPass but exclude certain sub directory - apache

This is what I need to achieve
I need to proxy all requests to /public/ route to another server, except that requests to /public/forms/ must not be proxied and should be served by this apache server.
I have added these directives to my httpd.conf
ProxyPass /public/ http://localhost:3002/public/ retry=10
ProxyPassReverse /public/ http://localhost:3002/public/
It proxies all requests to /public/ to this localhost:3002 but is there any way to exclude /public/forms/ from this proxying?

On top of those because most specific requests should be defined first with ProxyPass add this:
ProxyPass /public/forms/ !
That tells mod_proxy to "not proxy" for that path.

Related

Apache ReverseProxy Domain A Subdirectory to Domain B root

Example website is www.domainA.com.
I want to re-direct one of DomainA subdirectories to DomainB root. For example http://www.DomainA.com/Folder would re-direct to http://www.domainB.com/ to serve the content.
I tried Apache ProxyPass where I am doing the following:
ProxyPass /folder/ http://domainB.com
ProxyPassReverse /folder/ http://domainB.com
However that does not seem to be working as I only get a page not found.
I also tried simply doing
ProxyPass /Folder/ http://domainA.com
ProxyPassReverse /Folder/ http://domainA.com
Just to see if I can redirect /Folder with just an index file back to root of domainA and that did not work either
Try adding this to your domainA.com VirtualHost:
Redirect 302 /folder/ http://domainB.com/
(it is case sensitive)

How to re-write request URL with Apache HTTP Server?

My requirement is quite simple. I have a forward proxy in Apache and clients send SOAP calls to this forward proxy. This proxy needs to be able to rewrite the URL to something else, and forward the request yet another proxy.
This is my configuration so far:
<Proxy *>
Order Allow,Deny
Allow from all
RewriteEngine on
RewriteRule "^(.*)" "https://test.salesforce.com/services/Soap/u/20.0" [P]
</Proxy>
AllowCONNECT 80 443 553 22
# This is the main proxy configuration
ProxyPass /Salesforce http://user:pwd#10.54.167.70:80/ retry=1 acquire=3000 timeout=600 Keepalive=On
ProxyPassReverse /Salesforce http://user:pwd#10.54.167.70:80/
I am not sure if my rewrite rule is at all correct. Can any you please confirm?
The configuration looks so very ugly and I'm not sure what you are trying to do, but still, it really looks as you are just trying to reverse proxy, not forward proxy, I'll try to help with the mistakes anyways the best I can.
The Rewrite, if you are trying to capture a group, I guess you want to use it later:
RewriteRule ^(.*) https://test.salesforce.com/services/Soap/u/20.0/$1 [P,L]
But why a Rewrite?, you are using the P flag, which is used to proxy, what you are trying there is not a rewrite, this a reverse proxy, so why not just:
ProxyPass / https://test.salesforce.com/services/Soap/u/20.0/
AllowConnect, this is to allow SSL forward proxy connections, why do you specify port 80? You want them to go through SSL too? Looks very wrong.
AllowCONNECT 443 553 22
And about your last directives, you need to match slashes for them to work correctly, and also reverse proxy connections should be specified more specific first. First, make sure you do want a forward proxy, but in a forward proxy you can allow/disallow a backend but you don't specify backends, if you specify the backend that is a reverse proxy:
This is briefly what I would go for interpreting what you want, removing all forward proxy related directives:
ProxyPass /Salesforce/ http://user:pwd#10.54.167.70/ retry=1 acquire=3000 timeout=600 Keepalive=On
ProxyPassReverse /Salesforce/ http://10.54.167.70/
SSLProxyEngine on
ProxyPass / https://test.salesforce.com/services/Soap/u/20.0/
ProxyPassReverse / https://test.salesforce.com/services/Soap/u/20.0/
This answer could be refined if you can specify further the "forward proxy" part you mention.

apache2 rewrite rule without changing URL

This below rewrite redirects localhost to http://www.example.com/?id=211&test=1 but I want that localhost on browser should not be changed but the page will come form the above link.
I am using this rewrite rule on my Apache conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
RewriteRule ^/?$ http://www.example.com/?id=211&test=1 [L]
This below rewrite redirects localhost to
http://www.example.com/?id=211&test=1 but I want that localhost on
browser should not be changed but the page will come form the above
link.
If you want to load a page from elsewhere without chaining the URL, mod_rewrite is the wrong tool for the job. Use mod_proxy instead. First enable it in Apache like this; example assumes you are on Ubuntu 12.04 but should work on most any Linux Apache install
sudo a2enmod proxy proxy_http
Then set this to enable a reverse proxy from your root path of / to http://www.example.com/?id=211&test=1:
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com/?id=211&test=1
ProxyPassReverse / http://www.example.com/?id=211&test=1
</IfModule>
EDIT: Seems like mod_proxy and query strings for the destination do not mix; emphasis mine:
This directive allows remote servers to be mapped into the space of
the local server; the local server does not act as a proxy in the
conventional sense, but appears to be a mirror of the remote server.
The local server is often called a reverse proxy or gateway. The path
is the name of a local virtual path; url is a partial URL for the
remote server and cannot include a query string.
So if there is anyway you could set another page—maybe on localhost—that would bounce it behind the scenes. Meaning this happens on localhost:
ProxyPass / bounce.php
And then the file, bounce.php could have this line in it:
<?php
header('Location: http://www.example.com/?id=211&test=1');
?>
Which would allow mod_proxy to have a valid destination. And then the PHP file does the redirect? Hard to say, but the query string on your destination server is the issue.

mod_jk and mod_rewrite with prefix

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 before mod_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.

Can ProxyPass and ProxyPassReverse work in htaccess?

I've never set up a proxy before. I'm using shared hosting, so to set Apache directives, I need to use .htaccess. Can I use .htaccess to do something like below? Any limitations?
ProxyRequests Off
ProxyPass /img/ http://internal.example.com/img/
ProxyPass /app/ http://internal.example.com/app/
ProxyPassReverse / http://internal.example.com/
You cannot use a ProxyPass in an htaccess file. The documentation says it is only applicable in the context:
Context: server config, virtual host, directory
which excludes htaccess (you can't have a <Directory> block in htaccess). However, you can use a ProxyPassReverse to internally rewrite the Location field of proxied requests that cause a redirect. You'll just need to use mod_rewrite's P flag to proxy instead of ProxyPass. So something like:
RewriteEngine On
RewriteRule ^/?img/(.*)$ http://internal.example.com/img/$1 [L,P]
RewriteRule ^/?app/(.*)$ http://internal.example.com/app/$1 [L,P]
ProxyPassReverse / http://internal.example.com/
Just to be clear, you cannot use ProxyPass or ProxyPassReverse in the htaccess file, but you can use ProxyPassReverse with mod_rewrite rules that utilize the P flag.
You can't use ProxyPassReverse, but you can mimic it if you have the ability to rewrite the HTML as it comes back from the origin server.
See my writeup here.