linux:nginx

NGINX - ハイパフォーマンス ロードバランサー、Webサーバー、リバースプロキシ

$ sudo dnf install nginx
whitewaterfoundry_wslu                                                        212  B/s | 833  B     00:03
依存関係が解決しました。
==========================================================================================================
 Package                     Architecture       Version                        Repository            Size
==========================================================================================================
インストール:
 nginx                       x86_64             1:1.16.1-1.fc31                fedora               564 k
依存関係のインストール:
 gperftools-libs             x86_64             2.7-6.fc31                     fedora               322 k
 nginx-mimetypes             noarch             2.1.48-6.fc31                  fedora                19 k

トランザクションの概要
==========================================================================================================
インストール  3 パッケージ

ダウンロードサイズの合計: 905 k
インストール済みのサイズ: 3.5 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/3): nginx-mimetypes-2.1.48-6.fc31.noarch.rpm                                69 kB/s |  19 kB     00:00
(2/3): gperftools-libs-2.7-6.fc31.x86_64.rpm                                  586 kB/s | 322 kB     00:00
(3/3): nginx-1.16.1-1.fc31.x86_64.rpm                                         802 kB/s | 564 kB     00:00
----------------------------------------------------------------------------------------------------------
合計                                                                          575 kB/s | 905 kB     00:01
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                  1/1
  インストール中   : nginx-mimetypes-2.1.48-6.fc31.noarch                                              1/3
  インストール中   : gperftools-libs-2.7-6.fc31.x86_64                                                 2/3
  インストール中   : nginx-1:1.16.1-1.fc31.x86_64                                                      3/3
  scriptletの実行中: nginx-1:1.16.1-1.fc31.x86_64                                                      3/3
  検証             : gperftools-libs-2.7-6.fc31.x86_64                                                 1/3
  検証             : nginx-1:1.16.1-1.fc31.x86_64                                                      2/3
  検証             : nginx-mimetypes-2.1.48-6.fc31.noarch                                              3/3

インストール済み:
  gperftools-libs-2.7-6.fc31.x86_64  nginx-1:1.16.1-1.fc31.x86_64  nginx-mimetypes-2.1.48-6.fc31.noarch
$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

サーバーのプロセッサ数を調べる。
※下記の例では 4 個のプロセッサを搭載している。

$ cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3

ワーカープロセス数を auto または、プロセッサ数に設定。

$ sudo vi /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
#worker_processes auto;
worker_processes  4;

auto の場合はコア数を見て自動設定される。

/etc/nginx/nginx.conf
#worker_processes auto;
worker_processes 4;
worker_rlimit_nofile 8192;               # ワーカープロセスが扱える最大ファイル数
worker_priority 0;                       # ワーカープロセスの優先度
worker_cpu_affinity 0001 0010 0100 1000; # ワーカープロセスが使うCPUコア(4コア)

worker_cpu_affinity はプロセッサの数に応じて変更する。

/etc/nginx/nginx.conf
worker_cpu_affinity 01 10;                         # CPU 2コア
worker_cpu_affinity 001 010 100;                   # CPU 3コア
worker_cpu_affinity 0001 0010 0100 1000;           # CPU 4コア
worker_cpu_affinity 00001 00010 00100 01000 10000; # CPU 5コア
/etc/nginx/nginx.conf
events {
    multi_accept off;        # リスニングソケットオープン時の相互排他制御
    worker_connections 1024; # ワーカープロセス同時接続数
    use epoll;               # Linux 3.10 系で効率的なモデル
}
/etc/nginx/nginx.conf
http {
    server_tokens off;       # バージョン情報非表示
    #keepalive_timeout  65;
    keepalive_requests 1000; # KeepAlive 接続で処理できる最大要求数
    keepalive_timeout     5; # 要求が終わって KeepAlive 接続を切るまでの秒数
}
/etc/nginx/nginx.conf
http {
    #types_hash_max_size 2048;
    types_hash_max_size 4096;
}

ファイルのアップロード時に 413 Request Entity Too Large が発生する場合は、デフォルト 1M の制限を変更する。

/etc/nginx/nginx.conf
http {
    #client_max_body_size 1m; # default
    client_max_body_size 100m;
}

設定ファイルの内容を検証する。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx を再起動する。

$ sudo systemctl restart nginx

http://localhost にアクセスする。
NGINX インストール 001

nginx: [warn] could not build optimal types_hash, you should increase either types_hash_max_size: 2048 or types_hash_bucket_size: 64; ignoring types_hash_bucket_size

タイプハッシュテーブルの最大値を変更する。

/etc/nginx/nginx.conf
http {
    #types_hash_max_size 2048;
    types_hash_max_size 4096;
}

/etc/nginx/mime.types に含まれる多数のタイプに対して 2048 では不十分のようである。
1564878 – NGINX: could not build optimal types_hash, you should increase either types_hash_max_size

  • linux/nginx.txt
  • 最終更新: 2023/09/12 10:55
  • by ともやん