0
0

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-03-06

はじめに

Nginxを使用したロードバランシングをローカル環境で手軽に体感することを目的としておりますので、Nginxに初めて触れる方向けの内容となっております。

ロードバランシングとは

複数のサーバーに処理を分散することで、システムの負荷を均等化し、パフォーマンスや可用性を向上させる技術です

環境

  • Ubuntu 24.04
  • nginx-1.26.3
  • 推奨される設定状態
    • sites-availablesites-enabledディレクトリが存在する
    • /etc/nginx/sites-enabled/内の設定ファイルがNginxの設定に組み込まれている

下記のサイトで上記のような環境にする方法が記載されております。

手順1:index.htmlの作成

今回3つのバックエンドサーバ(localhost:5001localhost:5002localhost:5003)をグループ化して、リクエストをサーバに振り分けますので、3つディレクトリを用意してそれぞれにindex.htmlを準備します。

nginx_5001/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Nginx Site</title>
</head>
<body>
    <h1>5001</h1>
    <p>This is a sample page served by Nginx.</p>
</body>
</html>
nginx_5002/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Nginx Site</title>
</head>
<body>
    <h1>5002</h1>
    <p>This is a sample page served by Nginx.</p>
</body>
</html>
nginx_5003/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Nginx Site</title>
</head>
<body>
    <h1>5003</h1>
    <p>This is a sample page served by Nginx.</p>
</body>
</html>

手順2:ロードバランシング設定ファイルの編集

/etc/nginx/nginx.confに下記を追加します。

/etc/nginx/nginx.conf
http {
    upstream backend {
        server localhost:5001;
        server localhost:5002;
        server localhost:5003;
    }
}
  • upstream backend { }
    • backendという名前の負荷分散グループを作成します
    • Nginx はリクエストを受け取ると、backend に登録されたサーバーのいずれかにリクエストを転送します
  • server localhost:〇〇;
    • ローカルホストのポート番号〇〇で動作するバックエンドサーバです

loadbalancerport_3は新規でファイルを作成し、記述します。

/etc/nginx/sites-available/loadbalancer
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  • listen 80;

    • ポート番号80でリクエストを受け付けます(HTTP のデフォルトポート)
  • server_name localhost;

    • http://localhost へのアクセスに対して、この設定が適用されます
  • location / {}

    • ルート(/)にアクセスがあった場合の処理を指定します
  • proxy_pass http://backend;

    • リクエストをbackendという名前のWebサーバーに転送します
  • proxy_set_header Host $http_host;

    • 元のホストヘッダーをbackendという名前のWebサーバーに転送します
  • proxy_set_header X-Real-IP $remote_addr;

    • クライアントの実際のIPをbackendという名前のWebサーバーに転送します
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    • クライアントIPをX-Forwarded-Forヘッダーに追加します
  • proxy_set_header X-Forwarded-Proto $scheme;

    • クライアントが使ったプロトコル(HTTP/HTTPS)をbackendという名前のWebサーバーに転送します
/etc/nginx/sites-available/port_3
server {
        listen 5001;
        server_name localhost;

        # リクエストされた際にドキュメントがある場所
        root /path/to/nginx_5001;
        index index.html;
}

server {
        listen 5002;
        server_name localhost;

        # リクエストされた際にドキュメントがある場所
        root /path/to/nginx_5002;
        index index.html;
}

server {
        listen 5003;
        server_name localhost;

        # リクエストされた際にドキュメントがある場所
        root /path/to/nginx_5003;
        index index.html;
}

手順3:sites-enabled/にシンボリックリンクを作成

sudo ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/port_3 /etc/nginx/sites-enabled/

手順4:設定の反映

sudo systemctl reload nginx

http://localhost:80にアクセスしていただき、3回リロードを行いますと、順番にhttp://localhost:5001http://localhost:5002http://localhost:5003に表示されているページの表示ができました。
image.png

image.png

image.png

まとめ

以上で、ロードバランシングの設定ができました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?