CentOS6.5でNginx1.7とPHP-FPM5.5を連携してPHPを動かす設定の続き。
前回は「とりあえずPHPが動く」という設定だった。
変更した項目(ディレクティブというらし)も最小限だったし。
今回は、便利そうな設定を色々試してみる。
ただし、リバースプロキシの設定やWordPress用の設定は次回以降です。
基本はこのサイトを参考に。
1. nginx.confの設定
まずは以下のサイトを見て学ぶ。
Nginxの設定が詳しく説明してある。
nginx連載3回目: nginxの設定、その1 – インフラエンジニアway – Powered by HEARTBEATS
設定ファイルを編集する。
sudo vim /etc/nginx/nginx.conf
変更後の全体像がこちら。
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush off;
keepalive_timeout 65;
gzip on;
gzip_http_version 1.0;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
include /etc/nginx/conf.d/*.conf;
}
では、変更したディレクティブを見ていく。
最大同時接続数の上限を変更をする。
CPUの数に合わせた数に。
worker_processes 2;
CPUの数は以下のコマンドで確認できる。
grep processor /proc/cpuinfo | wc -l
workerプロセスが最大に開けるファイル数の制限(最大同時接続数)を設定。
worker_processesが2でworker_connectionsが1024なら2048開ける。
worker_connections 1024;
最大プロセス数はサーバごとに違うので、以下のコマンドで確認。
さくらVPSの1Gプランは1024だった。
ulimit -n
サーバ情報の隠蔽を行う。
server_tokensディレクティブを新たに追加する。
server_tokens off;
次は高速化の設定。
レスポンスヘッダとファイルの内容をまとめて送るようになり、少ないパケット数で効率良く送ることができる、らしい。
sendfile on; tcp_nopush off;
レスポンスの圧縮の設定。
gzipディレクティブについてはnginx最大パフォーマンスを出すための基本設定 – オープンソーサー・日本を参考に。
参考先をそのままコピーすると、gzip_typesのところにapplication/jsonが2つあってエラーになるので注意!
gzip on; gzip_http_version 1.0; gzip_disable "msie6"; 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;
gzip_staticとgunzipの設定については保留。
この技術関係しか書いてないブログは、gzipに対応したブラウザで閲覧されると思うから。
一応使えるかどうか、必要なモジュールがインストールされているか確認しとく。
nginx -V
下の2つが表示されれば、必要なモジュールはインストールされている。
今後設定するかもかもね。
–with-http_gunzip_module –with-http_gzip_static_module
設定に間違いがないか確認。
sudo /etc/init.d/nginx configtest
2. default.confの設定
この記事をよく読む。
nginx連載4回目: nginxの設定、その2 – バーチャルサーバの設定 – インフラエンジニアway – Powered by HEARTBEATS
nginx連載5回目: nginxの設定、その3 – locationディレクティブ – インフラエンジニアway – Powered by HEARTBEATS
バーチャルサーバの設定ファイルを開く。
sudo vim /etc/nginx/conf.d/default.conf
変更後のdefault.confがこれ。
server {
listen 80 default_server;
server_name runble1.com www.runble1.com;
if ($http_host = www.runble1.com) {
rewrite (.*) http://runble1.com$1;
}
access_log /var/log/nginx/runble1_access.log main;
error_log /var/log/nginx/runble1_error.log warn;
location / {
root /var/www;
index index.php;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
変更した箇所を見ていこう。
デフォルトサーバの設定。
defaultと付ければいいのか、default_serverと付ければいいのか迷ったが、本家サイトではdefault_serverと書いてあった。
付けなくてもいいのかも・・・
listen 80 default_server;
サーバネームの設定。
wwwありなしを書いておく。
server_name runble1.com www.runble1.com;
wwwありなしを統一。
www.runble1.comでアクセスされたら、runble1.comへリダイレクト。
if ($http_host = www.runble1.com) {
rewrite (.*) http://runble1.com$1;
}
バーチャルサーバのログの設定。
access_log /var/log/nginx/log/runble1_access.log main; error_log /var/log/nginx/log/runble1_error.log warn;
設定に間違いがないか確認。
sudo /etc/init.d/nginx configtest
設定を反映するコマンド。
再起動しなくてもいい。してもいい。
sudo /etc/init.d/nginx reload
3. おまけ:Nginxのエラー
以下のエラーは管理者権限(sudo)でconfigtestしなかった時に出る。
気をつけろ。
nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (13: Permission denied)
nginx: [warn] the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
参考
これから始める人のためのNginx(2):Nginxのインストールと基本設定 (4/4) – @IT
nginx最大パフォーマンスを出すための基本設定 – オープンソーサー・日本
CentOS 7 でLAMP(Nginx+MariaDB(MySQL)+PHP)インストールからWordPressを動かすまで(Nginx編) | レンタルサーバー・自宅サーバー設定・構築のヒント

コメント