
パス内のIDを別のID(ドメインも)に書き換えるnginxリバースプロキシを設定しようとしています。例えば
https://somewhere.com/user/123/setting -->https://somewhere.else.com/user/456/setting
しかし、書き直しの部分が困難です。誰か助けてくれませんか?
map $old_user_id $new_project_id {
123 456;
999 9876;
12 56;
}
server {
location ~ ^/api/(?<old_project_id>.+)/store {
rewrite ^/api/[^/]+(/.*)/store ....... # how to rewrite with the map?
proxy_pass http://somewhere.else.com/;
}
}
答え1
正規表現は例のIDと一致しません。
以下を変更することで、問題を解決できます。
location ~ ^/api/(?<project_id>.+)/store {
rewrite ^/user/(.*)/setting /user/$project_id_2/setting break;
proxy_pass http://host.docker.internal:8080;
}