Skip to content

nginx gzip 配置

nginx gzip 官方文档上介绍

  1. https://nginx.org/en/docs/ 搜索 ngx_http_gzip_module 、ngx_http_gzip_static_module、ngx_http_gunzip_module
  2. https://nginx.org/en/docs/http/ngx_http_gzip_module.html
  3. https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html#gzip_static
  4. https://nginx.org/en/docs/http/ngx_http_gunzip_module.html

nginx -V 列出 nginx 配置信息

bash
docker exec -it  my-nginx-container nginx -V

查看查看是否包含以下模块

powershell
PS C:\Users\Administrator\Documents\vite-docs> docker exec -it  my-nginx-container nginx -V
nginx version: nginx/1.25.4
built by gcc 12.2.0 (Debian 12.2.0-14)
built with OpenSSL 3.0.9 30 May 2023 (running with OpenSSL 3.0.11 19 Sep 2023)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.25.4/debian/debuild-base/nginx-1.25.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

What's next?
  Try Docker Debug for seamless, persistent debugging tools in any container or image → docker debug my-nginx-container
  Learn more at https://docs.docker.com/go/debug-cli/
PS C:\Users\Administrator\Documents\vite-docs>

nginx gzip 官网上的介绍

英文

The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.

DANGER

When using the SSL/TLS protocol, compressed responses may be subject to BREACH attacks.

翻译后

ngx_http_gzip_module 模块是一个过滤器,它使用“gzip”方法压缩响应。这通常有助于将传输数据的大小减少一半甚至更多。

DANGER

当使用 SSL/TLS 协议时,压缩响应可能会受到 BREACH 攻击的威胁。

nginx gzip_static

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files.

This module is not built by default, it should be enabled with the --with-http_gzip_static_module configuration parameter.

这段话的中文翻译如下:

ngx_http_gzip_static_module 模块允许发送带有 “.gz” 文件扩展名的预压缩文件,而不是常规文件。

此模块默认不会构建,应通过使用 --with-http_gzip_static_module 配置参数来启用。
Details

Example Configuration

nginx
gzip_static  on;
gzip_proxied expired no-cache no-store private auth;
Syntax:	gzip_static on | off | always;
Default:
gzip_static off;
Context:	http, server, location
Enables (“on”) or disables (“off”) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary.

With the “always” value (1.3.6), gzipped file is used in all cases, without checking if the client supports it. It is useful if there are no uncompressed files on the disk anyway or the ngx_http_gunzip_module is used.

The files can be compressed using the gzip command, or any other compatible one. It is recommended that the modification date and time of original and compressed files be the same.

中文翻译:

示例配置:

nginx
gzip_static  on;
gzip_proxied expired no-cache no-store private auth;
语法:	gzip_static on | off | always;
默认值:
gzip_static off;
适用范围:	http, server, location
启用(“on”)或禁用(“off”)检查预压缩文件的存在。以下指令也会被考虑:gzip_http_version, gzip_proxied, gzip_disable, 以及 gzip_vary。

在“always”值(1.3.6 版本)下,无论客户端是否支持压缩,都会使用 gzipped 文件。如果磁盘上无论如何都没有未压缩的文件,或者使用了 ngx_http_gunzip_module,这种设置就非常有用。

文件可以使用 gzip 命令或任何其他兼容的命令来压缩。建议原始文件和压缩文件的修改日期和时间保持一致。

nginx gzip_vary 指令是默认启动的吗?如果 gzip, gzip_static, 或 gunzip 指令被激活

在 Nginx 中,gzip_vary 指令默认是关闭的。这意味着,除非你明确地在配置中启用 gzip_vary,否则即使 gzip, gzip_static, 或 gunzip 指令被激活,Vary: Accept-Encoding 响应头也不会自动添加到响应中。

如果你使用了 gzip, gzip_static, 或 gunzip,并希望确保缓存行为正确处理不同客户端对压缩内容的支持,你需要在配置文件中明确启用 gzip_vary。启用后,Nginx 会在适当的响应中添加 Vary: Accept-Encoding 响应头,以指示该响应依赖于请求头中的 Accept-Encoding 值。

启用示例

如果你想启用这个功能,你可以在 Nginx 的配置文件中添加如下行:

nginx
gzip_vary on;

这行代码通常放在与其他 gzip 相关指令相同的上下文中,例如 httpserver 块内。这样配置确保了当启用内容压缩时,正确地处理客户端和代理的缓存逻辑。

nginx gunzip on; 是什么意思

https://nginx.org/en/docs/http/ngx_http_gunzip_module.html

在 Nginx 中使用 gunzip on; 指令意味着启用了 ngx_http_gunzip_module 模块的功能。这个模块允许 Nginx 服务器在发送给客户端之前动态地解压缩已经被 gzip 压缩的内容。这是对于那些不支持 gzip 解压缩的客户端非常有用的功能。

主要用途和功能

  1. 服务预压缩的内容给不支持 gzip 的客户端:如果服务器上的资源已经是预压缩的格式(通常是 .gz 文件),而客户端请求时却不支持 gzip 解压缩,开启 gunzip on; 将使 Nginx 动态地解压这些内容并以未压缩的形式发送给客户端。

  2. 兼容性提升:这可以提高网站对各种设备和网络环境的兼容性,尤其是一些老旧的浏览器或特定的客户端设备可能不支持压缩传输。

配置示例

nginx
http {
    gunzip on;
    ...
}

在这个配置中,gunzip on; 会使得 Nginx 检查客户端的请求头。如果请求头中表明客户端不支持 gzip 压缩,Nginx 将解压.gz 文件,并将未压缩的内容发送给客户端。

注意事项

  • 模块启用gunzip on; 指令需要 ngx_http_gunzip_module 模块支持,这个模块不是默认编译进 Nginx 的,可能需要在编译 Nginx 时通过添加 --with-http_gunzip_module 配置参数来启用。
  • 与 gzip_static 的关联:当使用 gzip_static on; 同时,gunzip on; 可以为不支持 gzip 的客户端提供服务,而 gzip_static on; 为支持 gzip 的客户端提供预压缩的文件。这种配置确保了对所有客户端的优化支持。

当启用了 gzip_static on;gzip on; 如果这两个都不支持的时候,不是会默认返回源文件吗?为啥还需要 gunzip on;

你提出了一个很好的问题。确实,在大多数情况下,当你启用了 gzip_static on;gzip on; 时,Nginx 会根据客户端是否支持 gzip 压缩来决定如何服务文件:

  • 如果客户端支持 gzip 压缩gzip_static on; 允许 Nginx 直接发送预压缩的 .gz 文件(如果存在),而 gzip on; 指令则会在运行时压缩原始文件。
  • 如果客户端不支持 gzip 压缩:通常情况下,Nginx 就会返回原始的未压缩文件。

然而,gunzip on; 的用途在于处理一些特殊的配置或需求场景。以下是 gunzip on; 可能需要使用的几种情况:

  1. 没有未压缩的源文件存储:在某些高度优化的部署中,可能只存储了 .gz 文件以节省空间,而没有存储未压缩的原始文件。在这种情况下,如果一个不支持 gzip 的客户端发出请求,没有 gunzip on; 的话,服务器无法提供服务,因为它没有未压缩的内容可供发送。

  2. gzip_static always; 的使用:如果配置了 gzip_static always;,Nginx 将忽略客户端是否支持压缩,始终发送 .gz 文件。这时,gunzip on; 就显得非常重要,它能确保即使在 gzip_static always; 模式下,也能为不支持 gzip 的客户端提供内容。

  3. 确保兼容性:虽然大多数现代浏览器都支持 gzip,但在一些特殊环境下(如某些 API 客户端、老旧设备或特定的网络环境),客户端可能不支持或不正确处理 gzip 压缩。gunzip on; 确保这些客户端能够接收和处理数据。

因此,gunzip on; 提供了一个额外的安全网,确保在所有配置和客户端支持的情况下,都能正确地服务内容。这是一个关于兼容性和健壮性的考量,特别是在你只想维护一套压缩过的文件时尤其有用。

如何理解“当使用 SSL/TLS 协议时,压缩响应可能会受到 BREACH 攻击的威胁。”

在解释这句话之前,需要了解 BREACH 攻击是什么。

BREACH(Browser Reconnaissance and Exfiltration via Adaptive Compression of Hypertext)是一种针对通过 SSL/TLS 加密的 HTTP 压缩数据的安全漏洞。这种攻击利用了 HTTP 压缩功能,这个功能可以显著减少通过网络传输的数据量。

具体来说,当网站开启了 HTTP 响应的压缩功能(如使用 gzip 压缩),并且这些响应是通过 SSL/TLS 协议加密传输的时,BREACH 攻击就有可能发生。攻击者可以观察从服务器返回给用户的压缩加密数据的大小变化,通过这种方法推断出关键信息,例如 CSRF (Cross-Site Request Forgery) 令牌、会话 cookie 等。

这种攻击的实施条件包括:

  1. 响应压缩:攻击者利用响应压缩中的信息泄露特性。
  2. 反射用户输入:攻击者发送的数据必须在响应中某种形式地“反射”出来,以便通过观察压缩数据的变化来猜测数据。
  3. 已知的秘密:攻击者必须知道一部分响应数据(如页面结构),以便构建利用压缩算法的特性的攻击。

因此,“当使用 SSL/TLS 协议时,压缩响应可能会受到 BREACH 攻击的威胁”这句话的意思是,虽然 SSL/TLS 协议本身提供了数据的安全传输,但在数据传输过程中开启压缩可以导致通过特定攻击手段泄露敏感信息,从而破坏通信的安全性。这提醒网站管理员在配置服务器时需权衡性能优化和安全风险。

如何避免 BREACH 攻击

要避免 BREACH 攻击,你可以采取以下几种措施:

  1. 禁用 HTTP 压缩: 禁用 HTTP 压缩是最直接的方法,可以完全消除 BREACH 攻击的可能性。虽然这会增加传输的数据量,可能会影响网站的加载速度,但它是保证安全的有效手段。

  2. 分离敏感信息和其他数据: 避免在相同的响应中传输敏感信息(如令牌、密码)和其他可被攻击者控制的数据。例如,可以将敏感信息通过不同的请求或不同的通道传输。

  3. 使用 HTTPS 令牌绑定技术: 这种技术可以将令牌绑定到特定的 TLS 会话,即使攻击者可以获取令牌,也无法在其他会话中使用。

  4. 使用随机填充: 在敏感数据前后添加一些随机数据,这样即使在启用压缩的情况下,每次响应的大小也会因为随机填充而不同,从而使攻击者难以通过观察响应长度来推断出数据。

  5. 限制攻击者的输入反射: 确保用户的输入不会未经更改地直接反射在响应中。对用户输入进行充分的清理和编码,防止恶意脚本的注入。

  6. 使用较短的会话时间和早期失效策略: 通过缩短会话令牌的有效期,即使攻击者获得了令牌,也只能在非常短的时间窗口内使用。

通过上述措施,可以有效地降低或避免通过 BREACH 攻击泄露敏感信息的风险。每种方法都有其利弊,选择适合自己网站安全需求和性能需求的策略至关重要。

如果我对一些静态资源使用 gzip_static on;是不是就不会存在 BREACH

使用 gzip_static on; 指令在 Nginx 配置中是为了直接发送事先压缩的静态资源。这意味着,服务器上存储的是已经压缩的文件版本,例如 .gz 文件。当请求这些资源时,如果客户端支持 gzip 编码,服务器将直接发送这些预压缩的文件,而不是在每次请求时动态地压缩内容。

关于 BREACH 攻击的考虑:

  1. 静态内容与动态内容:BREACH 攻击主要针对动态生成的内容,特别是当响应中包含由用户输入部分影响的数据时。因为这类攻击利用了反射的用户输入与压缩算法的特点来推断敏感数据。静态资源通常不包含敏感信息,也不反射用户输入。

  2. 预压缩的静态资源gzip_static on; 配置项意味着使用预压缩的静态资源,这些资源通常是不变的。由于这些资源不依赖于会话特定数据或动态内容,它们的内容在每次请求时保持不变。因此,使用预压缩的静态资源减少了通过压缩算法暴露敏感信息的风险。

  3. 安全性提高:在处理静态内容时,通过提前压缩文件并直接服务这些文件,可以减少服务器处理请求的复杂性和时间,同时也避免了每次请求都压缩相同数据时可能带来的安全风险。

因此,使用 gzip_static on; 来处理静态资源实际上是一个减少 BREACH 攻击风险的做法,尤其是因为它不涉及动态内容的处理。当然,这并不意味着你可以完全忽视 HTTPS 配置中的其他安全实践,如确保使用安全的 TLS 配置、更新和安全的软件版本等,这些都是保护网站免受各种攻击的关键部分。