Py学习  »  NGINX

nginx将与参数匹配的URL的所有请求重定向到子域

Ranknoodle • 7 年前 • 1127 次点击  

我有一个网站,也在为主域上的应用程序提供API请求。我想将所有匹配的/api请求发送到api子域。

例如,我想要 https://example.com/api + https://example.com/api/some_action + https://example.com/api/some_action?params1=somevalue&params2=value2 …重定向到相同的URL结构,但只在子域上。

因此,对于上面的示例:

https://example.com/api网站 -gt; https://api.example.com/api

https://example.com/api/some\u操作 -gt; https://api.example.com/api/some_action

https://example.com/api/some-action?参数1=someValue¶ms2=value2 … -gt; https://api.example.com/api/some_action?params1=somevalue&params2=value2 …

也适用于所有类型的请求(GET、POST等)。到目前为止,我在主域的server指令(在443 ssl server指令中)中尝试过这种方法。

location ~ /api(.*)$ {
  return 301 https://api.example.com/api/$request_uri$is_args$args;
}

执行简单GET请求时得到的结果 https://api.example.com/api/some_action?param1=value …是 https://api.example.com//some_action 没有参数,缺少API。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30750
文章 [ 1 ]  |  最新文章 7 年前
Richard Smith
Reply   •   1 楼
Richard Smith    7 年前

example.com/api/foo?bar api.example.com/api/foo?bar

location ^~ /api {
    return 307 https://api.exemple.com$request_uri;
}

$request_uri /api/

^~ location this document this link