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

More than 1 year has passed since last update.

Nginx③リバースプロキシの設定

1
Last updated at Posted at 2025-03-03

はじめに

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

リバースプロキシとは

クライアントのリクエストを受け取り、内部のサーバに転送する役割を持つサーバです

環境

  • Ubuntu 24.04
  • nginx-1.26.3
  • sites-availablesites-enabledディレクトリがある状態
  • /etc/nginx/sites-enabled/内にあるすべての設定ファイルをNginxの設定に組み込むことができている状態
  • 80番ポートでWebサーバが動作している状態
    • 80番ポートでなくても大丈夫ですが、どこかのポートでWebサーバが動作している状態にしてください

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

リバースプロキシの設定方法

  1. リバースプロキシの設定
  2. sites-enabled/にシンボリックリンクを作成
  3. 設定の反映

手順1:リバースプロキシの設定

sudo vim /etc/nginx/sites-available/server.conf
/etc/nginx/sites-available/server.conf
server {
        listen       8080;
        server_name  localhost;

        location / {
                # Webサーバが動作しているポート番号の指定
                proxy_pass http://localhost:80;
                proxy_set_header Host $http_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;
        }
}
  • proxy_pass http://localhost:80;
    • リクエストを80番ポートのWebサーバーに転送
  • proxy_set_header Host $http_host;
    • 元のホストヘッダーを80番ポートのWebサーバーに転送
  • proxy_set_header X-Real-IP $remote_addr;
    • クライアントの実際のIPを80番ポートの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)を80番ポートのWebサーバーに転送

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

sudo ln -s /etc/nginx/sites-available/server.conf /etc/nginx/sites-enabled/

手順3:設定の反映

sudo systemctl reload nginx

http://localhost:8080にアクセスすると、http://localhost:80に表示されているページの表示ができました。

まとめ

以上で、リバースプロキシの設定ができました。

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