LoginSignup
84
88

More than 5 years have passed since last update.

Nginxのバーチャルホスト設定

Last updated at Posted at 2015-10-01

バーチャルホストの設定についての体系的な記述が見当たらなかった。
下記の設定方法が正当であるかどうかは不明であるが一つの定番例として覚え書き。

バーチャルホスト設定ファイル配置用のディレクトリ作成

これは習慣的な方法らしく、sites-availableに設定ファイル実体を配置して、sites-enabledに設定ファイルへのシンボリックリンクを張るらしい。
シンボリックリンクを削除することで、簡単にバーチャルホストのサイトを閉鎖することが可能となる。

$ mkdir /etc/nginx/sites-available
$ mkdir /etc/nginx/sites-enabled

sites-availableに設定ファイルを配置

/etc/nginx/sites-available/virtualhost.com
server {
    listen       80;
    server_name www.virtualhost.com;
    access_log  /var/log/nginx/virtualhost.com.access.log;
    location / {
        root   /var/www/html/virtualhost;
        index  index.html index.htm index.php;
    }
    location ~ \.php$ {
        root           /var/www/html/virtualhost;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/virtualhost$fastcgi_script_name;
        include        fastcgi_params;
    }
}
$ cd /etc/nginx/sites-enabled/
$ ln -s /etc/nginx/sites-available/virtualhost.com virtualhost.com

シンボリックリンクのあるsites-enabledに対してインクルード

/etc/nginx/nginx.conf
include /etc/nginx/sites-enabled/*;

Apache HTTP Serverに比べると、直感的にも設定ファイルがわかりやすく記述できる。

84
88
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
84
88