Score:1

RewriteRule with flag [L] stopping caching?

cn flag

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.

kz flag
How exactly are you setting the `Expires` header? Where are you using these directives? What other directives do you have? "in apache **and** nginx" - how does Nginx fit into this? (Aside: All modern browsers use the `Cache-Control` + `max-age` header over the `Expires` header.)
cn flag
Thanks, I have edit my question. Cache-Control is working, but why not ExpiresByType and why static files its not processing by nginx at all?
Score:1
kz flag

why static files its not processing by nginx at all?

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.

ExpiresByType text/javascript A31556952

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 be application/javascript instead these days. In other words:

ExpiresByType application/javascript A31556952

To clarify, mod_expires ExpiresByType sets both the Expires and Cache-Control: max-age HTTP response headers. Cache-Control: max-age takes priority on all modern browsers. Expires is really only for legacy support.

Flag [L] in RewriteRule must stop static files processing by nginx

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).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.