1
1

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】POSTで受けたリクエストをGETでリダイレクトする

Posted at

Nginxでリバースプロキシサーバーを構築している際に、POSTで受けてGETでリダイレクトするような必要がありました。
その際にあまり情報がなく詰まったので記録として残します。

前提

以下のような設定イメージで、リクエストをhttpsで受けた後内部IPを使って別のサーバーにhttpでリダイレクトを行なっています。

nginx.conf
server {
    listen       443 ssl;
    server_name  example.com;

    proxy_set_header Host               $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP          $remote_addr;

    ssl_certificate /etc/nginx/example.com.crt;
    ssl_certificate_key /etc/nginx/example.com.key;

    # GET : https://example.com/sample/hoge?param1=123&param2=abc
    location ~^/sample/hoge$ {
          # GET : http://192.168.0.0.8080/sample/hoge?param1=123&param2=abc
          proxy_pass http://192.168.0.0:8080;
    }
}

解決策

以下のようにリダイレクトする際のmethodをGETに指定しつつパラメータを引き渡すことで実現できました。

nginx.conf
server {
    listen       443 ssl;
    server_name  example.com;

    proxy_set_header Host               $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP          $remote_addr;

    ssl_certificate /etc/nginx/example.com.crt;
    ssl_certificate_key /etc/nginx/example.com.key;

    # GET : https://example.com/sample/hoge?param1=123&param2=abc
    location ~^/sample/hoge$ {
          # GET : http://192.168.0.0.8080/sample/hoge?param1=123&param2=abc
          proxy_pass http://192.168.0.0:8080;
    }

    # POST : https://example.com/sample/fuga?param1=123&param2=abc
    location ~^/sample/fuga$ {
          # GET : http://192.168.0.0.8080/sample/fuga?param1=123&param2=abc
          proxy_method GET;
          proxy_pass http://192.168.0.0:8080$request_uri;
    }
}
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?