I use server management software plesk with apache and nginx. I set in apache and nginx a expired header for javascript files for one year. That's working. I can see one year for "expires" in header.
Now I want access javascript and php files with a rewrite (without redirect).
APACHE
ExpiresActive On
ExpiresByType text/javascript A31556952
RewriteEngine On
RewriteRule fake/(.+\.(?:js|php))$ original/$1 [L]
NGINX
location ~* ^/(.*\.js)$ {
try_files $uri @fallback;
expires 1y;
add_header Cache-Control "public";
}
That RewriteRule is working. I can access this files, but the "expires" header in "fake/file.js" is gone. What am I doing wrong?
example.com/original/file.js = expires in one year
example.com/fake/file.js = no header for expires
SOLUTION All static files are normally processing by NGINX, not apache. So set in apache this line for javascript give it a cache.
Header set Cache-Control "max-age=31556952, public"
Flag [L] in RewriteRule must stop static files processing by nginx and ExpiresByType is ignored.
Because
/fake/file.js
is no longer a "static file" (it doesn't exist), so is passed through to Apache, where it is internally rewritten to get the required response.text/javascript
may not be the "correct" mime-type for JavaScript responses on your server. You need to confirm what mime-type (ie.Content-Type
header) your server is sending back with these responses, but this would normally beapplication/javascript
instead these days. In other words:To clarify, mod_expires
ExpiresByType
sets both theExpires
andCache-Control: max-age
HTTP response headers.Cache-Control: max-age
takes priority on all modern browsers.Expires
is really only for legacy support.The
L
flag simply stops the current pass of the rewrite engine on Apache. These directives themselves do not influence Nginx (which would seem to be acting as a front-end proxy).