我有一个网站,该网站也向主域上的应用程序提供api请求。我想将所有匹配的/ api请求发送到api子域。
例如,我希望https://example.com/api + https://example.com/api/some_action + https://example.com/api/some_action?params1=somevalue¶ms2=value2 ....重定向到相同的url结构,但仅在子域上。
对于上述示例:
https://example.com/api -> https://api.example.com/api
https://example.com/api/some_action -> https://api.example.com/api/some_action
https://example.com/api/some_action?params1=somevalue¶ms2=value2 .... -> https://api.example.com/api/some_action?params1=somevalue¶ms2=value2 ....
也适用于所有类型的请求(获取,发布等)。到目前为止,我已经在主域的服务器指令(在443 SSL服务器指令中)中尝试过此操作
location ~ /api(.*)$ {
return 301 https://api.example.com/api/$request_uri$is_args$args;
}
在https://api.example.com/api/some_action?param1=value ...上执行简单的GET请求时,我得到的结果是https://api.example.com//some_action,没有参数并且缺少api。
答案 0 :(得分:0)
To redirect example.com/api/foo?bar
to api.example.com/api/foo?bar
you should use:
location ^~ /api {
return 307 https://api.exemple.com$request_uri;
}
The $request_uri
variable contains the original request, including the /api/
prefix and the query sring.
The ^~
operator gives this location
precedence (see this document for details). The 307 status code maintains GET/POST through the redirection (see this link for more).