Nginx とはマシンをWebマシンにするためのソフトウェア。既製品の配給は得意で非常に高速だが、組み立て配給は少し苦手。ただもう古いしオワコンな感じみたい。
Nginx を選ぶ理由
- Nginx+リバースプロクシが高速
- C10K問題に備えたアーキテクチャ
- 少ないリソースで沢山働いてくれる
- リクエストを同時並行で処理する
ソースからインストール
どのアプリにも言えることだが、ソースからインストールするとディレクトリが /usr/local/ にまとめて配置されて良い。パッケージ版だと分散される。
$ yum install pcre-devel openssl-devel ← ビルドに必要なライブラリ。zlib は openssl-devel の依存ファイル $ wget https://nginx.org/download/nginx-1.15.0.tar.gz $ cd nginx-1.15.0 $ ./configure ¥ --sbin-path=/usr/sbin/nginx ¥ --with-http_ssl_module ¥ --with-http_v2_module ← HTTP2 対応 $ make $ sudo make install
phpinfo の表示まで
設定ファイルは極力デフォルトのまま
php-fpm の設定
# useradd --shell /sbin/nologin nginx # yum install php-fpm # vi /etc/php-fpm.d/www.conf user = nginx group = nginx listen = /var/run/php-fpm.sock listen.owner = nginx listen.group = nginx # systemctl restart php-fpm ← 再起動で自動的に上記ソケットが作成される # chown nginx:nginx /var/run/php-fpm.sock
Nginx の設定
修正・追記は下線部
# vi /usr/local/nginx/conf/nginx.conf user nginx; http { server_tokens off; server { server_name hoge.com; location / { root html; index index.php index.html index.htm; } location ~ ¥.php$ { root html; fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } # nginx -s reload # firewall-cmd --add-service=http --permanent # firewall-cmd --reload
WordPress でカスタムパーマリンク設定
location / { root html; index index.php index.html index.htm; try_files $uri $uri/ @wordpress; ← 最後に @wordpressブロックへ } location ~ \.php$ { root html; try_files $uri @wordpress; ← 最後に @wordpressブロックへ fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; fastcgi_pass unix:/var/run/php-fpm.sock; } location @wordpress { fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/index.php; ← ★リクエストに関係なく全て index.php を実行 fastcgi_index index.php; fastcgi_pass unix:/var/run/php-fpm.sock; }
その他の主な設定
ホストごとのコネクション数の制限
http { limit_conn_zone $binary_remote_addr zone=addr_limit:10m; server { location / { limit_conn addr_limit 100; } } }
gzip圧縮転送
gzip on; gzip_types text/css image/gif image/png image/jpeg text/javascript application/x-javascript application/javascript application/json; gzip_min_length 1k; gzip_disable "msie6";