【问题标题】:redirect all the requests to api server with nginx使用 nginx 将所有请求重定向到 api 服务器
【发布时间】:2020-04-18 23:22:03
【问题描述】:

我使用 Nginx 作为反向代理将 api 请求重定向到我的服务器。不幸的是,它无法正常工作

我想要实现的是将/api/v1/* 等所有请求代理到http://my-api-server/api/v1/*

这是我写的规则

location /api/v1/ {
   proxy_pass http://my-api-server/api/v1/
}

但它不起作用。有什么想法吗?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    试试

    location /api/v1/ {
       proxy_pass http://my-api-server
    }
    

    proxy_pass 指令中,如果您指定的URI 为/api/v1/,所有匹配的URI 将被替换为精确指定的/api/v1/ 而不是/api/v1/*

    【讨论】:

      【解决方案2】:

      需要添加proxy_redirect属性:

      location /api/v1 {
          proxy_redirect  http://my-api-server/  /api/v1;
          proxy_pass http://my-api-server;
      }
      

      【讨论】:

      • proxy_redirect " " 设置应在代理服务器响应的“Location”和“Refresh”标头字段中更改的文本。假设代理服务器返回头字段“Location: ht tp://localhost:8000/two/some/uri/”。指令proxy_redirect http://localhost:8000/two/ http://frontend/one/; 会将此字符串重写为“Location: ht tp://frontend/one/some/uri/”。替换字符串中可以省略服务器名称:proxy_redirect http://localhost:8000/two/ /;,然后插入主服务器的名称和端口,如果不是 80,则将插入。 " "
      猜你喜欢
      • 2016-09-18
      • 1970-01-01
      • 2016-12-16
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      相关资源
      最近更新 更多