Squid Optimization Guide

  

A lot of squid optimizations are limited to adjustments on squid parameters and system parameters. But this is really a little bit of a thing, as long as it is not too mentally ill configuration can not be cached, Squid performance will not have much difference, it will increase by about 10%, only the actual business for Squid to make some adjustments, Squid will really burst Out of his energy, there are often 100%-200% improvement.

This article is basically a directional guide, does not involve details like a specific configuration file, so most of the content in this article can not be copied-paste into the configuration file. .

First of all, be clear about what Squid can do. Many people don't understand how Squid works. Just heard that Squid's performance is good enough to speed up the website. It just puts a squid in front of its website. This is basically useless, even if you are a static page, behind apache. The above does not open mod_expires, the same cache, Squid can only play a connection management.

Generally speaking, the website is accelerated with Squid, the purpose is two

1: Squid itself has a caching function, which can cache the contents of the webserver output, and access the cache before it expires. Both use the contents of the cache directly, which can effectively reduce the number of requests on the webserver machine. This is the main function of squid. 2: Users with slow network will occupy the TCP connection of the webserver for a long time. The webserver occupies a large amount of resources for each connection. If the service cannot be released for a long time, the performance will have a relatively large impact. Putting a squid in front, the webserver can quickly process the logic, send the data to squid quickly, and then process other logic, and squid takes up very little resources per TCP connection, so don't worry about taking up too much resources. This use is also called connection management, there are some network devices can do this, the price is very expensive.

The following two functions for Squid, how to adjust the business logic and Squid parameters

Zero: Pre-operation

Before you engage in Squid, no matter what configuration you use to compile , what special options are needed, please enable-snmp, and configure mrtg and so on, you can graphically display the squid state, such as Request Hit Ratio (RHR), Byte Hit Ratio (BHR), etc., feedback is to do everything The basis, optimization is no exception.


One: Cache

A: Use the Expires header to control the cache

Squid needs some control from the backend webserver when caching the webserver content The information tells him if the page can be cached and how long it can be cached. Otherwise Squid will not make a self-assertion to cache content for you. Whether a page can be cached or not, only the people who develop the website know, so developers are responsible for outputting Expires and Cache-Control headers in the dynamic page. Simply give an example of php to illustrate what the values ​​of these two headers mean, where $expiretime is in seconds.

header("Expires: " . gmt_date_format(time()+$expiretime));header("Cache-Control: max-age=" . "$expiretime");

For static files, there are two ways to have Squid automatically cache static files. One is to use apache's mod_expires to automatically output the cache header for the path or for the file type/extension. For details, please refer to the description of mod_expires. The other is specified with squid's refresh_pattern. For details, please refer to the squid configuration file. In general, if the backend is not cumbersome to configure, it is recommended to do it on the backend. Most of the configuration changes in the frontend are against the http protocol. If there is a problem, it is more difficult to troubleshoot.


B According to the mode of Squid access, business splitting

After the Expires Header processing, Squid can really accelerate, you may It can also be felt that the access speed of the website is significantly faster. But don't settle for this score. Check out squid's snmp chart. Usually the hit ratio is not too high, and 50% is amazing. This is what we need to further optimize. Our goal is to achieve a 9X% hit rate for most Squids.

Why is Squid hit so low? There are two reasons for this. Most websites have some pages that cannot be cached, such as login pages. These page requests also go from Squid and become part of the denominator, which directly reduces the hit rate. The first thing we can do is to split these uncacheable page requests into a single Squid, or have a small amount of access. If you just, expose the apache. The squid hit rate that can be cached immediately rises.

Some people may say that if you can split a page that cannot be cached, just to make the number that can be cached look good, isn't it a cover-up? In fact, it makes sense to do this. The first is to remove the interference that cannot cache the page, which makes our basis for further optimization of Squid more accurate. Secondly, there is usually a gap between the importance of non-cacheable requests and cacheable requests. After splitting, it is not easy for them to seize resources from each other. It will not fill up the Squid because of too many connections to download images. Important login request. The third is that the cacheable content is usually a page element such as a picture. When the browser loads them, there is control over the concurrent connection of each site. If it is separated into different IPs, more requests can be executed at the same time. Increase the display speed.

In fact, if you look at pages like sohu, sina, you will find that their pages are also split. You can see that the images in the page are all pointing to addresses like images.sohu.com, although they may Like other pages, the background points to the same apache.

After doing this, the cache hit rate can rise to 70%-80%, and when you are lucky, you can get 90%.

Another reason for the low Squid hit is similar to this one. It is also cacheable content, some may be large files on the software download station, some are small pictures on the news site, if the same If a squid speeds up such a huge file, it will seriously interfere with Squid's caching strategy. The two can't take care of it either, or the large file occupies the cache, the small files are squeezed out of the cache, or the small files are especially large. Large files cannot enter the cache, causing large files to often miss. This is more disgusting than a page that cannot be cached, so even if the server resources are limited, the two types of access should be split first. Generally speaking, the file size dividing line is set at about 1M. If there is a file with a large size such as software download, it can be split once again in 4M - 10M. For different access types of squid, the system optimization parameters will be different, which we will talk about later.

As long as you carefully split the business according to the access mode, most of the cached squids can achieve a high hit rate, at least 9X%.


C Adjusting parameter optimization cache according to different needs

After completing A and B two-step optimization, Squid's hit rate can often reach 9x%, we can say that we Already given to Squid

Copyright © Windows knowledge All Rights Reserved