Server ブロックの例¶
注意: "VirtualHost" はApacheの用語です。NGINX は Virtual hostを持ちません。tcpソケットにバインドするために server_name と listen ディレクティブを持つ "Server Blocks"を持ちます。
静的ファイルを提供する2つのServerブロック¶
http {
index index.html;
server {
server_name www.domain1.com;
access_log logs/domain1.access.log main;
root /var/www/domain1.com/htdocs;
}
server {
server_name www.domain2.com;
access_log logs/domain2.access.log main;
root /var/www/domain2.com/htdocs;
}
}
デフォルトの "Catch All" Server ブロック¶
http {
index index.html;
server {
listen 80 default_server;
server_name _; # This is just an invalid value which will never trigger on a real hostname.
access_log logs/default.access.log main;
server_name_in_redirect off;
root /var/www/default/htdocs;
}
}
親フォルダ内のワイルドカード サブドメイン¶
これは、DNSレコードがサーバを示す時に、新しいサブドメインを追加し続ける、あるいは自動的に新しいドメインを追加する実際に簡単な方法です。注意: 同様にFCGIをここに追加しました。単に静的ファイルを提供したい倍アは、FCGI設定を取り除き、デフォルトのドキュメントを index.html に変更してくださん。各ドメインごとに新しい vhost.confファイルを生成するのではなく、以下の1つを単に生成します:
server {
# Replace this port with the right one for your requirements
listen 80 default_server; #could also be 1.2.3.4:80
# Multiple hostnames separated by spaces. Replace these as well.
server_name star.yourdomain.com *.yourdomain.com; # Alternately: _
root /PATH/TO/WEBROOT;
error_page 404 errors/404.html;
access_log logs/star.yourdomain.com.access.log;
index index.php index.html index.htm;
# static file 404's aren't logged and expires header is set to maximum age
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need to
fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
}
location ~ /\.ht {
deny all;
}
}