====== NGINX - ハイパフォーマンス ロードバランサー、Webサーバー、リバースプロキシ ======
{{linux:nginx-logo.svg?200|NGINX Logo}}
本家: [[https://www.nginx.com/|NGINX | High Performance Load Balancer, Web Server, & Reverse Proxy]]\\
ソースコード: [[http://hg.nginx.org/nginx/|nginx: Mercurial repository]]\\
===== インストール =====
$ 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
===== NGINX の設定 =====
==== 自動起動設定 ====
$ 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
#worker_processes auto;
worker_processes 4;
**auto** の場合はコア数を見て自動設定される。\\
==== その他ワーカープロセスの設定 ====
#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** はプロセッサの数に応じて変更する。\\
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コア
==== Events モジュールの設定 ====
events {
multi_accept off; # リスニングソケットオープン時の相互排他制御
worker_connections 1024; # ワーカープロセス同時接続数
use epoll; # Linux 3.10 系で効率的なモデル
}
==== セッション、セキュリティ設定 ====
http {
server_tokens off; # バージョン情報非表示
#keepalive_timeout 65;
keepalive_requests 1000; # KeepAlive 接続で処理できる最大要求数
keepalive_timeout 5; # 要求が終わって KeepAlive 接続を切るまでの秒数
}
==== タイプハッシュテーブルの最大値を設定 ====
http {
#types_hash_max_size 2048;
types_hash_max_size 4096;
}
==== アップロードサイズ制限の変更 ====
ファイルのアップロード時に **413 Request Entity Too Large** が発生する場合は、デフォルト 1M の制限を変更する。\\
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|]] にアクセスする。\\
{{:linux:nginx_install_001.png?800|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
タイプハッシュテーブルの最大値を変更する。\\
http {
#types_hash_max_size 2048;
types_hash_max_size 4096;
}
/etc/nginx/mime.types に含まれる多数のタイプに対して 2048 では不十分のようである。\\
[[https://bugzilla.redhat.com/show_bug.cgi?id=1564878|1564878 – NGINX: could not build optimal types_hash, you should increase either types_hash_max_size]]\\
===== 参考文献 =====
[[http://nginx.org/en/docs/http/ngx_http_core_module.html|Module ngx_http_core_module]]\\
[[https://www.nginx.com/resources/wiki/start/topics/tutorials/install/|Install | NGINX]]\\
[[http://shim0mura.hatenadiary.jp/entry/20120110/1326198429|入門! nginx - tumblr]]\\
[[https://blog.dksg.jp/2012/04/centosnginx-php-fpminstallwordpress.html|CentOSにNginx + php-fpmをInstall、設定してWordPressを動かす]]\\