简体   繁体   English

使用 Location 将 Nginx 重定向到特定的 Docker 容器

[英]Use Location to Redirect Nginx to Specific Docker Container

I have 3 .net core apis running in a docker containers orchestrated via docker compose behind a Nginx Reverse proxy.我有 3 个 .net 核心 api 在一个 docker 容器中运行,这些容器通过 docker compose 在 Nginx 反向代理后面进行编排。

I want to make it so that I can use 1 sub domain for all 3 apis but use different URI routes to redirect to each API respectively for example:我想这样做,以便我可以对所有 3 个 api 使用 1 个子域,但使用不同的 URI 路由分别重定向到每个 API,例如:

Docker Compose Service Names: API_A, API_B, API_C Docker Compose 服务名称:API_A、API_B、API_C

URL in Browser -> Resource on Server浏览器中的 URL -> 服务器上的资源

http://api.MyDomain.com/APIA/Swagger -> http://API_A:80/swagger http://api.MyDomain.com/APIA/Swagger -> http://API_A:80/swagger

http://api.MyDomain.com/APIB/Swagger -> http://API_B:80/swagger http://api.MyDomain.com/APIB/Swagger -> http://API_B:80/swagger

http://api.MyDomain.com/APIC/Swagger -> http://API_C:80/swagger http://api.MyDomain.com/APIC/Swagger -> http://API_C:80/swagger

http://api.MyDomain.com/APIA/api/resource?query="a" -> http://API_A:80/resource?query="a" http://api.MyDomain.com/APIA/api/resource?query="a" -> http://API_A:80/resource?query="a"

http://api.MyDomain.com/APIB/api/resource?query="a" -> http://API_B:80/resource?query="a" http://api.MyDomain.com/APIB/api/resource?query="a" -> http://API_B:80/resource?query="a"

http://api.MyDomain.com/APIC/api/resource?query="a" -> http://API_C:80/resource?query="a" http://api.MyDomain.com/APIC/api/resource?query="a" -> http://API_C:80/resource?query="a"

Up until this point I have been using NGINX proxy manager UI to do this, but I think it will require some kind of nginx configuration that uses location.到目前为止,我一直在使用 NGINX 代理管理器 UI 来执行此操作,但我认为它需要某种使用位置的 nginx 配置。

Could somebody give me an example nginx config that accomplishes the above task?有人可以给我一个完成上述任务的 nginx 配置示例吗?

I am not exactly sure what this operation is called.我不确定这个操作叫什么。 I would appreciate someone giving me the vocabulary that will allow me to better understand my needs and the nginx documentation.我会很感激有人给我词汇,让我更好地理解我的需求和 nginx 文档。

Thanks for any advice.感谢您的任何建议。

it seems what you want is request forwarding being done in reverse proxy nginx configuration.看来您想要的是在反向代理 nginx 配置中完成请求转发。

You need edit some lines in nginx.config.您需要在 nginx.config 中编辑一些行。

Here as per this config, your request sent at http://api.MyDomain.com/APIA/swagger will replace the location prefix matching the one present in config and forward it to http://API_A:80/swagger .根据此配置,您在http://api.MyDomain.com/APIA/swagger发送的请求将替换与配置中存在的位置前缀匹配的位置前缀,并将其转发到http://API_A:80/swagger

The configuration results in passing all requests processed in this location to the proxied server at the specified address.配置导致将在此位置处理的所有请求传递到指定地址的代理服务器。

  • Request to - http://{some ip/ domain}/APIA/xxxx请求到 - http://{some ip/ domain}/APIA/xxxx
  • Forwards to - http://API_A:80/xxxx specified address written in proxy_pass转发到http://API_A:80/xxxx指定地址写在proxy_pass

When the location prefix matching is done, the rest request is appended to proxy_pass address and rewritten in a form a new address.当位置前缀匹配完成后,rest请求被附加到proxy_pass地址并以新地址的形式重写。

server   { 
    listen 80;
    
    location  /APIA/api/ { 
      proxy_pass  http://API_A:80/;
    }
    location  /APIB/api/ { 
      proxy_pass  http://API_B:80/;
    }
    location  /APIC/api/ { 
      proxy_pass  http://API_C:80/;
    }
    location  /APIA/ { 
      proxy_pass  http://API_A:80/;
    }
    location  /APIB/ { 
      proxy_pass  http://API_B:80/;
    }
    location  /APIC/ { 
      proxy_pass  http://API_C:80/;
    }
    
  }

As per official doc - Nginx official doc根据官方文档 - Nginx 官方文档

Note that in the first example above, the address of the proxied server is followed by a URI, /link/.请注意,在上面的第一个示例中,代理服务器的地址后跟一个 URI,/link/。 If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter.如果 URI 与地址一起指定,它将替换请求 URI 中与位置参数匹配的部分。 For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html .例如,这里带有 /some/path/page.html URI 的请求将被代理到http://www.example.com/link/page.html If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).如果在没有 URI 的情况下指定地址,或者无法确定要替换的 URI 部分,则传递完整的请求 URI(可能已修改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM