さくら VPS に nginx のインストール後の詳細設定メモです。
仕組みとしては nginx で全てのリクエストを受けて、静的ファイル以外は Apache に処理を流すようです。そのためまずは Apache のポート番号を変更します。ブラウザから example.com の動的ファイルにアクセスする場合は http://example.com:1111/ でアクセス、example.org の動的ファイルにアクセスする場合は example.com:2222/ でアクセスできるようにする場合の設定です。
1 | vi /etc/httpd/conf/httpd.conf |
設定ファイルが以下のような感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Listen 1111 Listen 2222 NameVirtualHost *:1111 NameVirtualHost *:2222 <VirtualHost *:1111> DocumentRoot /www/example1 ServerName www.example.com </VirtualHost> <VirtualHost *:2222> DocumentRoot /www/example2 ServerName www.example.org </VirtualHost> |
次に80番ポートで nginx を Listen して静的ファイルを処理して、それ以外は Apache に流すように設定します。ポート番号で振り分けるので、それぞれ Apache で設定した時のポート番号を使用します。
1 | vi /etc/nginx/conf.d/virtual.conf |
設定ファイルは以下のような感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | server { listen 80; server_name www.example.com; location ~ .*\.(jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) { root /www/example1; index index.html; ssi on; break; } location / { proxy_pass http://127.0.0.1:1111; break; } } server { listen 80; server_name www.example.org; location ~ .*\.(jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) { root /www/example2; index index.html; ssi on; break; } location / { proxy_pass http://127.0.0.1:2222; break; } } |
ちなみに .htaccess などは使えないので nginx の設定ファイルで設定します。nginx は使うときはチューニングして徹底的に使う、使わないなら使わないとした方が良いなと個人的に思いました。今回は勉強がてらのインストールで実際には Apache で運用していく予定です。
以下は今回参考にさせて頂いた記事です。有益な情報ありがとうございました。
コメント