LoginSignup
0
0

nginxでデプロイスロットをつくる

Posted at

まえがき

nginxでクラウドのようにデプロイスロットを構築できないかなぁ、と思って調べたのでその備忘録

前提

動かすのは以下の環境

  • Windows 11
  • nginx 1.26.1

スロット1と2をつくり、それぞれ以下のAPIサーバーにつなぐ想定

サーバー1: localhost:5000
サーバー2: localhost:5001

nginx.confを設定する、だけ

http {
    # スロット1のサーバーグループを定義
    upstream slot1 {
        server localhost:5000;
    }
    # スロット2のサーバーグループを定義
    upstream slot2 {
        server localhost:5001;
    }

    server {
        listen      localhost:80;
        server_name localhost;

        # 接続先切り替え用の変数(なくてもいい)
        set $slot slot1;

        location / {
            # 指定のスロットに転送
            proxy_pass http://$slot;
        }
    }
}

使い方

  1. $slot変数にslot1slot2を設定
    # 例:slot2に切り替える場合
    set $slot slot2 
    
  2. nginxの設定を読み込みなおす
    ./nginx -s reload
    

あとがき

以上、これだけとなります
設定ファイルをちょびっと書くだけでこういったことが実現できるのはすごい楽ですね✌️

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