# IIS/Nginx 的 HTTP/2、压缩与缓存

> 在 TLS 上启用 HTTP/2，正确压缩文本，设置缓存头，并避免双重压缩或缓存敏感页面。

**测试环境**

- 版本号: 26100.33650
- 版本: Standard / Datacenter
- 架构: x64
- 证据: E1
- 核验日期: 2026-10-29

## 一句话结论

- **HTTP/2** 浏览器要求 TLS；现代 Windows 上 IIS 用 HTTPS 默认协商。
- **压缩**只压文本（HTML/CSS/JS/JSON/SVG），别压图片/视频/zip。
- **缓存**是按路径的策略；不要缓存认证后的 HTML。

## HTTP/2

IIS：HTTPS 绑定是主要条件；HTTP/2 通过 ALPN 协商。Nginx：

```nginx
listen 443 ssl;
http2 on;
```

验证：

```powershell
curl.exe -I --http2 https://lab.example/
```

状态行应显示 `HTTP/2`。还是 `HTTP/1.1`，检查 TLS/ALPN 和中间代理是否降级。

HTTP/2 不是 HTTP/3；QUIC 见 HTTP/3 页。

## 压缩

IIS `web.config`：

```xml
<system.webServer>
  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="application/json" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="image/svg+xml" enabled="true" />
      <add mimeType="*/*" enabled="false" />
    </staticTypes>
  </httpCompression>
</system.webServer>
```

Nginx：

```nginx
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;
gzip_vary on;
```

不要压已压缩文件；不要在代理和源站双重压缩。

## 缓存

```xml
<location path="assets">
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
  </system.webServer>
</location>
```

带 hash 的资源可以长 `max-age` + `immutable`。HTML 用 `no-cache` 或短 TTL，部署才可见。

> **警告: 不要把认证 HTML 公开缓存**
>
> 共享缓存不能存另一个用户的数据。账户页标 `Cache-Control: private, no-store`；通过 CDN/代理测，不要只测源站。

## 验证

```powershell
curl.exe -I --compressed https://lab.example/assets/app.js
curl.exe -I https://lab.example/account
```

查 `content-encoding`、`cache-control`、`vary`，以及账户页不可公开缓存。

## 实验室边界

E1。实验室 IIS/Nginx 只跑 HTTP，没有验证 HTTP/2、压缩或 CDN 缓存。配置是 Server 2025 上待验证方案。

## 来源

- [Microsoft Learn — IIS 上的 HTTP/2](https://learn.microsoft.com/zh-cn/iis/get-started/whats-new-in-iis-10/http2-on-iis)
- [Microsoft Learn — IIS 压缩](https://learn.microsoft.com/zh-cn/iis/configuration/system.webserver/httpcompression/)
- [nginx — gzip](https://nginx.org/en/docs/http/ngx_http_gzip_module.html)

---

来源页面: https://winbook.fyi/zh/server/tls/http2/
