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】動的にリクエストを振り分ける

Posted at

やりたいこと

以下のようにリクエストを振り分けたい

リクエスト先 プロキシ後のリクエスト先
http://sample.test.com:80/hello http://sample2.test.com:81/
http://sample.test.com:80/hello/goodbye http://sample2.test.com:81/goodbye

要はリクエストパスからhelloの部分を取り除いて別のサブドメインの別ポートにリクエストしたい

やり方

以下のようにmapを使う

    server {
        listen       80;
        listen       [::]:80;
        server_name _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        # 最後の”/"を忘れない!
        location /hello/ { 
            resolver    <your resolber address> valid=5s;

            # /hello以下の部分を取り出す
            set $new_uri $uri;
            if ($uri ~ ^/hello(/.*)$) {
              set $new_uri $1;
            }
            
            # 81の後に"/"は不要
            proxy_pass http://sample.test.com:81$new_uri;
        }

これでリクエストを送るとちゃんとプロキシ後のリクエスト先に届いている。

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?