0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nginxでマルチサイトを構成するための設定ファイルメモ

Last updated at Posted at 2025-08-17

Nginxとは

Nginxは、現在最も広く使われているWebサーバープログラムです。ロードバランサーによって高速な処理を実現しております。登場当時、Webサーバーと言えばApache一択でしたが、その高速処理能力と設定の自由度の高さであっという間にApacheを追い落とし、現在ではWebサーバープログラムのデファクトスタンダードとなりつつあります。

マルチサイトとは

マルチサイトとは、一つのWebサーバー上で複数のWebサイトを運用することを指します。これによりハード・ソフト両面でリソースを大きく節約することが可能となります。

Nginxでもマルチサイトを構成したい!

Apacheでは標準の設定でマルチサイトが構成できるようになっています。それに対してNginxの場合、一般的な標準設定のままではマルチサイトにはなりません。そこで設定ファイルをいじくってマルチサイトに対応させる必要があります。

Nginxでマルチサイトを構成する方法

Nginxの設定ファイルを分割する

Nginxは、設定ファイルを分割して適宜それらを読み込むことで管理することができます。今回はこの機能を利用してマルチサイトを構成します。
Nginxの設定ファイルの構成を以下のようにします。

/etc/
  |-nginx/
    |-nginx.conf
    |-sites-available/
      |-www.example1.com.conf
      |-www.example2.com.conf
      |-www.example3.com.conf
    |-sites-enabled/
  

各設定ファイル

/etc/nginx/nginx.conf
# Generic startup file

user http;

worker_processes auto;
worker_cpu_affinity auto;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type text/html;
    sendfile on;
    keepalive_timeout 65;
    rewrite_log on;

    types_hash_bucket_size 2048;
    types_hash_max_size 64;

    include sites-enabled/*;
}

/etc/nginx/sites-available/www.example1.com.conf
server {
    server_name www.example1.com;
    root /srv/http/www.example1.com;
    index index.html index.htm;

    listen 80;
    listen [0::0]:80;
    
    gzip on;

    access_log /var/log/nginx/www.example1.com.access.log;
    error_log /var/log/nginx/www.example1.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~* .(?:css|js) {
	add_header Cache-Controle "public, max-age:31536000";
    }
}

他のサイト設定ファイル(www.example2.com.conf, www.example3.com.conf)も同様に作成します。

サイトの有効化

/etc/nginx/sites-available/に設定ファイルを置いたものの中から、有効化したいものだけをNginxに読み込ませるため、/etc/nginx/sites-enabled/にシンボリックリンクを張ります。
例えば、www.example1.comだけを有効化したい場合には以下のようにします。

$ sudo ln -s /etc/nginx/sites-available/www.example1.com.conf /etc/nginx/sites-enabled/www.example1.com.conf

これで、Nginxをマルチサイトに対応できるようになりました。あとは好きなだけ設定ファイルを/etc/nginx/sites-availableの中に作成して、使いたいものだけをシンボリックリンクを張ればOKです。

構成ファイルのチェック

構成ファイルにエラーがないか、以下のコマンドでチェックをかけます。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

と表示されれば準備完了です。

Nginxを再起動する

最後にNginxを再起動して出来上がりです。

$ sudo systemctl restart nginx

まとめ

今回はNginxの設定ファイルを分割することによって、マルチサイトへの対応を施しました。
この記事が少しでも皆様のお役に立てば幸いです。
それでは良いNginxライフを♪

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?