插件窝 干货文章 Nginx记录分析响应慢的请求及替换网站响应内容怎么配置

Nginx记录分析响应慢的请求及替换网站响应内容怎么配置

替换 slow Nginx filter 214    来源:    2025-04-26

Nginx配置:记录慢请求与内容替换

一、记录响应慢的请求

要记录处理时间过长的请求,可以使用Nginx的ngx_http_log_module$request_time变量。

配置方法:

  1. 在http或server块中添加日志格式
http {
    log_format slow_requests '$remote_addr - $remote_user [$time_local] '
                            '"$request" $status $body_bytes_sent '
                            '"$http_referer" "$http_user_agent" '
                            'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

    server {
        access_log /var/log/nginx/slow_requests.log slow_requests if=$slow_condition;

        # 定义慢请求条件(例如响应时间超过2秒)
        map $request_time $slow_condition {
            default 0;
            "~^[5-9]\." 1;  # 5-9秒
            "~^[1-9][0-9]" 1; # 10秒以上
        }
    }
}
  1. 更精确的慢请求日志(使用location)
server {
    location / {
        access_log /var/log/nginx/slow_requests.log slow_requests if=$slow_condition;
        proxy_pass http://backend;

        # 记录所有超过3秒的请求
        set $threshold 3;
        set $slow_condition 0;

        if ($request_time > $threshold) {
            set $slow_condition 1;
        }
    }
}

二、替换网站响应内容

Nginx可以通过ngx_http_sub_module模块实现响应内容替换。

1. 确保模块已安装

检查Nginx是否包含--with-http_sub_module编译选项。

2. 基本配置示例:

server {
    location / {
        proxy_pass http://backend;

        # 启用替换过滤器
        sub_filter '原内容' '替换内容';
        sub_filter_once off; # 替换所有匹配项而不仅是第一个
        sub_filter_types text/html text/css application/javascript; # 指定MIME类型

        # 当替换内容包含变量时需要设置
        sub_filter_last_modified on;
    }
}

3. 高级替换示例:

location / {
    proxy_pass http://backend;

    # 多组替换
    sub_filter '旧文本1' '新文本1';
    sub_filter '旧文本2' '新文本2';

    # 正则表达式替换(需要Nginx 1.9.4+)
    sub_filter 'href="(.*?)"' 'href="https://newdomain.com$1"';

    # 只对特定内容类型生效
    sub_filter_types text/html;

    # 确保代理头正确处理
    proxy_set_header Accept-Encoding "";
}

三、综合配置示例

http {
    log_format slow_requests '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" '
                           'rt=$request_time';

    server {
        listen 80;
        server_name example.com;

        access_log /var/log/nginx/access.log combined;
        access_log /var/log/nginx/slow.log slow_requests if=$is_slow;

        set $is_slow 0;
        set $slow_threshold 2; # 2秒阈值

        location / {
            proxy_pass http://backend;

            # 慢请求检测
            if ($request_time > $slow_threshold) {
                set $is_slow 1;
            }

            # 内容替换
            sub_filter '旧公司名' '新公司名';
            sub_filter '旧电话' '400-123-4567';
            sub_filter_once off;
            sub_filter_types text/html text/plain;

            # 禁用上游gzip以允许内容替换
            proxy_set_header Accept-Encoding "";
        }
    }
}

注意事项

  1. 内容替换会影响性能,特别是对大响应体或频繁替换
  2. 对于HTTPS站点,确保替换内容不会破坏安全相关头部
  3. 慢请求日志可能会快速增长,需要定期轮转或清理
  4. 在生产环境修改前,建议先在测试环境验证配置

以上配置可以根据实际需求进行调整,如修改慢请求阈值、替换规则等。