# NGINX 設定ティップス

**URL:** https://forum.ficusonline.com/t/topic/333
**Category:** Server
**Created:** [2019 年 11 月 1 日午前 8:00 UTC](https://forum.ficusonline.com/t/topic/333 "2019-11-01T08:00:56Z")
**Posts on this page:** 1
**Showing post:** 20

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 11 月 18 日午後 12:03 UTC](https://forum.ficusonline.com/t/topic/333/20 "2022-11-18T12:03:37Z")

</div>

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

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

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

> <https://serverfault.com/questions/396595/nginx-proxy-domain-to-another-domain-with-no-change-url>

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

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

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

```auto
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の引継ぎ

> **[Forwarding Visitor's Real-IP + Nginx Proxy/Fastcgi backend correctly](https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip/)**
>
> Fix real-ip for backend fastcgi applications using fastcgi headers method. Also it covers HTTPRealIP module based solution where nginx is built with it.

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

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

### Module ngx\_http\_realip\_module

> **[Module ngx\_http\_realip\_module](https://nginx.org/en/docs/http/ngx_http_realip_module.html)**

`/etc/nginx/nginx.conf`

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

```

> <https://serverfault.com/questions/314574/nginx-real-ip-header-and-x-forwarded-for-seems-wrong>

---

_[View the full topic](https://forum.ficusonline.com/t/topic/333)._
