Nginx 在单个用户请求但配置文件较大时出现性能问题

Nginx 在单个用户请求但配置文件较大时出现性能问题

我在服务器块中使用了以下配置,随着位置数量的增加,即使只有一个请求,性能也会大幅下降(例如https://example.com/loc1/MyContext/index.html)。直接访问(无需代理)非常快。所谓降级,是指通过代理呈现页面需要几分钟,而直接访问则只需几秒钟。

对同一位置进行多次重定向的目的是为了满足以下原始要求。

https://example.com/loc1 -> should go to https://example.com/loc1/MyContext/index.html
https://example.com/loc1/ -> should go to https://example.com/loc1/MyContext/index.html
https://example.com/loc1/myadmin -> should go to https://example.com/loc1/MyContext/index.html
https://example.com/loc1/admin -> should go to https://example.com/loc1/MyContext/admin.html
https://example.com/loc1/reporter -> should go to https://example.com/loc1/reporter/index.html

用户始终可以输入完整的 URL(例如通过书签)。keepalive_timeout 设置为 0,因为代理服务器不支持 keepalive。公共标头包含在通过 include 指令包含的公共文件中(但为了简单起见,已从本问题中删除)。

对于每个代理服务器,有 6 个位置块支持上述 URL 方案。默认配置文件大小现在超过 250KB。

问题/问题:1. 为什么性能会下降到这样的水平,以至于配置文件中的第一个位置也需要几分钟(有时甚至会超时)。代理服务器对直接访问的响应非常灵敏。2. 如何重构位置配置以缩短响应时间

主配置文件(nginx.conf)如下

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 50000;

events {
    worker_connections  4096;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    underscores_in_headers on;
    proxy_buffering off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log  debug;

    sendfile        on;
    #tcp_nopush     on;
    server_tokens   off;
    keepalive_timeout  0;
    #gzip  on;

    include /etc/nginx/conf.d/default.conf;
}

default.conf如下:

server {
  listen 80;
  return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    server_name example.com;
    ssl on;
    ssl_certificate       /etc/nginx/security/server.pem;
    ssl_certificate_key   /etc/nginx/security/key.pem;
    ssl_session_cache shared:SSL:5m;
    ssl_protocols TLSv1.2;
    server_name_in_redirect on;
    client_max_body_size 100m;

    location = /loc1 {
        try_files $uri $uri/ =301 /loc1/myadmin;
    }
    location = /loc1/myadmin {
        return 301 https://example.com/loc1/MyContext/index.html;
    }
    location = /loc1/admin {
        return 301 https://example.com/loc1/MyContext/admin.html;
    }
    location = /loc1/reporter {
        return 301 https://example.com/loc1/reporter/index.html;
    }

    location = /loc1/ {
        return 301 https://example.com/loc1/MyContext/index.html;
    }

    location /loc1/ {
        proxy_pass          https://Sevice_1_IP_Address:443/;
        proxy_set_header        Host <Service_1_IP_Address>;
        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_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade";
        proxy_buffering         off;
        proxy_http_version      1.1;
        proxy_set_header        Origin '';
    }   

    location = /loc2 {
        try_files $uri $uri/ =301 /loc2/myadmin;
    }
    location = /loc2/myadmin {
        return 301 https://example.com/loc2/MyContext/index.html;
    }
    location = /loc2/admin {
        return 301 https://example.com/loc2/MyContext/admin.html;
    }
    location = /loc2/reporter {
        return 301 https://example.com/loc2/reporter/index.html;
    }

    location = /loc2/ {
        return 301 https://example.com/loc2/MyContext/index.html;
    }

    location /loc2/ {
        proxy_pass          https://Sevice_2_IP_Address:443/;
        proxy_set_header        Host <Service_2_IP_Address>;
        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_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade";
        proxy_buffering         off;
        proxy_http_version      1.1;
        proxy_set_header        Origin '';
    }

}

我搜索了 SO 和这个论坛,但没有找到任何与我的问题相关的内容。我很乐意提供任何其他信息。

答案1

显然,在重新启动 k8s pod 后,问题得到了解决。在此之前,我尝试检查与 CPU、内存、磁盘等相关的所有指标,没有什么可担心的。

所以我不知道是什么原因导致了这个修复,所以我会将其视为一次性事件。

我仍然想在单独的问题中探讨配置重构。感谢大家抽出时间。

相关内容