1

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.

2
  • 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.)
    – MrWhite
    Jun 28, 2021 at 9:31
  • 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?
    – user706420
    Jun 28, 2021 at 10:24

1 Answer 1

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .