- config: 反向代理、缓存、负载均衡、安全、SSL 等配置模板 - lua: API 网关、认证、动态路由、限流、WebSocket 等脚本示例 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
85 lines
2.4 KiB
Plaintext
85 lines
2.4 KiB
Plaintext
# ============================================================
|
||
# Nginx Brotli 压缩配置示例
|
||
# ============================================================
|
||
#
|
||
# 功能说明:
|
||
# - Brotli 压缩算法(比 Gzip 更高效)
|
||
# - 更高压缩率,更快解压
|
||
# - 需要安装 ngx_brotli 模块
|
||
#
|
||
# Lolly 对应配置:
|
||
# server:
|
||
# compression:
|
||
# type: "brotli" # 或 "both" 同时启用 gzip 和 brotli
|
||
# level: 6
|
||
# min_size: 1024
|
||
# gzip_static: true
|
||
# gzip_static_extensions: [".br", ".gz"]
|
||
# ============================================================
|
||
|
||
# 需要安装 ngx_brotli 模块
|
||
# https://github.com/google/ngx_brotli
|
||
|
||
http {
|
||
# Brotli 基础配置
|
||
brotli on;
|
||
brotli_comp_level 6; # 压缩级别(0-11)
|
||
brotli_min_length 256; # 最小压缩大小
|
||
brotli_buffers 16 8k; # 缓冲区
|
||
brotli_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/xml+rss image/svg+xml;
|
||
|
||
# 预压缩文件支持
|
||
# 自动查找 .br 文件
|
||
brotli_static on;
|
||
|
||
server {
|
||
listen 443 ssl http2;
|
||
server_name brotli.example.com;
|
||
|
||
ssl_certificate /etc/nginx/ssl/server.crt;
|
||
ssl_certificate_key /etc/nginx/ssl/server.key;
|
||
|
||
# Brotli 压缩配置
|
||
# Lolly 对应: compression.type: "brotli"
|
||
|
||
location / {
|
||
brotli on;
|
||
brotli_comp_level 6;
|
||
root /var/www/html;
|
||
}
|
||
|
||
# 静态资源(预压缩优先)
|
||
location /assets {
|
||
brotli_static on;
|
||
gzip_static on; # 同时支持 gzip 预压缩
|
||
|
||
root /var/www/assets;
|
||
}
|
||
}
|
||
}
|
||
|
||
# Brotli vs Gzip:
|
||
#
|
||
# 1. 压缩效率:
|
||
# - Brotli: 通常比 Gzip 高 20-30% 压缩率
|
||
# - 相同质量下体积更小
|
||
# - 更适合静态资源
|
||
#
|
||
# 2. 性能:
|
||
# - 压缩速度: Gzip 更快(低级别)
|
||
# - 解压速度: Brotli 更快
|
||
# - 推荐: 预压缩文件使用 Brotli
|
||
#
|
||
# 3. 浏览器支持:
|
||
# - Brotli: 所有现代浏览器支持(2016+)
|
||
# - 通过 Accept-Encoding: br 协商
|
||
# - IE11 不支持 Brotli
|
||
#
|
||
# 4. 同时启用 gzip 和 brotli:
|
||
# nginx 根据客户端支持自动选择
|
||
# Lolly 对应: compression.type: "both"
|
||
#
|
||
# 5. 预压缩生成:
|
||
# brotli --best file.js # 最高压缩率
|
||
# brotli -q 6 file.js # 平衡压缩率
|
||
# gzip -9 -k file.js # gzip 预压缩 |