使用 Angular 的 Nginx 反向代理时 TTFB 很慢(60 秒)

使用 Angular 的 Nginx 反向代理时 TTFB 很慢(60 秒)

有问题的服务器运行的是 Ubuntu 16.04,通过反向代理为 Angular 应用程序提供服务。连接到主页后,并非所有页面都需要这么长时间才能加载,但有些页面确实需要这么长时间。即https://我的网站/admin页面。这里还有一些缺失的 js 按钮,虽然页面的其他方面都显示出来,但它们并没有显示在页面上。我们的网站是使用 js 脚本来路由流量的,但这是 TTFB 为 60 秒的文件之一!(大多数 TTFB 较慢的文件都是 js)虽然整个应用程序在没有反向代理的情况下无法工作,但我可以确认它何时不起作用。此外,补充一点可能会有所帮助,即网站上的 TTFB 在第一次加载后并不总是 60 秒,但在隐身窗口中加载时始终是 60 秒。

nginx.conf

user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
        worker_connections 1024;
}

http {
    proxy_cache_path /etc/nginx-cache levels=1:2 keys_zone=backcache:8m max_size=50m;                                                                                                                                                         
    proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;

    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

    upstream mysite {
            server [::]:1337;
     }


    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    #Looked at 12/4
    #fastcgi_buffers 8 16k;
    #fastcgi_buffer_size 32k;

    #client_max_body_size 24M;
    #client_body_buffer_size 128k;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";
     application/javascript                                                                                                                                                              text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

站点启用/默认.conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        listen 443 ssl http2; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/mysite.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mysite.net-0001/privkey.pem; # managed by Certbot

        root /home/admin_user/root;

        index index.html index.htm index.nginx-debian.html;

        server_name mysite.net;

        proxy_buffering on;
        proxy_buffer_size 1k;
        proxy_buffers 24 4k;
        proxy_busy_buffers_size 8k;
        proxy_max_temp_file_size 2048m;
        proxy_temp_file_write_size 32k;

        location / {
                sendfile on;
                tcp_nopush on;
                tcp_nodelay on;
                proxy_cache backcache;
                proxy_cache_bypass $http_cache_control;
                add_header X-Proxy-Cache $upstream_cache_status;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;

                proxy_http_version 1.1;
                proxy_set_header Connection "";
                proxy_pass http://localhost:1337;
    }
}

答案1

好的,根据您的消息,我认为您有一个用于反向代理的复制粘贴的 nginx 配置设置。

您的配置包含以下节:

location / {
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            proxy_cache backcache;
            proxy_cache_bypass $http_cache_control;
            add_header X-Proxy-Cache $upstream_cache_status;

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;

            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_pass http://localhost:1337;
}

这说明 - 对于 下的任何内容/,发送请求到localhost:1337您的评论中的内容可能不存在。Nginx 有 60 秒的超时时间,所以我猜它会等待这么长时间,然后回退到/home/admin_user/root

您需要做的是将您的配置更改为:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        listen 443 ssl http2; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/mysite.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mysite.net-0001/privkey.pem; # managed by Certbot

        index index.html index.htm index.nginx-debian.html;

        server_name mysite.net;

        location / {
            root /home/admin_user/root;
        }
    }

由于 angular 是客户端渲染应用程序 - 您没有被代理的后备服务器。因此,只需在 中交付资产即可/home/admin_user/root

相关内容