ドキュメント
仮想ホスト
Winginx のサイトは既定設定で自動的に処理されます。winginx\home\mysite.local\public_html のようなフォルダを作れば、再起動なしで http://mysite.local/ または http://www.mysite.local/ で動作します。hosts ファイル への登録は HostsEditor または手動で行ってください。
自動設定の内容:
- listen: 127.0.0.1:80
- server_name: mysite.local と www.mysite.local
- access_log: winginx\logs\mysite.local-access.log
- index: index.php index.html
- charset: utf-8
location の例:
# . で始まるリクエストを拒否 (.htaccess, .user.ini など)
location ~ /\. {
deny all;
}
# / で始まる通常リクエスト
location / {
}
# favicon.ico と robots.txt
location = /favicon.ico { }
location = /robots.txt { }
# PHP
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
多くの場合この設定で十分です。
個別設定が必要なときは winginx\conf\vhosts に ドメイン名.conf(例: mysite.local.conf)を作成します。
設定の追加・変更後は Winginx を再起動してください(start-winginx.exe をダブルクリック)。
問題があれば winginx\logs\error.log を確認してください。
設定例
404、403 などのエラーページ
server {
listen 127.0.0.1:80;
server_name test.local www.test.local;
root home/test.local/public_html;
index index.php index.html;
log_not_found off;
charset utf-8;
access_log logs/test.local-access.log main;
error_page 404 /404.html;
error_page 403 /403.html;
location ~ /\. {
deny all;
}
location / {
}
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
インデックスファイルの変更
server {
listen 127.0.0.1:80;
server_name test.local www.test.local;
root home/test.local/public_html;
index default.php default.html;
...
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass localhost:9000;
fastcgi_index default.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
ポートを 80 から 8001 などに変更
server {
listen 127.0.0.1:8001;
server_name test.local www.test.local;
...
}
エイリアス(複数 server_name)
server {
listen 127.0.0.1:80;
server_name test.local www.test.local test2.local test3.local test4.local;
# 列挙したドメインを hosts に追加すること
...
}
Node.js と PHP の併用
server {
listen 127.0.0.1:80;
server_name test.local www.test.local;
root home/test.local/public_html;
index index.php index.html;
log_not_found off;
charset utf-8;
access_log logs/test.local-access.log main;
location ~ /\. {
deny all;
}
location / {
if (!-e $request_filename) {
# ファイルがなければ Node.js へ
proxy_pass http://localhost:1337;
}
}
# .php は PHP
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 例: /chat/ を Node.js へ
location /chat/ {
if (!-e $request_filename) {
proxy_pass http://localhost:1337;
}
}
}