またも作業メモと、設定について。
やらなきゃいけないのは以下2点。
- ドメインの振り分け設定
- パーマリンクのための設定
IP直打ちで初期設定でWordPressを使う分には不要ですが、そんな人はまぁいないと思うので・・w
nginxでのドメイン設定やドキュメントルートについて
ココさえ抑えれば、後は特に悩まなくて良いかと。
逆に色々調べると、あれこれやり方は出てきますが、基本的には一緒みたい。
nginx.conf
nginxのインストールディレクトリにある、nginx.confを主に設定します。
私はちなみにyumでインストールしました。
nginx.confの中身
#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.nginx.org/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################
#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.nginx.org/NginxHttpMainModule
#
#----------------------------------------------------------------------
user nginx;
# worker_processes 1;
worker_processes 2;
worker_cpu_affinity 01 10;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
#----------------------------------------------------------------------
# Events Module
#
# http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------
events {
worker_connections 1024;
}
#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.nginx.org/NginxHttpCoreModule
#
#----------------------------------------------------------------------
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 on;
#keepalive_timeout 0;
keepalive_timeout 45;
#gzip on;
gzip on;
gzip_http_version 1.0;
gzip_min_length 1000;
gzip_comp_level 2;
gzip_proxied expired no-cache no-store private auth;
gzip_vary on;
gzip_types text/plain
text/xml
text/css
text/javascript
image/x-icon
application/xml
application/rss+xml
application/json
application/x-javascript;
gzip_disable "MSIE [1-6]\.";
gzip_disable "Mozilla/4";
# Hide server info.
server_tokens off;
# Cache contents
expires 14d;
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
}
まるまる載せてますが、サーバーによって設定が変わる部分もあるのでご注意。
ちなみにここでは本記事で設定すべし!とした内容はひとつも設定してません。
Webサーバーの大元として、設定すべき部分を書いてます。
なので、各ドメイン単位でのドキュメントルートとか、そういった設定をちゃんとする必要があります。
どこで設定してるかというと、最後の方にある・・・
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
ってとこです。
nginxでは、このnginx.confだけじゃなくてconf.dディレクトリ以下に設定ファイルを置いて、そっちで設定することもできるそうな。
他所様によっては、sites-availableとかsites-enableとかディレクトリを作ってApacheのソレらしく書いてたりもします。
もちろんそれでもできます。
どれが正しいの?って思いがちですが、どれも正しいんですね・・。
その場合は、nginx.confの最後の方に、include...がちゃんと書かれてるはずです。
conf.dディレクトリ
初期設定では、この中に以下のファイルがあると思います。
- default.conf
- ssl.conf
- virtual.conf
default.confには、いわゆるデフォルトサーバーの設定を書きます。
基本的には、ここの内容に従って処理を進めます。
で、より適合する条件があれば、そっちを採用します。
今回は通例に従って、virtual.confに設定していきます。
ここでは、www.example.comと、example.comで1WordPressにします。
blog.example2.comでもう1WordPressにします。
server {
listen 80;
server_name example.com *.example.com;
rewrite ^ http://www.example.com$request_uri? permanent;
}
server {
listen 80;
server_name www.example.com;
access_log /var/www/example/logs/access.log;
error_log /var/www/example/logs/error.log;
location / {
root /var/www/example/public_html;
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public_html$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name blog.example2.com;
access_log /var/www/blog/logs/access.log;
error_log /var/www/blog/logs/error.log;
location / {
root /var/www/blog/public_html;
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^.+?($/-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/blog/public_html$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
最初のブロックで、「なんたら.example.comドメインへのアクセスは、全て以下に書いてるwww.example.comの設定と同じ処理に」する、としています。
で、肝心のwww.example.comへのアクセス時の処理を設定します。
ログの場所とか、ドキュメントルートの場所とかをここで任意の場所に設定できます。
複数のドメインで、という場合は、また同じように書きます。
もうおわかりかとおもいますが、[server]で囲まれてる部分を追加することで、どんどん細かくやっていけます。
1ファイルでいっぱい書きたくないわ!って人のために、さっきのsites-availableとかがあるのかと。
WordPressのパーマリンクのために
最後のブロックのblog.example2.comでは、パーマリンク用に記述を追加しています。
if (!-e $request_filename) {
rewrite ^.+?($/-.*) $1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
これをしないと、パーマリンクを設定した時に、わけのわからんURL遷移になって、とにかく動きません。
設定を反映
/etc/init.d/nginx configtest /etc/init.d/nginx reload
設定ファイルをテストしてみて、OKだったらロードしちゃいましょう。
いきなりロードしちゃってもOKです。
気になるところ
fast-cgiのパスって、どっか一箇所に書けないもんなんでしょうか・・。
やっぱそれぞれのserverディレクティブ単位でちゃんと書かないといけないのかしら。