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