2

I'm using an htaccess rule to proxy to an internal server, using the answer recommended on this question, "Can ProxyPass and ProxyPassReverse Work in htaccess". I'm using htaccess as that is all I have access to. The method suggested works, but when I make a change on one of the internal pages and reload (from the external server) I don't even see it hitting the internal server, even after clearing the cache on the browser. In fact, if I try to load the page from another browser which never has tried to load the page before, it too gets the old copy.

This suggests something is being cached on the server, but how to change this? The apparent caching is rather annoying as I am trying to fix some issues that only occur on the proxied page.

If I hit the internal server directly and reload after a change, I always get the latest page.

I have tried a <filesMatch ...> rule for the affected pattern (using the same pattern as used in the RewriteRule in the following manner:

<filesMatch "^/?somedir/(.*)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</filesMatch>

My rewrite rule looks like this, and comes after the filesMatch directive:

RewriteEngine On
RewriteRule ^/?somedir/(.*)$ https://internal.local.net:8000/$1 [L,P]

But this has not had any effect. I have also tried "NoCache *" but this directive causes an error as it is not allowed in an .htaccess file.

Community
  • 1
  • 1
Michael
  • 9,060
  • 14
  • 61
  • 123

2 Answers2

4

The P-flag in your RewriteRule causes the request to be proxied to the internal server using mod_proxy. mod_proxy by itself does not cache content. The caching is probably a result of mod_cache being enabled as well on the server. The settings you need to disable caching for your internal server can unfortunately only be done in server or virtual-host config. The solution would be to add what you tried to the configuration of the internal server thus telling mod_cache that it should not cache any response from your internal server:

Using .htaccess

Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"

or PHP

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.
lsmooth
  • 566
  • 1
  • 5
  • 12
  • Ok, so it sounds like once this is done, I will have to wait until the cached copy on the external server times out before I will see the desired effect? – Michael Jan 02 '14 at 22:49
  • Also, it sounds like maybe mod_cache is caching more aggressively than the browser would? – Michael Jan 02 '14 at 22:50
  • Yeah you'll have to wait. How mod_cache caches depends on headers and settings. CacheDefaultExpire defaults to 1 hour but that depends on the config of the server. – lsmooth Jan 02 '14 at 22:55
0

Try adding this in an htaccess file in your "somedir" directory:

ExpiresActive On
ExpiresDefault "now"
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • "Directory not allowed here" – Michael Jan 02 '14 at 22:47
  • @Michael I'm not using a `` container. You can't use those containers in an htaccess file. – Jon Lin Jan 02 '14 at 22:49
  • oh sorry, I interpreted "in your 'somedir' directory" as being in the directory directive. There is no actual 'somedir' directory on the external server, the URL gets rewritten before Apache gets a chance to discover that. – Michael Jan 02 '14 at 22:51
  • I tried putting the above directives in my top-level directory, as well as a dummy "somedir" directory I created, but neither fixed the problem. – Michael Jan 02 '14 at 22:53