nginxでさくっとマルチサイト構築

単一サーバーや単一IPアドレスでも複数のドメインとサイトを立ち上げたいと思いnginxを使ってURLでルーティングさせる設定をしました。利用OSはUbuntu 18.04LTSです。

docker composeのインストール

#apt install docker-compose

ymlを編集

#vi docker-compose.yml
version: '3.1'

services:

  lb:
    image: nginx
    restart: always
    ports:
      - 80:80

一度立ち上げて動作を確認してみましょう。
# docker-compose up

nginxの設定をローカルにコピーしてコンテナにリンクします。

#docker cp コンテナ名:/etc/nginx/nginx.conf ./config/lb/nginx.conf
#docker cp コンテナ名:/etc/nginx/conf.d/default.conf ./config/lb/conf.d/default.conf

nginxの設定はサイトごとにコンフィグを作るだけです。default.confをコピーして2か所を編集するのみ。

#cp ./config/lb/conf.d/default.conf ./config/lb/conf.d/siteA.conf
#vi ./config/lb/conf.d/siteA.conf
server {
    listen       80;
    server_name  siteA.dns.com; #変更1 URL叩いた時のアドレスを記載

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://192.168.1.10:8080; #変更2 server_name叩かれたときに実際に呼ばれるアドレス 同PC内でもポートを変えればOK
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

ymlを編集しておきましょう。

#vi docker-compose.yml
version: '3.1'

services:

  lb:
    image: nginx
    restart: always
    ports:
      - 80:80
    volumes:
      - ./config/lb/nginx.conf:/etc/nginx/nginx.conf:ro #変更ないのでマウントいらないかも
      - ./config/lb/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro #変更ないのでマウントいらないかも
      - ./config/lb/conf.d/siteA.conf:/etc/nginx/conf.d/siteA.conf:ro
      - ./config/lb/conf.d/siteB.conf:/etc/nginx/conf.d/siteB.conf:ro

# docker-compose up
で立ち上げ完了

投稿を作成しました 112

nginxでさくっとマルチサイト構築” に1件のコメント

コメントを残す

関連する投稿

検索語を上に入力し、 Enter キーを押して検索します。キャンセルするには ESC を押してください。

トップに戻る
%d人のブロガーが「いいね」をつけました。