49

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/
Joe Fletcher
  • 2,133
  • 4
  • 28
  • 33

2 Answers2

51

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Brilliant! That really helped me to understand it. Thanks! – Joe Fletcher Oct 10 '12 at 15:59
  • 30
    You cannot use `ProxyPassReverse` in htaccess either (http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypassreverse) – Paul Draper Oct 06 '13 at 03:38
  • This works, but when I make a change on one of the internal pages and reload I don't even see it hitting the internal server, even after clearing the cache on the browser. This suggests something is being cached on the server, but how to change this? – Michael Jan 02 '14 at 21:50
  • 3
    When I use ProxyPassReverse in an htaccess on OSX Server 3.1.1 I receive the following "ProxyPassReverse not allowed here" – Gary Rudolph Apr 04 '14 at 03:44
  • 3
    I'm on OSX 10.9 and I'm also getting ProxyPassReverse not allowed here. – CR47 Apr 17 '14 at 14:27
  • 3
    I use this in .htaccess to fix redirects: `Header edit Location ^http://internal\.example\.com/(.*) http://example.com/$1` – Gianluca P. Feb 25 '15 at 12:03
  • Note that you cannot use this "proxy" with SSL image server. To be able to use SSL you need to add `SSLProxyEngine On` in httpd.conf (or in VirtualHost configuration file). – Nux Mar 03 '15 at 14:45
  • Apache 2.4 doesn't allow ProxyPass in – andig Jan 14 '16 at 12:17
  • 1
    The documentation states that L Flag is implicit set and it warns that using P is performance wise a very bad idea. – sigi Jan 09 '17 at 13:01
  • I still don't understand your post (and specifically don't understand your last line, starting with "Just to be clear"). Like the other commenters here, my server is throwing an error saying that `ProxyPassReverse` is not allowed here (in the `.htaccess` file, presumably, since that's what the docs say). – Ryan Jul 19 '17 at 17:11
  • @Ryan I was trying to point out this bit of the documentation: `Note that this ProxyPassReverse directive can also be used in conjunction with the proxy feature (RewriteRule ... [P]) from mod_rewrite because it doesn't depend on a corresponding ProxyPass directive.` where the `ProxyPassReverse` can be used together with the `P` flag in a Rewrite Rule, but still not in an htaccess file – Jon Lin Jul 22 '17 at 15:54
  • 5
    I was stuck trying to get the proxy stuff working for days, but this really helped. Didn't need the `ProxyPassReverse / http://internal.example.com/` part in the end – Kevin Nagurski May 09 '18 at 11:20
  • The `ProxyPassReverse` rule is to rewrite HTTP redirects so they don't point to `internal.example.com` but to your proxy's address. I'm on shared hosting, so I can't use `ProxyPassReverse`, and I'd like to emulate it from `.htaccess` somehow. – reinierpost Nov 11 '19 at 11:27
  • This answer is accepted with many upvotes, but I think it is wrong or at least incomplete. I think the question means to ask whether the directives can be used in `.htaccess`. They cannot. You do need something equivalent to `ProxyPassReverse`, and the way around it is Gianluca P.'s comment: use `Header edit Location` (which of course requires `mod_headers` being enabled). – reinierpost Dec 13 '19 at 13:26
0

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.

Mike J-P.
  • 121
  • 3
  • Your answer looks good, but one thing here you can add the answer link like this:https://stackoverflow.com/a/74660280/1746258 You added here question link. I advice to change it to your answer link – Ulug'bek Dec 15 '22 at 09:13