
我正在嘗試設定一個 nginx 反向代理,以使用另一個 id(也是域)重寫路徑中的 id。例如
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;
}