阅读视图

发现新文章,点击刷新页面。

Nginx 模块机制深度解析:从核心原理到生产实践

Nginx 的强大之处不仅在于其高性能的事件驱动架构,更在于其高度模块化的设计。本文将深入解析 Nginx 模块机制,并盘点生产环境中最值得使用的第三方模块。


一、Nginx 模块架构概览

Nginx 采用模块化架构,几乎所有功能都由模块实现。这种设计的核心优势是按需加载、灵活扩展——你只启用需要的功能,避免不必要的内存开销。

┌─────────────────────────────────────────┐
│           Nginx Core (核心)              │
│    (事件循环、内存池、配置解析等)        │
├─────────────────────────────────────────┤
│  Core Modules  │  HTTP Modules          │
│  (事件、日志) │  (处理 HTTP 请求)      │
├─────────────────────────────────────────┤
│  Mail Modules  │  Stream Modules        │
│  (邮件代理)   │  (TCP/UDP 代理)        │
├─────────────────────────────────────────┤
│  Third-Party Modules(第三方模块)       │
│  (Lua、Brotli、WAF 等)                │
└─────────────────────────────────────────┘

二、模块类型详解

1. 核心模块(Core Modules)

内置在 Nginx 源码中,编译时自动包含,提供基础功能:

模块 功能
ngx_core_module 全局配置、进程管理
ngx_events_module 事件驱动模型(epoll/kqueue)
ngx_http_module HTTP 协议处理入口
ngx_stream_module TCP/UDP 四层代理

2. 官方可选模块(Official Modules)

Nginx 源码自带,但需显式启用:

模块 编译参数 功能
SSL --with-http_ssl_module HTTPS 支持
Gzip --with-http_gzip_static_module Gzip 压缩
Real IP --with-http_realip_module 获取真实客户端 IP
Stub Status --with-http_stub_status_module 状态监控页面
HTTP/2 --with-http_v2_module HTTP/2 协议支持
Image Filter --with-http_image_filter_module 图片处理

3. 第三方模块(Third-Party Modules)

社区开发的扩展模块,功能极其丰富。这是 Nginx 生态的核心竞争力。


三、静态模块 vs 动态模块

Nginx 支持两种模块加载方式,这是理解 Nginx 模块机制的关键。

静态模块(Static Modules)

编译时直接链接到 Nginx 二进制文件中。

# 静态编译模块
./configure \
    --with-http_ssl_module \
    --add-module=/path/to/third-party-module
make && make install

特点

  • 模块代码嵌入 nginx 二进制,启动即生效
  • 无需额外配置加载
  • 缺点:更新模块必须重新编译整个 Nginx

动态模块(Dynamic Modules)

编译为独立的 .so 文件,运行时按需加载。

# 动态编译模块
./configure \
    --with-compat \
    --add-dynamic-module=/path/to/third-party-module
make && make install

# 生成的 .so 文件在 objs/ 目录
# 复制到 nginx 模块目录
cp objs/ngx_http_xxx_module.so /usr/lib/nginx/modules/

配置加载

# nginx.conf 顶部加载
load_module modules/ngx_http_xxx_module.so;

特点

  • 独立编译、独立更新,无需重新编译 Nginx 核心
  • 按需加载,减少内存占用
  • 推荐方式:生产环境优先使用动态模块

性能差异:动态模块与静态模块在运行时性能几乎没有区别,区别仅在于加载机制 。


四、Nginx 请求处理阶段(模块介入点)

理解模块机制必须了解 Nginx 的请求处理流水线:

┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│  NGX_HTTP_POST_READ_PHASE      │ ← 读取请求头后
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_SERVER_REWRITE_PHASE │ ← server 块 rewrite
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_FIND_CONFIG_PHASE    │ ← 匹配 location
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_REWRITE_PHASE        │ ← location rewrite
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_PREACCESS_PHASE      │ ← 访问控制前(限流)
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_ACCESS_PHASE         │ ← 访问控制(认证)
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_CONTENT_PHASE        │ ← 生成响应内容
├─────────────┤    ├─────────────┤    ├─────────────┤
│  NGX_HTTP_LOG_PHASE            │ ← 记录日志
└─────────────┘    └─────────────┘    └─────────────┘

模块类型对应介入阶段

模块类型 介入阶段 典型模块
Handler 模块 CONTENT 生成响应内容
Filter 模块 CONTENT 之后 修改响应(Gzip、Header)
Upstream 模块 CONTENT 代理到后端
Load Balancer CONTENT 负载均衡算法
Access 模块 ACCESS IP 限制、认证

五、生产环境常用第三方模块详解

1. ngx_lua / OpenResty(最重要)

功能:在 Nginx 中嵌入 Lua 脚本,实现复杂业务逻辑。

# 加载模块
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;

location /api {
    access_by_lua_block {
        -- JWT 验证
        local jwt = require "resty.jwt"
        local token = ngx.var.http_authorization
        -- ...验证逻辑
    }
    
    content_by_lua_block {
        -- 直接查询 Redis
        local redis = require "resty.redis"
        local red = redis:new()
        red:connect("127.0.0.1", 6379)
        local res = red:get("user:" .. ngx.var.arg_id)
        ngx.say(res)
    }
}

解决的问题

  • 无需后端服务,直接在 Nginx 层处理认证、限流、缓存
  • 极高性能(LuaJIT 编译执行)
  • 构建 API 网关、WAF、灰度发布等复杂场景

典型应用:API 网关、限流熔断、AB 测试、边缘计算


2. ngx_brotli(Brotli 压缩)

功能:支持 Google 的 Brotli 压缩算法,比 Gzip 压缩率高 15-25%。

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

http {
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/javascript application/json;
    
    # 预压缩静态文件
    brotli_static on;
}

解决的问题:减少传输体积,提升页面加载速度,尤其利好移动端用户。


3. headers-more-nginx-module(Header 控制增强)

功能:更灵活地添加、修改、删除请求头和响应头。

load_module modules/ngx_http_headers_more_filter_module.so;

server {
    # 删除默认的 Server 头
    more_clear_headers Server;
    
    # 条件性添加头
    more_set_headers "X-Frame-Options: SAMEORIGIN" "always";
    more_set_headers "X-Request-ID: $request_id" "always";
    
    # 修改请求头(发送给后端前)
    more_set_input_headers "X-Real-IP: $remote_addr";
}

解决的问题:原生 add_header 有诸多限制(如不在错误响应中添加),此模块完全突破。


4. ngx_cache_purge(缓存清理)

功能:提供接口主动清理 Nginx 代理缓存。

load_module modules/ngx_http_cache_purge_module.so;

location ~ /purge(/.*) {
    # 只允许特定 IP 访问
    allow 192.168.1.0/24;
    deny all;
    
    # 清理匹配 URI 的缓存
    proxy_cache_purge cache_zone $1$is_args$args;
}

解决的问题:缓存更新后需要手动清理,避免用户看到旧内容。


5. ModSecurity-nginx(WAF 防火墙)

功能:集成 ModSecurity Web 应用防火墙。

load_module modules/ngx_http_modsecurity_module.so;

server {
    modsecurity on;
    modsecurity_rules_file /etc/nginx/modsecurity/main.conf;
}

解决的问题:防御 SQL 注入、XSS、命令执行等 Web 攻击。

⚠️ 注意:原 nginx-naxsi 包已废弃,不再接收安全更新 ,建议使用 ModSecurity。


6. ngx_http_geoip2_module(GeoIP2 地理位置)

功能:基于 MaxMind GeoIP2 数据库获取用户地理位置。

load_module modules/ngx_http_geoip2_module.so;

http {
    geoip2 /var/lib/GeoIP/GeoLite2-Country.mmdb {
        $geoip2_country_code country iso_code;
        $geoip2_country_name country names en;
    }
    
    # 按国家分流
    map $geoip2_country_code $backend {
        CN      cn_backend;
        default global_backend;
    }
}

解决的问题:按地理位置路由、限制特定国家访问、内容本地化。


7. nginx-module-vts(流量监控)

功能:提供详细的 Nginx 流量统计页面,类似 stub_status 的增强版。

load_module modules/ngx_http_vhost_traffic_status_module.so;

http {
    vhost_traffic_status_zone;
    
    server {
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

解决的问题:可视化监控各虚拟主机、URI 的流量、响应时间、状态码分布。


8. nginx-upload-progress-module(上传进度)

功能:跟踪大文件上传进度。

location /upload {
    upload_progress uploads 1m;  # 1MB 存储空间跟踪进度
    
    track_uploads uploads 30s;   # 跟踪 30 秒
}

location /progress {
    report_uploads uploads;      # 返回 JSON 格式进度
}

解决的问题:用户上传大文件时实时显示进度条。


9. nginx-rtmp-module(流媒体)

功能:支持 RTMP 协议,构建直播推流/播放服务。

load_module modules/ngx_rtmp_module.so;

rtmp {
    server {
        listen 1935;
        
        application live {
            live on;
            
            # 推流认证
            on_publish http://localhost/auth;
            
            # HLS 切片
            hls on;
            hls_path /var/hls;
            hls_fragment 3s;
        }
    }
}

解决的问题:低成本搭建直播推流服务器,支持 RTMP/HLS/DASH 协议。


10. set-misc-nginx-module(变量增强)

功能:提供丰富的变量操作函数。

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_set_misc_module.so;

location / {
    # 生成随机数
    set_random $rand 100 999;
    
    # 计算 MD5
    set_md5 $hash $uri;
    
    # Base64 编码
    set_encode_base64 $encoded $string;
    
    # 条件判断
    if ($http_user_agent ~* "Mobile") {
        set $is_mobile 1;
    }
}

解决的问题:原生 Nginx 变量操作能力弱,此模块极大增强配置灵活性。


六、常用模块组合推荐

场景一:高性能 Web 服务

# 必装模块
ngx_brotli                    # Brotli 压缩
headers-more-nginx-module     # 灵活 Header 控制
ngx_cache_purge               # 缓存清理

场景二:API 网关

# 必装模块
ngx_lua (OpenResty)           # Lua 脚本处理业务逻辑
ngx_http_geoip2_module        # 地理位置路由
headers-more-nginx-module     # Header 操作

场景三:安全防护

# 必装模块
ModSecurity-nginx             # WAF 防火墙
ngx_lua                       # 自定义安全规则
headers-more-nginx-module     # 安全响应头

场景四:流媒体服务

# 必装模块
nginx-rtmp-module             # RTMP 直播
nginx-upload-progress-module  # 上传进度

七、模块编译安装完整示例

以 Ubuntu 系统编译 ngx_brotli 为例:

# 1. 安装依赖
sudo apt update
sudo apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev

# 2. 下载 Nginx 源码
NGINX_VERSION=1.26.0
wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xzf nginx-${NGINX_VERSION}.tar.gz

# 3. 下载第三方模块
git clone https://github.com/google/ngx_brotli.git

# 4. 配置编译(动态模块)
cd nginx-${NGINX_VERSION}
./configure \
    --with-compat \
    --add-dynamic-module=../ngx_brotli

# 5. 编译(只编译模块,不安装 Nginx)
make modules

# 6. 安装模块
sudo cp objs/ngx_http_brotli_*.so /usr/lib/nginx/modules/

# 7. 加载模块
echo "load_module modules/ngx_http_brotli_filter_module.so;" | sudo tee /etc/nginx/modules-enabled/50-mod-brotli.conf
echo "load_module modules/ngx_http_brotli_static_module.so;" | sudo tee -a /etc/nginx/modules-enabled/50-mod-brotli.conf

# 8. 配置使用
sudo nginx -t && sudo nginx -s reload

八、模块管理最佳实践

建议 说明
优先使用动态模块 便于独立更新,不影响 Nginx 核心
只加载需要的模块 减少内存占用和攻击面
版本锁定 生产环境固定模块版本,避免兼容性风险
测试环境先行 新模块先在测试环境验证
关注维护状态 避免使用已废弃的模块(如 Naxsi)

九、总结

Nginx 的模块化架构是其成为顶级 Web 服务器/反向代理的核心原因:

维度 核心要点
架构设计 事件驱动 + 模块化 = 高性能 + 可扩展
加载方式 动态模块优于静态模块,便于维护
核心模块 Lua、Brotli、Headers-More 是生产必备
安全模块 ModSecurity 替代已废弃的 Naxsi
监控模块 VTS 提供比 stub_status 更丰富的指标

💡 一句话:Nginx 的强大 = 核心性能 + 模块生态。掌握模块机制,才能真正发挥 Nginx 的全部潜力。

Nginx 生产环境配置完全指南:从安全加固到性能调优

本文系统梳理 Nginx 在生产环境中常用的配置项,每个配置都附带解决的问题实际应用场景,帮助你构建健壮、安全、高性能的 Web 服务。


一、基础安全配置

1. 隐藏 Nginx 版本信息

http {
    server_tokens off;
}

解决的问题:防止攻击者通过版本号查找已知漏洞(如 nginx/1.18.0 暴露后可直接搜索该版本的 CVE)。

验证

curl -I http://your-site.com
# 没有 server_tokens 时:Server: nginx/1.18.0
# 开启后:Server: nginx

2. 限制请求方法与大小

server {
    # 只允许常见 HTTP 方法
    if ($request_method !~ ^(GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH)$) {
        return 405;
    }
    
    # 限制请求体大小(防止恶意上传超大文件耗尽内存)
    client_max_body_size 10M;
    client_body_buffer_size 128k;
    
    # 限制请求头大小(防止 Slowloris 攻击)
    large_client_header_buffers 4 16k;
}
配置 解决的问题
client_max_body_size 防止恶意上传超大文件导致内存耗尽
client_body_buffer_size 控制请求体缓冲区,超出部分写入临时文件
large_client_header_buffers 防止通过超长请求头进行的 DoS 攻击

3. 禁用不安全的 HTTP 方法

location / {
    # 显式拒绝 TRACE 和 TRACK 方法(可能用于 XST 攻击)
    if ($request_method ~ ^(TRACE|TRACK)$) {
        return 405;
    }
}

二、HTTPS / SSL 安全配置

4. 强制 HTTPS 跳转

server {
    listen 80;
    server_name example.com;
    # 301 永久重定向到 HTTPS
    return 301 https://$host$request_uri;
}

解决的问题:确保所有流量走加密通道,防止中间人窃听。


5. 现代 TLS 配置

server {
    listen 443 ssl http2;
    
    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;
    
    # 只启用安全的 TLS 版本
    ssl_protocols TLSv1.2 TLSv1.3;
    
    # 强加密套件,优先使用 ECDHE 实现前向保密
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    
    ssl_prefer_server_ciphers on;
    
    # 会话复用,减少 TLS 握手开销
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    
    # OCSP Stapling,加速证书验证
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 1.1.1.1 8.8.8.8 valid=300s;
}
配置 解决的问题
ssl_protocols TLSv1.2 TLSv1.3 禁用存在漏洞的 SSLv2/3 和 TLSv1.0/1.1
ssl_ciphers 只使用强加密算法,防止降级攻击
ssl_session_cache 复用 TLS 会话,减少握手延迟
ssl_stapling 服务器主动提供证书吊销状态,避免客户端额外查询

6. HSTS(HTTP Strict Transport Security)

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

解决的问题:告诉浏览器永远用 HTTPS 访问该域名(包括子域名),防止 SSL 剥离攻击。

⚠️ 注意:确保 HTTPS 完全配置好再开启,否则会导致网站无法通过 HTTP 访问。


三、安全响应头(Security Headers)

7. 完整的 Security Headers 配置

# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN" always;

# 防止 MIME 类型嗅探(如把 .jpg 当脚本执行)
add_header X-Content-Type-Options "nosniff" always;

# 启用 XSS 过滤器(现代浏览器已废弃,但仍建议保留兼容)
add_header X-XSS-Protection "1; mode=block" always;

# 控制 Referer 信息泄露
add_header Referrer-Policy "no-referrer-when-downgrade" always;

# 内容安全策略(CSP),限制资源加载来源
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; frame-ancestors 'self';" always;

# 限制浏览器功能权限
add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=()" always;
响应头 解决的问题
X-Frame-Options 点击劫持(Clickjacking)
X-Content-Type-Options MIME 嗅探攻击
X-XSS-Protection 反射型 XSS(浏览器层面)
Referrer-Policy Referer 信息泄露(如从含 token 的 URL 跳转)
Content-Security-Policy XSS、数据注入、恶意资源加载
Permissions-Policy 限制不必要的浏览器 API 权限

8. CSP frame-ancestors 替代 X-Frame-Options

# 更灵活的方案:允许指定域名嵌入
add_header Content-Security-Policy "frame-ancestors 'self' https://partner.example.com;" always;

解决的问题X-Frame-Options 只能设 DENYSAMEORIGIN,无法指定具体域名。frame-ancestors 可以精确控制哪些网站可以嵌入你的页面。


四、防盗链与访问控制

9. 图片/资源防盗链

location ~* \.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    # 允许空 Referer 和本域名
    valid_referers none blocked server_names *.example.com;
    
    if ($invalid_referer) {
        # 返回 403 或重定向到警告图
        return 403;
        # 或:rewrite ^/ https://example.com/forbidden.png break;
    }
}

解决的问题:防止其他网站直接引用你的图片/视频资源,消耗你的带宽和流量。


10. IP 黑名单/白名单

# 黑名单:拒绝特定 IP
location /admin {
    deny 192.168.1.100;
    deny 10.0.0.0/24;
    allow all;
}

# 白名单:只允许特定 IP 访问
location /api/internal {
    allow 192.168.1.0/24;
    allow 10.0.0.5;
    deny all;
}

11. 目录遍历防护

location ~ /\. {
    # 拒绝访问隐藏文件(.git, .env, .htaccess 等)
    deny all;
    access_log off;
    log_not_found off;
}

location ~* \.(git|svn|htaccess|env)$ {
    deny all;
}

解决的问题:防止攻击者访问 .git 目录、.env 文件等敏感文件泄露源代码或密钥。


五、限流与防攻击

12. 请求速率限制(Rate Limiting)

http {
    # 定义限流区域:按 IP 限制,10MB 内存存储状态,每秒 10 个请求
    limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
    
    # 针对登录接口更严格
    limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
    
    # 按 IP 限制并发连接数
    limit_conn_zone $binary_remote_addr zone=addr:10m;
}

server {
    location / {
        # 突发 20 个请求,不延迟处理(超出直接 503)
        limit_req zone=general burst=20 nodelay;
        
        # 限制单个 IP 最多 10 个并发连接
        limit_conn addr 10;
    }
    
    location /api/login {
        # 登录接口更严格:每秒 1 次
        limit_req zone=login burst=5 nodelay;
    }
}
配置 解决的问题
limit_req 防止接口被暴力刷、爬虫过度抓取
limit_conn 防止单个 IP 占用过多连接资源
burst 允许短时间突发请求,平滑处理
nodelay 不排队,超出直接拒绝(降低延迟)

13. 防 Slowloris / Slow POST 攻击

http {
    # 读取客户端请求头的超时时间
    client_header_timeout 10s;
    
    # 读取客户端请求体的超时时间
    client_body_timeout 10s;
    
    # 发送响应的超时时间
    send_timeout 10s;
}

解决的问题:Slowloris 攻击通过缓慢发送请求头/体占用连接,导致正常用户无法连接。


六、性能优化配置

14. Gzip / Brotli 压缩

http {
    # Gzip 压缩
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types
        text/plain
        text/css
        text/javascript
        application/javascript
        application/json
        application/xml
        image/svg+xml;
    
    # Brotli 压缩(需要编译 ngx_brotli 模块,压缩率比 gzip 高 15-25%)
    # brotli on;
    # brotli_comp_level 5;
    # brotli_types text/plain text/css application/javascript application/json;
}

解决的问题:减少传输体积,提升页面加载速度。文本类资源通常可压缩 60-80%。


15. 静态资源缓存

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|otf)$ {
    # 强缓存:30 天
    expires 30d;
    add_header Cache-Control "public, immutable";
    
    # 禁用日志减少 I/O
    access_log off;
}

location ~* \.(html|htm)$ {
    # HTML 不缓存或短时间缓存
    expires -1;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}
配置 解决的问题
expires 30d 强缓存,浏览器 30 天内不再请求
immutable 声明资源永不改变,连条件请求(304)都跳过
access_log off 静态资源访问不记录日志,减少磁盘 I/O

16. 高效文件传输

http {
    # 内核态直接传输文件,零拷贝
    sendfile on;
    
    # 累积到一定大小再发送(配合 sendfile)
    tcp_nopush on;
    
    # 立即发送小数据包(降低延迟)
    tcp_nodelay on;
}

解决的问题

  • sendfile:避免数据在用户态和内核态之间拷贝,大幅提升静态文件传输性能
  • tcp_nopush + tcp_nodelay:平衡吞吐量和延迟

17. 长连接优化

http {
    # 长连接保持时间
    keepalive_timeout 30s;
    
    # 单个长连接最大请求数
    keepalive_requests 1000;
    
    # 与后端服务保持长连接(反向代理场景)
    upstream backend {
        server 127.0.0.1:8080;
        keepalive 32;  # 保持 32 个空闲连接
    }
}

解决的问题:减少 TCP 握手开销,提升高并发下的性能。


七、反向代理配置

18. 完整的反向代理配置

upstream backend {
    server 127.0.0.1:3000 weight=5;
    server 127.0.0.1:3001 weight=5;
    server 127.0.0.1:3002 backup;  # 备用节点
    keepalive 32;
}

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://backend;
        
        # 传递真实客户端 IP
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 代理相关优化
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        
        # 超时配置
        proxy_connect_timeout 5s;
        proxy_send_timeout 10s;
        proxy_read_timeout 10s;
        
        # 缓冲区
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
    }
}
配置 解决的问题
X-Forwarded-For 后端服务获取真实客户端 IP(而非 Nginx IP)
X-Forwarded-Proto 后端服务知道原始请求是 HTTP 还是 HTTPS
proxy_buffering 缓冲后端响应,优化传输效率
keepalive 与后端保持长连接,减少 TCP 建立开销

八、日志配置

19. 结构化日志与性能监控

http {
    # 自定义日志格式(含性能指标)
    log_format performance '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           'rt=$request_time '
                           'uct="$upstream_connect_time" '
                           'uht="$upstream_header_time" '
                           'urt="$upstream_response_time"';
    
    # JSON 格式(便于 ELK / Grafana 解析)
    log_format json_combined escape=json '{'
        '"time":"$time_iso8601",'
        '"remote_addr":"$remote_addr",'
        '"request":"$request",'
        '"status":$status,'
        '"request_time":$request_time,'
        '"upstream_response_time":"$upstream_response_time",'
        '"body_bytes_sent":$body_bytes_sent,'
        '"http_referer":"$http_referer",'
        '"http_user_agent":"$http_user_agent"'
    '}';
    
    # 主日志
    access_log /var/log/nginx/access.log performance buffer=32k flush=5s;
    
    # API 专用 JSON 日志
    access_log /var/log/nginx/api.log json_combined;
}

解决的问题:便于排查慢请求、监控后端服务健康状态、对接日志分析系统。


九、完整配置模板

# /etc/nginx/nginx.conf

user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /run/nginx.pid;

events {
    worker_connections 4096;
    use epoll;
    multi_accept on;
}

http {
    # 基础设置
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 1000;
    types_hash_max_size 2048;
    server_tokens off;
    
    # 缓冲区
    client_body_buffer_size 128k;
    client_max_body_size 10m;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
    
    # 超时
    client_header_timeout 10s;
    client_body_timeout 10s;
    send_timeout 10s;
    
    # 压缩
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/javascript application/javascript application/json application/xml image/svg+xml;
    
    # MIME 类型
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    # 日志
    log_format performance '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           'rt=$request_time uct="$upstream_connect_time" '
                           'uht="$upstream_header_time" urt="$upstream_response_time"';
    access_log /var/log/nginx/access.log performance buffer=32k flush=5s;
    error_log /var/log/nginx/error.log warn;
    
    # 限流
    limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    
    # 安全响应头
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
    
    # 引入站点配置
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

总结速查表

类别 配置 解决的核心问题
信息隐藏 server_tokens off 版本信息泄露
传输安全 ssl_protocols TLSv1.2 TLSv1.3 中间人攻击、协议降级
劫持防护 X-Frame-Options / frame-ancestors 点击劫持
注入防护 X-Content-Type-Options / CSP XSS、MIME 嗅探
资源盗用 valid_referers 带宽被盗
暴力破解 limit_req / limit_conn 接口被刷、DDoS
连接耗尽 各种 timeout Slowloris 攻击
传输优化 gzip / sendfile / keepalive 加载慢、高延迟
缓存策略 expires / Cache-Control 重复请求、带宽浪费

💡 建议:配置修改后务必执行 nginx -t 检查语法,再用 nginx -s reload 平滑重载,避免服务中断。

How to Install Nginx on Ubuntu 26.04

Nginx (pronounced “engine x”) is an open-source, high-performance HTTP and reverse proxy server used by some of the largest sites on the Internet. It can act as a standalone web server, load balancer, content cache, and proxy for HTTP and non-HTTP services.

This guide explains how to install Nginx on Ubuntu 26.04, configure the firewall, and manage the service.

Prerequisites

Before continuing, make sure you are logged in as a user with sudo privileges , and that no other service such as Apache is running on port 80 or 443.

Installing Nginx

Nginx is available in the default Ubuntu repositories. To install it, run:

Terminal
sudo apt update
sudo apt install nginx

Once the installation is completed, the Nginx service starts automatically. To verify it is running:

Terminal
sudo systemctl status nginx
output
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Fri 2026-04-25 15:00:00 UTC; 10s ago
...

Nginx is now installed and running. You can manage the Nginx service in the same way as any other systemd unit.

Configuring the Firewall

Now that Nginx is running, you need to make sure the firewall allows traffic on HTTP (80) and HTTPS (443). If you are using ufw , enable the Nginx Full profile which covers both ports:

Terminal
sudo ufw allow 'Nginx Full'

To verify the firewall status:

Terminal
sudo ufw status
output
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
Nginx Full ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)

Testing the Installation

To confirm Nginx is serving requests, open http://YOUR_IP in a browser. You should see the default Nginx welcome page. You can also test from the command line:

Terminal
curl -I http://localhost
output
HTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)
...

Nginx Configuration Structure

  • All Nginx configuration files are located in /etc/nginx/.
  • The main configuration file is /etc/nginx/nginx.conf.
  • Create a separate configuration file for each domain to keep things manageable. Server block files are stored in /etc/nginx/sites-available/ and must be symlinked to /etc/nginx/sites-enabled/ to be active.
  • Follow the standard naming convention: if your domain is mydomain.com, name the file /etc/nginx/sites-available/mydomain.com.conf.
  • The /etc/nginx/snippets/ directory holds reusable configuration fragments that can be included in server block files.
  • Log files (access.log and error.log) are located in /var/log/nginx/. Use a separate log file per server block.
  • Common document root locations: /var/www/<site_name>, /var/www/html/<site_name>, or /home/<user>/<site_name>.

Conclusion

Nginx is now installed and ready to serve your applications on Ubuntu 26.04. To set up multiple sites on the same server, see the guide on Nginx server blocks .

❌