
複数のアプリケーションをサブドメインを使ってサーバー1台で動かしてみる
新年のご挨拶
明けましておめでとうございます。
 2025年も、カサレアルをよろしくお願いいたします。
やりたいこと
複数のアプリケーションを1台のEC2で動かしたい!
 ということでnginxと格闘したお話です。
https://subdomain1.casareal.jp
 https://subdomain2.casareal.jp
 https://subdomain3.casareal.jp
 みたいな形でアプリケーションを起動してるんだけど、実体はEC2が1台です。
 環境は
 ・Amazon Linux 2023
 ・nginx 1.24.0
 ちなみにアプリは PHP8.3 です(全部)
やってみたこと(失敗1)
とりあえず nginx.confのserver ディレクティブを複数書いてみる
# subdomain1
    server {
        listen       80;
        listen       [::]:80;
        server_name  subdomain1.casareal.jp;
        root         /var/www/subdomain1/public;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
	location / {
            try_files $uri /index.php?$query_string;
        }
        location ~ \.php$ {
            root           /var/www/subdomain1/public;
            fastcgi_pass   unix:/var/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/subdomain1/public$fastcgi_script_name;
            include        fastcgi_params;
        }
   }
# subdomain2
    server {
        listen       80;
        listen       [::]:80;
        server_name  subdomain2.casareal.jp;
        root         /var/www/subdomain1/public;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
	location / {
            try_files $uri /index.php?$query_string;
        }
        location ~ \.php$ {
            root           /var/www/subdomain2/public;
            fastcgi_pass   unix:/var/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/subdomain2/public$fastcgi_script_name;
            include        fastcgi_params;
        }
   }
※subdomain3は省略
 nginxのサービスを再起動してみる・・・・・
“server” directive is not allowed here
serverディレクティブは複数書いたらだめらしい。
やってみたこと(失敗2)
server を複数書いちゃいけないのかということで、subドメインとってきて
 if文で分岐してみる。
server {
    server {
        listen       80;
        listen       [::]:80;
        server_name ~^(?<subdomain>.+).casareal.jp;
        if ($subdomain == "subdomain1") {
            root         /var/www/subdomain1/public;
        }
        if ($subdomain == "subdomain2") {
            root         /var/www/subdomain2/public;
        }
        if ($subdomain == "subdomain3") {
            root         /var/www/subdomain3/public;
        }
        try_files $uri $uri/ =404;
  }
}
なんか行けそう。
 そのままlocationも追記してみたところ。。。。
 “fastcgi_index” directive is not allowed here
 “fastcgi_param” directive is not allowed here
 むぅ。。。。どこにどれ書いちゃいけないとか1個にしなきゃいけないとか結構エラーが出たので却下
 ※ちなみに else if で記載したところ nginx.conf はelseなんてねぇ!って怒られました・・・
やってみたこと(成功)
ちょっと適当にやりすぎたのでちゃんと調べてみる
 serverディレクティブの上に
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
という記載があるので こっちに移せば動くのでは?
 ということで まず nginx.confのserverディレクティブを全て削除
 ここのファイルを作成し、/etc/nginx/conf.d/ 配下に以下を作成
# subdomain1
    server {
        listen       80;
        listen       [::]:80;
        server_name  subdomain1.casareal.jp;
        root         /var/www/subdomain1/public;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
	location / {
            try_files $uri /index.php?$query_string;
        }
        location ~ \.php$ {
            root           /var/www/subdomain1/public;
            fastcgi_pass   unix:/var/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/subdomain1/public$fastcgi_script_name;
            include        fastcgi_params;
        }
   }
/etc/nginx/conf.d/subdomain1.conf
 /etc/nginx/conf.d/subdomain2.conf
 /etc/nginx/conf.d/subdomain3.conf
 こんな感じで配置してやって、nginx再起動。
 無事やりたかったことができました。
ちゃんと調べてからやる
結論から言うと何とか動きました。
 nginx.confにserverディレクティブ複数書いちゃいけないのに、conf.d配下に置いたファイルだと
 ファイル内に複数あってもいけるのはなんでだろう?
 複数置いちゃいけないってよりなんか書き方が悪かったんでしょうかね・・・・
 今思うと最初から/etc/nginx/conf.d/ 配下に(失敗1)の部分をconfに切り出したらできたような気がしますね。
 でも、今の方がスッキリしてるので良いかと思っておきます。
 なんとなくで勝手に書いたりしちゃダメだなというのが教訓です。
 実際に動かすまではAWS環境だと、Route53とかロードバランサーとかを設定しないと動かないのですが
 とりあえずは一番ハマったのがNginxでした。
 nginx.confって 言語的には何でかかれているんだろうか・・・?