0

when configuring web servers (nginx) is it uncommon to set and expire time and cache every element in every directory that gets requested by the client browser?

some examples of expire time i just found on the nginx site and servervault:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
}


location ~* \.(?:css|gif|jpe?g|png)$ {
    expires max;
}

how would i write the location line if i wanted to experiement with setting an expiration to every element?

location ~* \.(?:*)$ {
    expires 2d;
    add_header Pragma public;
    add_header Cache-Control "public"
}
  • 1
    Nginx only evaluates one location block. You're going to need to include that in every block that might serve content. I have some useful information available [here](https://www.photographerstechsupport.com/tutorials/hosting-wordpress-on-aws-tutorial-part-4-wordpress-website-optimization/). – Tim Jul 04 '17 at 23:07
  • Thanks! Which part of that article should I be looking at exactly? There's a lot of information there, it's a little overwhelming. I'm still new to nginx. – user423490 Jul 04 '17 at 23:53
  • Look at the heading "Setting Cache Control Headers" – Tim Jul 05 '17 at 00:10
  • Thank you, that article was indeed informative. I'm having trouble with the wildcard in the location line though. `pcre_compile() failed: nothing to repeat in "\.(?:*)$" at "*)$`. How do I tell nginx to consider ALL files and elements? – user423490 Jul 05 '17 at 02:07
  • I have no idea. You'd need to edit your question to clearly show the configuration in use, the error message, and any applicable logs before we could start to consider that question. – Tim Jul 05 '17 at 04:05
  • Your regex is not correct. It says "search for a period followed by nothing between 0 and any times and nothing more behind" since you try to apply a modifier to "nothing", it breaks. If you want to keep the regex, just replace the second parameter to location for a single period: `location ~* . {` – NuTTyX Jul 16 '20 at 10:02

1 Answers1

0

When you want to match everything, you can just use:

location / {
    expires 2d;
    add_header Pragma public;
    add_header Cache-Control "public"
}

However, if you have any kind of dynamic content like user log-ins, pages that are updated over time etc., it is better to set the caching headers inside the application itself, and then let nginx cache data according to the cache headers. This is a far too big subject to go through in detail in ServerFault though.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63