Should compression be done in the program or in the Web server, and should static files be pre-compressed?

📅 October 9, 2026 👁️ 11

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.

Related articles

Static resources added version number still hit the old cache, how should the cache header be written?

Adding version number still hits old files, usually because the response header and filename strategy are not matched: max-age is calculated from the moment the response is generated on the source site instead of the moment it is received, without must-revalidate after expiration, it may continue to reuse outdated copies; s-maxage only manages shared caching and overrides max-age; immutable should be used with version numbers to avoid unnecessary conditional requests. The cache duration for page content and static resources should be configured separately.

2026-10-09

Should the backup take the uploaded images and static files together?

A backup of only the database will leave a very specific gap: after the restore, the article list is still there, but the images, attachments, and static resources in the main text all point to invalid addresses. The scope of a complete backup should cover both data and static files, and regular recovery drills should be conducted. The trash can only cover deleted documents and cannot replace backups; the location relationship of the backup files themselves should also be recorded with the site deployment directory and upload directory.

2026-10-09

When automatic updates are enabled, what is the difference between the default behavior of major and minor versions?

When automatic updates are enabled, there is a big difference in the default orientation between major and minor versions: An official operation manual of a mainstream program states that most sites default to enabling automatic updates, covering four categories: core, plugins, themes, and translation files. Since version 5.6, new installations have default automatic updates for both minor and major versions. For enterprise sites, a more stable compromise is to automatically update minor versions, manually evaluate major versions, and confirm the backup range before the upgrade to include both data and static files.

2026-10-09

How should the filing number be handled when changing servers or access providers?

How to handle the filing number when changing the server of the website, the judgment standard is whether the access provider has changed: When changing the access provider, the access filing should be handled at the new access provider, and the change of materials for the same subject should go through the change filing and require bureau review. This article clarifies two actions, the impact on access during the review period, and the most easily overlooked one when multiple sites share a domain name.

2026-10-09

Certificates are valid for ninety days and automatically renewed, where should this be configured?

The default validity period of the HTTPS certificate is ninety days, and renewal should not wait until the browser alarms. Automatic renewal should be as a fixed scheduled task in the deployment process, after the certificate is issued and before the service is reloaded, and it is recommended to trigger once every sixty days. To confirm that it has been renewed, you need to check the actual expiration date of the online certificate, and also check the old address redirection and mixed content, and keep a backup before changing the certificate.

2026-10-10

Which parts need to be changed when replacing HTTP with HTTPS, including the internal addresses and redirections?

Changing from HTTP to HTTPS is not just about installing certificates. It also involves: the 301 redirect rules for old addresses, hardcoded absolute addresses within the site, the protocol in the sitemap and robots, pseudo-static and standard address configuration, as well as hard-coded links in navigation and single-page applications. The recommended sequence is to first handle certificates and redirects, then perform bulk replacements, and finally update the checklist files and review mixed content.

2026-10-10

After replacing the certificate, the page style is missing half, how to clear the http external links in the https page?

After replacing the certificate, the style is missing half, usually due to the residual http resources in the https page being blocked by the browser. The cleaning order is to first locate the residual references, then batch replace them with internal relative addresses or encrypted addresses according to the main text data, template, and configuration, and finally close the jump rules together.

2026-10-10

Rejected upload returns 413, modify program limit first or modify gateway limit first

When the upload is rejected and returns 413, modify the limit of the gateway layer first, because this status code is returned by the front-end service and the request has not entered the program yet. The sequence is to confirm at which layer the request stops, release the request body limit of the entry layer, and then check the program's own limit. When importing large files in batches, handle them in the same order.

2026-10-10