Leverage browser caching in nginx

yaashul

Member
May 3, 2017
7
0
1
India
cPanel Access Level
Root Administrator
In which file should I add this code?


Code:
location ~* \.(js|jpg|jpeg|gif|png|css|tgz|gz|rar|bz2|doc|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|woff|woff2|svg)$ {
expires 30d;
add_header Pragma "public";
add_header Cache-Control "public";
}
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
Hey there! I can't say I'm familiar with that exact configuration, but details on the caching with our Nginx reverse proxy installation can be found here:

 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
Thanks for that clarification. I believe this portion of that link would be still be relevant:

"If your application uses GET or HEAD requests for dynamic content and does not include cache control headers, the server may return cached responses that do not apply to the user.

To correct this issue, perform one of the following actions:
  • Disable caching for the user in WHM’s NGINX Manager interface (WHM >> Home >> Software >> NGINX Manager).
  • Modify your web application to use the appropriate headers so NGINX does not cache the request."
The specific configuration options you provided would work well with Apache, so these would not need to be placed in a separate area for the Nginx service, as our Nginx tools proxy requests to Apache. You would likely want to create a custom include file to apply that to the overall Apache configuration:

 

tbutler

Active Member
Feb 11, 2004
41
5
158
I'm trying to figure out this question, too. The NGINX Manager suggestion doesn't apply since this is about _browser_ caching rather than _server side_ caching. I created a new include in server-includes with code like @yaashul's but it doesn't seem to get included. I was able to add it to the conf.d/users/[username] folder, but I'd like to apply it to all users, not have to do it individually for each username. Is there a way to apply it to every NGINX user?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
I spoke with one of the Nginx developers about this issue and while using the include system is the only option, you could make it a bit easier with symlinks. For example, if your username is "username" you could do this:

-create your custom cache configuration file in /etc/nginx/conf.d/includes-optional/
-use symlinks to that like this: /etc/nginx/conf.d/users/username/cache_customizations.conf would be a symlink to /etc/nginx/conf.d/includes-optional/cache_customizations.conf

That would save you from needing to recreate the file each time.
 

tbutler

Active Member
Feb 11, 2004
41
5
158
Thanks for your help. That does help, at least! The documentation seems to suggest there should be a global way to do this:

If you want to adjust every server block on your server, create your .conf file in the /etc/nginx/conf.d/server-includes/ directory.
Is that no longer supported?
 

tbutler

Active Member
Feb 11, 2004
41
5
158
Thanks, I tried that, but it didn't seem to do anything when I added it in server-includes. Which makes some sense, because the cPanel generated template for each server block doesn't reference the conf.d/server-includes directory for some reason.
 

tbutler

Active Member
Feb 11, 2004
41
5
158
Thanks! Do you think they’d be receptive to a feature request? It doesn’t just have to be about caching (although being able to follow Google’s official “Lighthouse” best practices on this matter — when it is such a straight forward fix seems like a worthy request), but it’d also be relevant for *any* other reason one may need to deploy an adjusted nginx setup for one’s clients, not just this one issue.