I'd like to redirect traffic from http://198.51.100.13/site1/ to the server's port 1001.
This works (but don't forget to a2enmod proxy and a2enmod proxy_http first!):
RewriteEngine On
RewriteRule ^/site1(.*)$ http://localhost:1001$1 [P,L]
I have read that it should be possible to do it that way, instead of using RewriteRule:
ProxyPass /site1 http://localhost:1001/
ProxyPassReverse /site1 http://localhost:1001/
However, it doesn't work for me for now (Proxy Error, The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /site1. Reason: DNS lookup failure for: localhost:1001site1).
Question: What's the benefit of solution 2 instead of simple URL rewriting (solution 1)? and how to correct solution 2 to make it work?
Related
I'm trying to forward all requests from domain1.com to domain2.com, without redirect using mod_proxy and mod_rewrite. My .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^(.*)$ http://domain2.com/$1 [L,P]
Actually this isn't working because it produces an invalid host. The target site domain2.com complains about invalid host, that it seems to be the following:
domain1.com, domain1.com
Yes, with comma. Any idea is much appreciated.
If I dump the server variables i get domain1.com, domain1.com as SERVER_NAME and HTTP_HOST.
EDIT: I solved looking at the source code doing the check.
AFIK it seems pretty normal to double the host value. Why? Because setting the IP of the proxy as trusted proxy the magic happens: the host get splitted by ',' and the last will be used as host, that is domain1.com. It works.
Sending requests from one domain to another is a perfect example for ProxyPass.
ProxyPreserveHost On
ProxyPass / http://domain2.com
ProxyPassReverse / http://domain2.com
This requires the modules proxy and proxy_http, but should solve your problem.
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.
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.
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.
I am trying to use Apache as a proxy for an internal tomcat server, but the tomcat server performs a redirect that got me crazy
I first tried mod_proxy through:
ProxyPass / ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse / ajp://127.0.0.1:8045/bv-via-web/
When accessing / (root) the internal server performs a redirect to /bv-via-web/hola which doesn't exist. It should redirect to /hola. How can I achieve this through the Apache config?
As a workaround I though I add the following:
ProxyPass /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/
But that doesn't work as it will extend the url to an internal request of bv-via-web/bv-via-web/hola. So basically I want to change the url but don't know how that is possible with mod_proxy.
I then tried mod_rewrite, with the following:
RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]
But then when I open the root of the website it performs a redirect and http://127.0.0.1:8040/bv-via-web/hola appears in the browser address.
I don't understand, as it should work as an internal proxy due to the [P] flag.
What am I doing wrong ?
I solved it by adding:
ProxyPreserveHost On
Such it's not forwarded to the 127.0.0.1 as mentioned above.
So the total config snippet is:
RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]