nginxのproxy_passの注意点

タグ: nginx / 公開: 2014-05-09

nginxをリバースプロキシにする場合に使用するproxy_passディレクティブは、URIが与えられた場合と、そうでない場合で挙動が異なる。

どういうことかというと、以下の1.2.は別々の結果となる。proxy_passディレクティブの引数に注目して欲しい。

# 1. specified with a URI
location /name/ {
    proxy_pass http://127.0.0.1/;
}
# 2. specified without a URI
location /name/ {
    proxy_pass http://127.0.0.1;
}

1.はproxy_passディレクティブに完全なURIを与えた例である。この場合http://example.com/name/fooへのアクセスは、/nameが削除されたhttp://127.0.0.1/fooへ転送される。

2.はproxy_passディレクティブに完全なURIを与えなかった例である。この場合http://example.com/name/fooへのアクセスは、そのままhttp://127.0.0.1/name/fooへ転送される。

この事は少々わかりにくいがnginxのドキュメントにも書かれている。

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:

サブドメインが使用できない環境で、location毎に接続先のWebサーバを切り替えたい場合など、一般的には1.の挙動の方が好ましいように思われる。

nginxでリバースプロキシを設定する場合には、自分がどちらのパターンのproxy_passを書いているのか注意が必要になる。

この記事をSNSでシェアする
タイトルとURLをコピーする
または投稿画面を開く
Author
Icon
ぺけみさお / xmisao
プログラマ。
Subscription
Recent articles
Related to nginx