简体   繁体   English

使用 Nginx 容器通过 docker-compose 代理到 Elastic Beanstalk 上的 React 容器:这是一种有效的方法吗?

[英]Using an Nginx container to proxy to a React container on Elastic Beanstalk with docker-compose: is that a valid approach?

I'm hoping for some general feedback on my approach to trying to get a multi-container app up on Elastic Beanstalk via docker-compose (choosing "docker running on 64bit amazon linux 2" as the platform).我希望通过docker-compose (选择“docker running on 64bit amazon linux 2”作为平台)在 Elastic Beanstalk 上尝试获取多容器应用程序的方法得到一些一般性反馈。 Two main questions: `两个主要问题:`

  1. Am I right that Elastic Beanstalk listens on port 80 by default when we use docker-compose , and so we need to treat port 80 as the entry point to our application?当我们使用docker-compose时,Elastic Beanstalk 默认侦听端口80是否正确,因此我们需要将端口80视为我们应用程序的入口点? (reasons for thinking so below) (下面这么想的原因)

  2. I'm using an nginx container (listening on port 80 of the host machine) to route traffic to my front end container (which talks to a back end container).我正在使用 nginx 容器(在主机的端口80上侦听)将流量路由到我的前端容器(与后端容器通信)。 Is that approach approach to getting multiple containers running on EB valid?这种让多个容器在 EB 上运行的方法是否有效? Or is there some other much more common/straighforward way of doing this, with docker-compose ?或者是否有其他更常见/更直接的方式来执行此操作,使用docker-compose (Something like, "yes, that's a valid way to do this," or "no, you are going about this the totally wrong way, go in this other direction" would be helpful) (比如,“是的,这是执行此操作的有效方法”或“不,您正在以完全错误的方式进行此操作,另一个方向的 go”会有所帮助)

Why Elastic Beanstalk instead of ECS?为什么选择 Elastic Beanstalk 而不是 ECS? I'm just trying to understand the basic functionality of Elastic Beanstalk first.我只是想首先了解 Elastic Beanstalk 的基本功能。

More context:更多背景:

I have three containers: 1) a react front end, 2) an express back end, and 3) an nginx proxy.我有三个容器:1) 反应前端,2) 快速后端,以及 3) nginx 代理。 Why did I add nginx?为什么我加了nginx? Because it's my understanding that Elastic Beanstalk sets the proxy server configuration to none when you use docker-compose ( see here, under "Environments with Docker Compose" ), so what we have to do is get nginx to listen on the default point of entry for an Elastic Beanstalk app, port 80 , and then forward requests to other containers on the bridge.network created by docker-compose .因为据我了解,当您使用docker-compose时,Elastic Beanstalk 会将proxy server配置设置为none请参阅此处,在“具有 Docker Compose 的环境”下),所以我们要做的是获取 nginx 以侦听默认入口点Elastic Beanstalk 应用程序,端口80 ,然后将请求转发到docker-compose创建的 bridge.network 上的其他容器。 In other words, when you go to the link associated with an EB app, it will default to whatever is listening on port 80 .换句话说,当您 go 到与 EB 应用关联的链接时,它将默认为侦听端口80的任何内容。 And if that's an Nginx server, you will be routed according to the configuration of the Nginx server.如果那是 Nginx 服务器,您将根据 Nginx 服务器的配置进行路由。

I'll paste my docker-compose.yml file below, along with my nginx Dockerfile and its default.conf file.我将在下面粘贴我的docker-compose.yml文件,以及我的 nginx Dockerfile及其default.conf文件。 At this point, it mostly runs on my local machine (I can get to the front and back end directly on their respective ports), but I still get an error Invalid Host header when I go to localhost:80/ , where I should be proxied (by Nginx) to the React app homepage.此时,它主要在我的本地机器上运行(我可以直接在各自的端口上到达前端和后端),但是当我 go 到localhost:80/时,我仍然收到错误Invalid Host header ,我应该在哪里代理(由 Nginx)到 React 应用主页。 Any errors noticed would be helpful.发现的任何错误都会有所帮助。 But I'm not necessarily looking for a solution to that specific problem.但我不一定要寻找解决该特定问题的方法。 Rather, my main two questions are up above, looking for some confirmation/correction about my general approach.相反,我的主要两个问题在上面,希望对我的一般方法进行一些确认/更正。

Thank you!谢谢!

docker-compose.yml

version: "3"
services:
  nginx:
    image: mfatigati/shop-proxy
    ports:
      - "80:80"
    depends_on:
      - client
  server:
    image: mfatigati/shop-server-amd
    container_name: shop-server
    ports:
      - "4001:4000"
  client:
    image: mfatigati/shop-client-amd
    container_name: shop-client
    depends_on:
      - server
    ports:
      - "3001:3000"

Dockerfile for nginx Dockerfile为 nginx

FROM nginx

EXPOSE 80

RUN rm -r /usr/share/nginx/html*

COPY configs/default.conf /etc/nginx/conf.d/default.conf

CMD ["nginx", "-g", "daemon off;"]

default.conf for nginx nginx 的default.conf

upstream shop-client {
  server shop-client:3001;
}

server {
  listen 80;

  location / {
    proxy_pass http://shop-client;
  }
}

I think it might be fine.我觉得可能还好。 I have a similar approach on ecs.我在ecs上有类似的方法。 However, you will need to use nginx to serve as a reverse proxy otherwise you will run into CORS issues as soon as you run it anywhere else then on localhost.但是,您需要使用 nginx 作为反向代理,否则一旦在本地主机上的其他任何地方运行它,您就会遇到 CORS 问题。

On ecs you have options to run all containers in the same pod or in different ones.在 ecs 上,您可以选择在同一个 pod 或不同的 pod 中运行所有容器。 If you choose the latter (for better scalabilty, amongst other reasons), you will need to set up discovery service as well.如果您选择后者(为了更好的可扩展性,以及其他原因),您还需要设置发现服务。

On Beanstalk there might be options I do not know about, but at least using nginx should be fine.在 Beanstalk 上可能有一些我不知道的选项,但至少使用 nginx 应该没问题。

You can set beanstalk to different ports if you want, but for reverse proxy you will still need something, so I'd just leave it at 80.如果需要,您可以将 beantalk 设置为不同的端口,但对于反向代理,您仍然需要一些东西,所以我将其保留在 80。

EDIT: addition on reverse proxy: The problem to solve is CORS issues you encounter.编辑:添加反向代理:要解决的问题是您遇到的CORS问题。

The directive upstream is used for loadbalancing/routing. upstream指令用于负载平衡/路由。 (see this question and it's answers ) (请参阅此问题及其答案

To the general mechanism is to have several locations in your nginx conf that point specific requests to other servers.一般机制是在您的 nginx conf 中有几个locations将特定请求指向其他服务器。 This way the requests can be done to the same origin (domain, port, scheme), and then nginx will forward it to the servers who then can happily reply.这样就可以对origin (域、端口、方案)进行请求,然后 nginx 会将其转发给服务器,然后服务器可以愉快地回复。

Something like:就像是:

server {
    listen       80;
    listen  [::]:80;
    server_name  my-app.com;

    location /api/ {
      proxy_pass http://api-service:8080;
    }

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

You can have as many locations as necessary.您可以根据需要拥有任意多个位置。 This is very basic, and you might want to look into some other features, but this should be the basic setup.这是非常基本的,您可能想研究一些其他功能,但这应该是基本设置。

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

相关问题 如何使用 docker-compose 将容器部署到谷歌云? - How to deploy container using docker-compose to google cloud? 使用 docker-compose 在本地运行胶水容器 - run glue container locally using docker-compose 在 AWS Elastic Beanstalk 上的 Docker 容器中运行 GPU 加速应用程序 - Running GPU accelerated apps in a Docker container on AWS Elastic Beanstalk AWS Elastic Beanstalk 中的容器选项 - Container Options in AWS Elastic Beanstalk Docker-compose:如何使用相同的网络地址从容器和主机访问 Localstack 资源 - Docker-compose: How to access Localstack resources both from container and host, using same network address AWS Elastic Beanstalk Docker 未将默认的 nginx 反向代理设置为应用程序端口 - AWS Elastic Beanstalk Docker is not setting a default nginx reverse proxy to application port 在弹性 beanstalk 上部署多容器 docker 应用程序:找不到 webpack 命令 - Deploying a multi-container docker application on elastic beanstalk: webpack command not found 如何在 elastic beanstalk nginx 中禁用 nginx 代理缓冲区 - how to disable nginx proxy buffer in elastic beanstalk nginx Elastic Beanstalk Python 3.7 上的容器命令在 Django 中失败 - Container command fails in Django on Elastic Beanstalk Python 3.7 为什么 AWS 将停用支持多容器的 Elastic Beanstalk? - Why AWS is retiring Elastic Beanstalk with multi container support?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM