仅通过前端 ecs 容器访问后端 ecs 容器

我在项目中有一个限制后端容器访问的问题。 我的目标是让前端 ecs 容器对客户端开放,并使用 nginx 反向代理将任何带有“/api”路径的请求重定向到后端容器。但我不想允许除上述方案之外的任何访问我的后端。 我试图将后端容器定位到私有子网中并将其连接到前端,但它没有起作用。

BACKEND_ORIGIN 变量被存储在任务定义中作为键值对,稍后我将使用单独的服务来存储它。我试图在其中存储后端 IP。

我在我的 .conf nginx 文件中添加了以下部分。

server {
listen 8080;
server_name localhost;

set_by_lua_block $backend_origin {
  return os.getenv('BACKEND_ORIGIN')
}

location /api {
  proxy_pass $backend_origin;
  proxy_redirect off;
  add_header Access-Control-Allow-Origin *;
  proxy_http_version 1.1;
  proxy_buffering off;
}

location / {
  root /usr/share/openresty/html;
  index index.html index.htm;
  try_files $uri $uri/ /index.html;
}

原文链接 https://stackoverflow.com/questions/70696015

点赞
stackoverflow用户10558160
stackoverflow用户10558160

所以你在前端和后端都使用同一个域名?

我认为你可以最简单的方法是使用一个内部 ALB 来作为后端,并使用该 ALB 域名进行重定向。新的 ALB 只允许从前端流量。

2022-01-13 11:58:26