Nginx パフォーマンスチューニング
main context
worker_processes
worker プロセスの数
autoでCPUのコア数をもとにいい感じにしてくれる
worker_rlimit_nofile
リソース消費の制限
Nginx以外に他のプロセスがリソースを消費していればそれを考慮して減らす
worker_rlimit_nofile * worker_processes < OS全体で扱えるファイル数 になるようにする
events context
woker_connections
同時に貼れるコネクションの数
worker_connections * 4 < worker_rlimit_nofile くらいになるようにする
multi_accept on
同時に複数リクエストを受けれるように
use epoll
select, pollとかが選べるが、epollが計算量一番少ないらしい
http context
server_tokens off
HTTPレスポンスヘッダーにNginxのバージョンを含めない
access_log, error_log
少しでもパフォーマンスを上げるならoffに
tcp_nopush on
1つのデータに全てのヘッダー情報を含める
tcp_nodelay on
データをキャッシュせず送信させる
sendfile on
ebiken.iconよくわからない
keepalive_timeout, client_header_timeout, client_body_timeout, send_timeout, resolver_timeout
それぞれ通信周りのタイムアウト設定
デフォルトで60sや30sになっている
遅いリクエストのコネクションを長く保持してしまうので短めに設定する
reset_timedout_connection
非アクティブなコネクションをクローズする
open_file_cache, open_file_cache_valid, open_file_cache_min_uses, open_file_cache_errors
ファイルオープンのキャッシュ
gzip on
gzip_http_version, gzip_proxied, gzip_min_length, gzip_comp_level, gzip_types
limit_conn_zone
共有メモリ設定
ebiken.iconよくわからない
limit_conn addr
アドレスあたりのコネクション数制限
server context
http2
location context
expires, add_header Cache-Control, gzip_static
静的ファイルをクライアントでキャッシュさせる
upstream context
fail_timeout
keepalive
まとめ
worker_processes 2のときの設定
code:nginx.conf
user www-data;
pid /run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 32767;
events {
worker_connections 8191;
multi_accept on;
use epoll;
accept_mutex_delay 100ms;
}
# error_log off;
http {
charset UTF-8;
include /etc/nginx/mime.types;
server_tokens off;
# access_log off;
tcp_nopush on;
tcp_nodelay on;
sendfile on;
keepalive_timeout 10s;
client_header_timeout 10s;
client_body_timeout 10s;
send_timeout 10s;
resolver_timeout 10s;
reset_timedout_connection on;
open_file_cache max=100000 inactive=60s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
gzip on;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
server {
listen 80; # TLS使うなら書き換える
# TLS設定
# listen 443 ssl http2;
# ssl_certificate /etc/nginx/tls/server.crt;
# ssl_certificate_key /etc/nginx/tls/server.key;
# ssl_protocols TLSv1.2;
# ssl_prefer_server_ciphers on;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# proxy_set_header X-Forwarded-Proto "https";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header True-Client-IP $remote_addr;
proxy_set_header Host $http_host;
upstream app {
server <host>:<port> fail_timeout=0;
keepalive 128;
}
location ~ .*\.(html|css|js|jpg|jpeg|png|gif|ico)$ {
root /puglic; # 静的ファイルの場所
gzip_static always;
expires 30d;
add_header Cache-Control "public,max=age=2592000,immutable";
}
location / {
}
}
}