Squid cache - part 2

Squid is not only a proxy and cache service, it can do much more: manage acl (access lists), filter content, it can even do ssl filtering even in transparent mode (proxy method - without having to configure in proxy settings from their browsers, it's like man in the middle, nobody knows it's there). So I commonly see how the full potential of this application is wasted by not knowing how to configure each of its parts.

Now the interesting thing that squid does is the cache (in my opinion). You will tell me, why cache? The reason is simple, better managing the use of your speed and bandwidth is the main thing. Think carefully, 1000 people in your company consulting every 5 minutes, common pages, Google, Hotmail, Gmail, etc ... so that you are going to download images, banners, advertising, html content over and over again, all these are static things, no They change so frequently, better to have them stored in your local network and you deliver a copy that you consider recent within the configurations that you considered.

How to do it? Simple with the following sentence:

refresh_pattern [-i] regex min percent max [options]

As I always say, don't believe everything, so I encourage you to read from the official source. I recommend reading the manual for this ruling HERE.

The refresh_pattern statement will always be our tag for adding new parameters to cache.

Important, your cache lists must be sequential, because once it matches the first one that matches the object, it will not continue reading your other rules.

Regular expressions are case-sensitive, so flv is not the same as FLV, but you can avoid this if you wish by using the -i option . Then it would look like this: refresh_pattern -i

'Min': This is the time (in minutes) an object will be considered "fresh" if it doesn't have an explicit "expired" tag. By default, Squid recommends 0, because some dynamic applications might behave strangely. This value should really be a number you consider useful and effective for the elements you want to cache. For example, 1440 minutes (one day) seems fine to me for JPGs; it's not like the images in a post on a website change every 5 minutes.

'Percent' is the percentage of an object's age (since the last modification) that will be considered "recent" or "fresh." For example, if you're constantly reloading or refreshing a webpage to see the latest modifications, Squid might consider that if the object has reached, say, 50% of the time between the minimum and maximum values , it will download that object from the internet again and give you a fresh copy.

'Max' is the limit above or equal to 'Min' for how long an object is considered "recent or fresh". Suppose an image on a page was only viewed once by a user; that object has already reached its min time , but not the max time . So when it is viewed again, a cached copy will be delivered.

Options:
override-expire
override-lastmod
reload-into-ims
ignore-reload
ignore-no-store
ignore-private
max-stale=NN
refresh-ims
store-stale

These options were made mostly to ignore pre-established behaviors in languages ​​and protocols, in order to guarantee the effective use of the cache.

override-expire

It enforces the minimum time of an object, even if the server sent a shorter expiration time (example things like header or Cache-Control: max-age). If we do this, a "warning" will appear saying things like this "VIOLATES the HTTP standard" but that is just warnings that we can ignore. Now if the time a server sends is longer then squid will take the time (expiration) of the server

override-lastmod

Reinforces the minimum time of an item, even if that item was recently modified.

reload-into-ims

The short explanation is that it prevents that when we press the refresh button or make a no-cache request, squid will deliver cache if it has not been "modified since" and / or if there is no "headers" on the page.

ignore-reload

Ignore the action of users to press the reload or refresh page button

ignore-no-store

Ignore any rule in headers not to cache, for example of videos

ignore-private

Ignore any rules in private content headers that should not be cached, example: facebook content.

refresh-ims

Squid contacts the server, to guarantee if the object is the newest. If it is then it will deliver cache

store-stale

Squid will save all of those responses, even if they don't have an expiration date, this is very impractical as they usually cannot be reused. If you decide to enable it you must declare max-stale = NN

max-stale=NN

If you enabled the above, you must declare a maximum lifetime for that response or factor. Squid does not deliver objects of this style but can validate it with the source

Here is a table of how the state of fresh "FRESH" works according to the values ​​we have discussed:

  • FRESH if expire> now, else STALE
  • STALE if age> max
  • FRESH if lm-factor <percent, else STALE
  • FRESH if age <min else STALE

Here is an example configuration for a specific company with a lot of disk space, good equipment and good bandwidth

refresh_pattern -i \.(3gp|7z|ace|asx|bin|deb|divx|dvr-ms|ram|rpm|exe|inc|cab|qt)$ 43200 99% 43200 ignore-no-store ignore-must-revalidate override-expire override-lastmod reload-into-ims

refresh_pattern -i \.(rar|jar|gz|tgz|bz2|iso|m1v|m2(v|p)|mo(d|v)|arj|lha|lzh|zip|tar)$ 43200 99% 43200 ignore-no-store ignore-must-revalidate override-expire override-lastmod reload-into-ims

refresh_pattern -i \.(jp(e?g|e|2)|gif|pn[pg]|bm?|tiff?|ico|swf|dat|ad|txt|dll)$ 43200 99% 43200 ignore-no-store ignore-must-revalidate override-expire override-lastmod reload-into-ims

refresh_pattern -i \.(avi|ac4|mp(e?g|a|e|1|2|3|4)|mk(a|v)|ms(i|u|p)|og(x|v|a|g)|rm|r(a|p)m|snd|vob)$ 43200 99% 43200 ignore-no-store ignore-must-revalidate override-expire override-lastmod reload-into-ims

refresh_pattern -i \.(pp(t?x)|s|t)|pdf|rtf|wax|wm(a|v)|wmx|wpl|cb(r|z|t)|xl(s?x)|do(c?x)|flv|x-flv)$ 43200 99% 43200 ignore-no-store ignore-must-revalidate override-expire override-lastmod reload-into-ims
cache_mem 8092 MB

Now caching isn't just on the hard drive; we can also cache in RAM. This value is per Squid process, so you should take it into account when using redirectors like SquidGuard.

maximum_object_size_in_memory 1024 KB

The maximum size of the object in memory that squid will store in RAM. You can also declare a minimum.


memory_replacement_policy heap GDSF
cache_replacement_policy heap GDSF

As you can see, one is the policy for the replacement of cache in RAM memory and another in hard disk. There are 2 policies GDSF and LFUDA. The first seeks to improve the percentage of cache hits, having many small objects on hand, the second seeks the opposite, it keeps objects in cache regardless of their size.

The question that I imagine you are asking me at this moment is, what use do I use? Well, if you consider that in your environment they do a lot of queries and few downloads use GDSF if on the contrary they do a lot of downloads and few LFUDA queries. That if I recommend LFUDA when you are going to do, I don't know, cache in 1TB of disk, it is more efficient.

maximum_object_size 4 MB

The maximum size that an object can have to be stored

cache_dir aufs /media/proxy249/cache 100 16 256

Where the cache will be stored, attention here, important is if you use ufs, aufs or diskd, all 3 work more or less the same, the difference is that aufs and diskd work with separate processes to do the I / O operations on the hard disk and avoid that squid processes hang during these operations, additionally diskd you can specify the number of threads that you will have for this task. I recommend aufs if you have a good team.

Size 100 (megabytes), you can put 100000 is almost 100GB depends on your availability. 16 is the number of folders, and 256 is the number of sub-folders. You can play with both values ​​depends on how fast your discs are and how much resources you have.


cache_swap_low 90
cache_swap_high 95

These options are the replacement values ​​of objects, it is the minimum and maximum value as a watermark according to squid, where these numbers are in percentage (%), and in a very small cache, 5% like this right now would be let's say 300 objects per second , but in very large caches we would be talking about thousands of MB

Well, that's all for now. Comment, and also take into consideration those who told me they couldn't cache and filter HTTPS (SSL) pages in Squid 3.5 or higher; I'll bring it to you soon, so stay tuned to this blog.


Add as preferred source in Google