Should compression be done in the program or in the Web server, and should static files be pre-compressed?
Compressing pages and static resources, which can be enabled in the content management program, can be handed over to the Web server, and can also connect both into a chainThe point of judgment is not which layer is more 'advanced', but when the compression cost falls: Dynamic compression needs to be recalculated each time a request is made, pre-compression shifts the cost to the publishing stage, at the cost of maintaining synchronization of two sets of files.
When do the costs of the two methods fall
When assigning dynamic compression to the Web server, the compression action occurs on the way of generating the response. The nginx gzip module itself is a filter that compresses the response using the gzip method, compressing the same content as many times as it is accessed. When there are intensive concurrent requests, this part of the CPU will be抢出来 first.
The pre-compression takes another path: the files are compressed during construction or release, and the server only needs to determine if the client supports it, and then directly send the existing compressed file. The cost is shifted from the request path to the release path, and the price is that the release process needs to add one step, and it must be ensured that the two sets of files are the same version.
Why does the minimum length and type list affect the profit
The benefits of dynamic compression are limited by two switches. One is the length threshold: nginx'sgzip_min_lengthThe default is 20, and the length is only judged from theContent-Lengthheader judgment -If the response is chunked output, and there is no such header, this level of judgment is empty.. Smaller response compression actually consumes more CPU, so the threshold should be left, but you need to understand what it judges by.
The second is the type list:gzip_typesThe default only containstext/htmlScript, style, interface JSON and these need to be explicitly added to the list to be compressed. The typical phenomenon of list configuration errors is that the page is smaller, but the static resources have not changed. Compression levelgzip_comp_levelThe value range is from 1 to 9, the default is 1, the higher the level, the more time-consuming it is, and the benefits are not linear.
There is also one that is easy to miss:gzip_varyThe default is off, and it will be inserted after turning it onVary: Accept-Encoding. This header is very important when there is a cache layer in front, otherwise the shared cache may send the compressed version to clients that do not support it.
The naming and synchronization requirements of the pre-compressed files
The gzip_static module of nginx allows direct sending with.gzPre-compressed files with extensions instead of regular files. There are three orientations of the switch:onIt will check if the pre-compressed file exists,alwaysThen do not judge the client support situation and use compressed files all the time - the latter is only suitable for the scene where all visitors support it.
The synchronization requirement is the main risk at this step: it is recommended by the authorities that the modification date and time of the original file and the compressed file be consistent. If only the original file is updated without re-compression, the client receives an old content compressed version, and it is not intuitive to trace such problems. It is more reliable to bind 'Generate → Compress → Update Time' into one step in the release script than to make corrections after the fact.
How to coordinate the two layers
| Link | Who will do it | Suitable objects | Points to note |
|---|---|---|---|
| Dynamic compression | Web server | The page response is always different | The type list needs to be completed, the length judgment depends on Content-Length |
| Pre-compression | Release or build process | Static resources, generated page files | The original file and the modification time of .gz are consistent |
| Reusable page response | Caching and publishing layer | Pages like article pages and list pages that have the same content after two visits | The static rules and file paths should match |
| Transmission overhead | The entire site | Page size directly affects loading speed | In high concurrency, prioritize moving the repetitive compression cost out of the request path |
The basis for this layer of division is very straightforward: the page of the same article usually does not change between two visits, and it is a waste of CPU to compress it again each time. This type of reusable response is given to pre-compression or caching layer, and dynamic compression is left for responses that cannot be determined in advance, which is a stable configuration.
What else needs to be supplemented in the reverse proxy and caching layer
When a reverse proxy or CDN is in front, it needs to be confirmed that the compression results are correctly retained according to the client's ability, which is also mentioned earlierVaryHeader. Sites deployed with Docker images or Baota panel often have configuration changes scattered in three places: container environment, server block, and panel settings. After the change, you need to confirm layer by layer whether it takes effect, don't just look at one layer.
Common questions
Question: If compression has been turned on in the program, do we still need to turn it on at the server level?Answer: Look at who generates the response. It may compress the same content twice if both layers are turned on, and it is usually kept at one layer responsible for dynamic response and another layer responsible for static files.
Ask: Will increasing the compression level save bandwidth?Answer: The level and time consumption increase simultaneously, the benefits are not linear. The default level, combined with a reasonable type list, is usually more cost-effective than simply increasing the level.
Ask: How can you confirm that the client has really received the compressed version?Answer: Look at the encoding identifier and volume comparison of the response, and then send a request with an unsupported compression header again for comparison.