NGINX 設定ティップス

外部サーバへのリバースプロキシ設定

ドメイン名でのみでアクセス許可した外部 Nginx サーバ(バックエンド)へのプロキシ(フロントエンド)設定。

簡単に言うとフロントエンドで受けたドメイン名をバックエンドへ引継ぐための設定です。

リバースプロキシ側(フロントエンド)で、下記 proxy_set_header で指定したヘッダー情報を外部サーバ(バックエンド)へ受け渡します。

Note) ホスト名を明示 proxy_set_header Host test.example.com; しないとプロキシパスのアドレス(とポート)が受け渡されてしまいます。

/etc/nginx/conf.d/default.conf

server {
    server_name test.example.com;

    location / {
        proxy_pass http://12.34.56.78:8888;
        proxy_set_header Host test.example.com;
        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;

    }
.....

外部サーバでクライアントIPの引継ぎ

フロントエンドで受けたクライアントIPを外部サーバ(バックエンド)で引継ぐ場合は、下記バックエンドの Nginx の設定でフロントエンドサーバのIPアドレス(グローバルIP)を明示して下さい。

設定する上で、以下のモジュールが必要です。$ nginx -V コマンドでコンパイル条件を確認して下さい。

Module ngx_http_realip_module

https://nginx.org/en/docs/http/ngx_http_realip_module.html

/etc/nginx/nginx.conf

http {
....
    set_real_ip_from   xx.xx.xx.xx;   ← フロントエンドのIPアドレスを入力
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;
.....
}