はじめに
Nginx は Web サーバーです。他の有名な Web サーバーに Apache がありますが、違いはプロセス(あるいはワーカー) ベースの処理か、イベントベースの処理か、になります。Nginx はイベントベースの Web サーバーで、同時リクエストの処理を得意とします。一方で、レスポンスまでに時間がかかるような、サーバーサイドで実行するプログラムのハンドリングは苦手です。このようなスクリプトは、FastCGI プロセスや uWSGI プロセスに中継して実行してもらうようにすることで、Nginx がボトルネックにならないように構成する必要があります。Nginx に uWSGI を連携させる手順についてはこちらの投稿を参照してください。
今回、Nginx を CentOS7 にインストールする手順と、PHP スクリプトを FastCGI で実行できるようにする PHP FPM のインストール手順を紹介します。
Nginx インストール
標準のリポジトリには Nginx パッケージが用意されていません。Nginx 公式のリポジトリが用意されていますので、これをインストールしてから Nginx をインストールします。
Nginx リポジトリのインストール
$ sudo yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Nginx パッケージのインストール
$ sudo yum install --enablerepo=nginx nginx
Nginx 初期設定
Nginx サーバーの設定は基本部分の設定と、バーチャルホスト毎の設定の 2 つに大きく分けられます。今回、基本部分の設定は変更せず、default.conf をベースにバーチャルホストの設定を紹介したいと思います。
CGI を含まない、静的コンテンツのみ配信するバーチャルホスト
最も基本的な構成です。default.conf からの変更は下記のとおりです。
- IPv6 に対応
- 複数のバーチャルホストを管理しやすいようにドキュメントルートを変更
また、ドキュメントルート (root) を location ブロックの外に出してバーチャルホスト内で一意になるように設定 - エラーログ、エラードキュメント周りを有効化
- Apache 用のアクセス制御ファイル .htaccess がドキュメントルート以下にあっても内容を読み取られないように設定
etc/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name static.example.com;
root /var/www/static.example.com/public_html;
access_log /var/log/nginx/static.example.com-access_log main;
error_log /var/log/nginx/static.example.com-error_log;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
index index.html index.htm;
}
location ~ /\.ht {
deny all;
}
}
WordPress 用に、PHP FPM と連携してサービスするバーチャルホスト
WordPress は PHP スクリプトで動的なコンテンツを配信します。Nginx はコンテンツ配信にのみ特化しており、動的なコンテンツの生成はできません。動的なコンテンツの生成を PHP FPM に実行してもらうよう構成します。
WordPress は .htaccess でアクセスを制御しますが、Nginx は .htaccess を解釈できないため、.htaccess に記述されている制御内容をここで記述しておく必要があります。
/etc/nginx/conf.d/wp.example.com.conf
server {
listen 80;
listen [::]:80;
server_name wp.example.com;
root /var/www/wp.example.com/public_html;
access_log /var/log/nginx/wp.example.com-access_log main;
error_log /var/log/nginx/wp.example.com-error_log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
disable_symlinks off;
location / {
index index.php index.html index.htm;
}
# Settings for WordPress access.
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ index.php last;
}
# Settings for PHP FPM.
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
PHP-FPM のインストールについてはこちらの投稿を参照してください。
HTTPS アクセスをサポートするバーチャルホスト
HTTPS はリクエストからレスポンスまでをクライアント(ブラウザー)とサーバーの 2 者しかわからないよう暗号化して、安全なやりとりができるようになる仕組みです。SSL 証明書が別途必要になります。SSL 証明書の申請からインストール方法はこちらの投稿に、SSLv3無効化など、SSL関連のセキュリティ向上策はこちらの投稿に、それぞれまとめてありますので参照してください。
バーチャルホストの設定ですが、HTTPS を有効にする場合は HTTP のアクセスも HTTPS にアップグレードするよう 301 リダイレクトを指示できるようになります。HTTP でないと都合が悪くなる場合を除き、いつも HTTPS でアクセスしてもらうようにしてセキュリティを確保しておくことをお勧めします。
いままで listen 80 に記述していた内容を listen 443 のブロックに移して、ssl から始まる 3 行分の設定を追加しました。一方で、listen 80 のブロックは 301 リダイレクトだけするよう変更しました。このとき、ドメイン名以降にあるリクエスト URI も忘れず転送するようにしておきます。
Nginx 1.16.1 では ssl ディレクティブが deprecated になり、ssl on;
と記述すると warning が発生するようになりました。代わりに listen ディレクティブを使用して listen 443 ssl;
と記述するようにします。
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/www.example.com.conf:18
/etc/nginx/conf.d/www.example.com.conf
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com
www.example.com;
root /var/www/www.example.com/public_html
access_log /var/log/nginx/www.example.com-access_log main;
error_log /var/log/nginx/www.example.com-error_log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
disable_symlinks off;
location / {
index index.php index.html index.htm;
}
# Settings for SSL.
ssl_certificate /etc/pki/tls/certs/www.example.com.bundle.crt;
ssl_certificate_key /etc/pki/tls/private/www.example.com.key;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDH !aNULL !eNULL !SSLv2 !SSLv3';
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
# Settings for WordPress access.
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/wp-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ index.php last;
}
# Settings for PHP FPM.
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
listen [::]:80;
server_name example.com
www.example.com;
return 301 https://www.example.com$request_uri;
}
Nginx の自動起動設定、起動と停止など
Nginx サーバーを操作するコマンドを紹介します。
自動起動設定
$ sudo systemctl enable nginx
起動
$ sudo systemctl start nginx
停止
$ sudo systemctl stop nginx
設定適用
$ sudo systemctl reload nginx
稼働状況確認
$ sudo systemctl status nginx