【问题标题】:How to send request from react app inside nginx which is inside docker container to golang webservice which is inside docker container on same AWS EC2如何将请求从 docker 容器内的 nginx 内的 react 应用程序发送到同一 AWS EC2 上的 docker 容器内的 golang webservice
【发布时间】:2021-10-27 18:21:43
【问题描述】:

目前我在 AWS EC2 实例中有两个容器。其中一个是 React 应用程序。它使用 nginx。这个应用程序应该向另一个容器内的 golang Web 服务发送请求。它们都在同一个 EC2 实例中。当我打开浏览器并转到 EC2 实例的公共 IP 地址时,我可以看到我的 React 应用程序正在运行。当我尝试向 golang Web 服务发送请求时,它说“(失败)net::ERR_CONNECTION_REFUSED”。我能够在 EC2 中使用 curl 请求并接收响应。我如何在 React Request 上做同样的事情。

这是我的 axios 帖子

axios.post('http://localhost:9234/todos', { "name": todo, "completed": false },).then((res) => {
      console.log(res)
      if (res.data.todo.name === todo) {
        setTodos([...todos, todo]);
      }
    }).catch((err) => { console.log(err) });

这是我对 curl 的要求

curl -d '{"id":9,"name":"baeldung"}' -H 'Content-Type: application/json' http://localhost:9234/todos

感谢帮助

【问题讨论】:

  • 粘贴的代码是在服务器端还是在浏览器中运行?
  • 容器是否运行在同一个 docker 网络上?
  • axios 发布请求来自 react 应用。当我点击一个按钮时,它正在投射。带有 curl 的请求在 EC2 实例中
  • 容器在同一台机器上。但不同的容器。他们有不同的容器 ID
  • 因此,如果正确解释您的答案,则反应应用程序正在浏览器中运行。这意味着 react 应用程序会尝试连接到正在运行浏览器的计算机上的端口 9234。

标签: reactjs docker go nginx amazon-ec2


【解决方案1】:

正如@Daniel 所说,javascript 被提供给浏览器并在那里运行。因此,当您的浏览器请求地址localhost 时,它实际上意味着您计算机的本地主机。要访问 golang 服务器,您需要从 docker 容器中转发 9234 端口。

services:
  web:
    ports:
      - "9234:9234"

然后您还需要在您的 ec2 实例的防火墙中打开 9234 端口,然后您可以使用浏览器中的 ec2 的公共地址访问您的 go 服务器。

axios.post('http://{{public_address_of_Ec2}}:9234/todos', { "name": todo, "completed": false },).then((res) => {
      console.log(res)
      if (res.data.todo.name === todo) {
        setTodos([...todos, todo]);
      }
    }).catch((err) => { console.log(err) });

但不建议公开端口以访问您的服务器。您可以使用 nginx 监听您的 80 端口,然后将请求负载平衡到您的 go 服务器。这是一个 yaml,您可以在 docker-compose 中添加以使用 nigx:

nginx:
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on: 
      - web #your web app service name
    ports: 
      - "80:80"
    networks: 
      - "web.network" #your existing network name

nginx conf 应该是:

user nginx;
# can handle 1000 concurrent connections
events {
    worker_connections   1000;
}
# forwards http requests
http {
        # http server
        server {
              # listens the requests coming on port 80
              listen 80;
              access_log  off;
              # / means all the requests have to be forwarded to api service
              location / {
                # resolves the IP of api using Docker internal DNS
                proxy_pass http://web:9234;
              }
        }
}

【讨论】:

    【解决方案2】:

    从您的详细信息看来,以下一项或多项可能是正确的:

    • 您没有将端口 9234 转发到容器
    • 您没有在 EC2 实例的安全组中打开端口 9234

    此外,@Jaroslaw 指出的localhost 来自浏览器的视角。 localhost 也应该是解析到该 IP 的 ec2 实例或 dns 的 IP。

    需要明确的是,react webapp 不在 ec2 实例上运行。它的静态资产(例如 DOM 元素和 Javascript)被提供给浏览器并在那里运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多