Performance 如何正确设置Nginx以获得最小的TTFB延迟?

Performance 如何正确设置Nginx以获得最小的TTFB延迟?,performance,nginx,ruby-on-rails-5,puma,time-to-first-byte,Performance,Nginx,Ruby On Rails 5,Puma,Time To First Byte,我有一个rails应用程序,在生产环境中运行在Nginx和Puma上 网页加载(TTBF延迟)有问题,我正在试图找出原因 在production.log的后端,我看到我的网页在134ms的时间内呈现得足够快: Completed 200 OK in 134ms (Views: 49.9ms | ActiveRecord: 29.3ms) 但在浏览器中,我看到TTFB是311.49ms: Completed 200 OK in 134ms (Views: 49.9ms | ActiveRecor

我有一个rails应用程序,在生产环境中运行在Nginx和Puma上

网页加载(TTBF延迟)有问题,我正在试图找出原因

在production.log的后端,我看到我的网页在134ms的时间内呈现得足够快:

Completed 200 OK in 134ms (Views: 49.9ms | ActiveRecord: 29.3ms)
但在浏览器中,我看到TTFB是311.49ms

Completed 200 OK in 134ms (Views: 49.9ms | ActiveRecord: 29.3ms)

我知道设置或过程中可能存在问题,计数可能不是最优的,但无法找到~177ms延迟的原因。。我将非常感谢你的建议

下面列出了我的VPS属性和配置

环境

  • Nginx 1.10.3
  • Puma 3.12.0(轨道5.2)
  • PostgreSQL
  • 西德基
  • 弹性搜索
VPS属性

  • Ubuntu 16.04(64位)
  • 8芯(2.4 GHz)
  • 16gb内存
  • 网络带宽:1000 Mbps
nginx.conf

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

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

http {

  # 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;

  # Logging Settings
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  # Gzip Settings
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
upstream puma {
  server unix:///home/deploy/apps/web_app/shared/tmp/sockets/web_app-puma.sock fail_timeout=0;
}

log_format timings '$remote_addr - $time_local '
                   '"$request" $status '
                   '$request_time $upstream_response_time';

server {
  server_name web_app.com;

  # SSL configuration
  ssl on;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;

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

  ssl_certificate  /etc/ssl/certs/cert.pem;
  ssl_certificate_key /etc/ssl/private/key.pem;

  root /home/deploy/apps/web_app/shared/public;

  access_log /home/deploy/apps/web_app/current/log/nginx.access.log;
  error_log /home/deploy/apps/web_app/current/log/nginx.error.log info;
  access_log /home/deploy/apps/web_app/current/log/timings.log timings;

  location ^~ /assets/ {
    #gzip_static on;
    expires max;
    add_header Cache-Control public;
    add_header Vary Accept-Encoding;
    access_log off;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_request_buffering off;
    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;

  client_body_buffer_size 8K;
  client_max_body_size 10M;
  client_header_buffer_size 1k;
  large_client_header_buffers 2 16k;
  client_body_timeout 10s;
  keepalive_timeout 10;

  add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
}
web_app.conf

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

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

http {

  # 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;

  # Logging Settings
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  # Gzip Settings
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
upstream puma {
  server unix:///home/deploy/apps/web_app/shared/tmp/sockets/web_app-puma.sock fail_timeout=0;
}

log_format timings '$remote_addr - $time_local '
                   '"$request" $status '
                   '$request_time $upstream_response_time';

server {
  server_name web_app.com;

  # SSL configuration
  ssl on;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;

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

  ssl_certificate  /etc/ssl/certs/cert.pem;
  ssl_certificate_key /etc/ssl/private/key.pem;

  root /home/deploy/apps/web_app/shared/public;

  access_log /home/deploy/apps/web_app/current/log/nginx.access.log;
  error_log /home/deploy/apps/web_app/current/log/nginx.error.log info;
  access_log /home/deploy/apps/web_app/current/log/timings.log timings;

  location ^~ /assets/ {
    #gzip_static on;
    expires max;
    add_header Cache-Control public;
    add_header Vary Accept-Encoding;
    access_log off;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_request_buffering off;
    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;

  client_body_buffer_size 8K;
  client_max_body_size 10M;
  client_header_buffer_size 1k;
  large_client_header_buffers 2 16k;
  client_body_timeout 10s;
  keepalive_timeout 10;

  add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
}
美洲狮.rb

threads 1, 6

port 3000

environment 'production'

workers 8

preload_app!

before_fork    { ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) }
on_worker_boot { ActiveRecord::Base.establish_connection        if defined?(ActiveRecord) }

plugin :tmp_restart
检查后端的真实响应时间 后端可能会声称它在130毫秒内应答/呈现,但这并不意味着它实际上正在这样做。您可以定义如下所示的日志格式:

log_format timings '$remote_addr - $time_local '
    '"$request" $status '
    '$request_time $upstream_response_time';
并将其应用于:

access_log /var/log/nginx/timings.log timings;
这将告诉后端实际需要多长时间来响应

其他可能的调试方法
  • 检查您与服务器之间的原始延迟(即使用
    ping
    或从服务器本身查询)
  • 检查提供静态内容以获得基线的速度

使用缓存 将类似于以下内容添加到位置块:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g 
             inactive=60m use_temp_path=off;
proxy_cache my_cache;
如果您的后端支持“ModModified-since”标头:


禁用缓冲 您可以指示nginx转发来自后端的响应,而无需缓冲响应。这可能会缩短响应时间:

proxy_buffering off;
由于版本1.7.11还存在一个指令,允许nginx将响应转发到后端,而无需对其进行缓冲

proxy_request_buffering off;
检查后端的真实响应时间 后端可能会声称它在130毫秒内应答/呈现,但这并不意味着它实际上正在这样做。您可以定义如下所示的日志格式:

log_format timings '$remote_addr - $time_local '
    '"$request" $status '
    '$request_time $upstream_response_time';
并将其应用于:

access_log /var/log/nginx/timings.log timings;
这将告诉后端实际需要多长时间来响应

其他可能的调试方法
  • 检查您与服务器之间的原始延迟(即使用
    ping
    或从服务器本身查询)
  • 检查提供静态内容以获得基线的速度

使用缓存 将类似于以下内容添加到位置块:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g 
             inactive=60m use_temp_path=off;
proxy_cache my_cache;
如果您的后端支持“ModModified-since”标头:


禁用缓冲 您可以指示nginx转发来自后端的响应,而无需缓冲响应。这可能会缩短响应时间:

proxy_buffering off;
由于版本1.7.11还存在一个指令,允许nginx将响应转发到后端,而无需对其进行缓冲

proxy_request_buffering off;

增加了计时日志。所以现在:rails在174ms内完成渲染,nginx计时显示请求时间180ms,相同的上游响应时间180mBuffering被禁用,因为我不确定“代理”是否正确,因为我在页面上有动态内容,在html表单中有CSRF令牌。这~170ms的延迟对我的nginx配置和VPS属性合适吗?仅供参考,根据“如果proxy_buffering设置为off,NGINX不会缓存响应。默认情况下,它是开启的。”添加了计时日志。所以现在:rails在174ms内完成渲染,nginx计时显示请求时间180ms,相同的上游响应时间180mBuffering被禁用,因为我不确定“代理”是否正确,因为我在页面上有动态内容,在html表单中有CSRF令牌。这~170ms的延迟对我的nginx配置和VPS属性合适吗?仅供参考,根据“如果proxy_buffering设置为off,NGINX不会缓存响应。默认情况下,它是打开的。”