nginxのリバースプロキシ一例
/post/:encryptedId?hoge=1111
-> /posts/:encryptedId?hoge=1111
location ~ ^/post/(?<encryptedId>[a-z1-9]+)/?$ {
proxy_pass http://localhost:3000/posts/$encryptedId$is_args$args;
}
tips
match対象を命名したい
(?<encryptedId>[a-z1-9]+)
のように宣言し、 $encryptedId
の変数で値を使用できる。
e.g.
/post/a1b2c3
の場合、
$encryptedId => a1b2c3
nginx location ~
正規表現でマッチした部分文字列を回収して使う方法 - Qiita
queryパラメーターの変数
$is_args$args
を使用する。
e.g.
~?hoge=1111
の場合、
$is_args => ?
$args => hoge=1111
How can query string parameters be forwarded through a proxy_pass with nginx? - Stack Overflow
URLも書き換えたい
location ~ ^/post/(?<encryptedId>[a-z1-9]+)/?$ {
return 301 https://$host/posts$is_args$args;
}