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/
The "ProxyPass" and "ProxyPassReverse" parameters are used to tell Apache how to proxy requests. They require the "mod_proxy.so" and "mod_proxy_http.so" Apache modules, which are loaded by default in RHEL5 and RHEL6, but check the following lines are uncommented in the "/etc/httpd/conf/httpd. conf" file to make sure.
ProxyPass is the main proxy configuration directive. In this case, it specifies that everything under the root URL ( / ) should be mapped to the backend server at the given address.
The ProxyPreserveHost directive is used to instruct Apache mod_proxy, when acting as a reverse proxy, to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With