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

huangapple go评论87阅读模式
英文:

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

问题

目前我在AWS EC2实例中有两个容器。其中一个是React应用程序,它使用nginx。该应用程序应该向另一个容器中的golang web服务发送请求。它们都在同一个EC2实例中。当我打开浏览器并访问EC2实例的公共IP地址时,我能够看到我的React应用程序正在工作。当我尝试向golang web服务发送请求时,它显示"(failed)net::ERR_CONNECTION_REFUSED"。我能够在EC2内部使用curl请求并接收响应。如何在React请求中做到这一点。

这是我的axios post请求:

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

感谢帮助!

英文:

Currently I have two container inside AWS EC2 instance.One of them is React app. It uses nginx. This app should send request to golang web service which is inside another container. They are both in same EC2 instance. When i open browser and go to EC2 instance's public IP address i am able to see my React app is working. When i try to send request to golang web service it says that "(failed)net::ERR_CONNECTION_REFUSED". I am able to use curl request inside EC2 and receive response. How can i do the same on React Request.

Here is the my axios post

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) });

Here is my request with curl

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

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
Thanks for helps

答案1

得分: 1

根据你的详细信息,有可能以下一个或多个情况是真实的:

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

此外,正如@Jaroslaw指出的那样,localhost是从浏览器的角度来看的。localhost也应该是EC2实例的IP地址或解析为该IP地址的DNS。

需要明确的是,React Web应用程序不在EC2实例上运行。它的静态资源,如DOM元素和JavaScript,会被提供给浏览器并在浏览器中运行。

英文:

from your details it seems likely that one or more of the following is true:

  • you do not have port 9234 forwarded to the container
  • you do not have port 9234 open in the EC2 instance's security group

Furthermore, localhost as @Jaroslaw points out is from the persective of the browser. That localhost should also be the IP of the ec2 instance or dns that resolves to that IP.

To be clear, the react webapp doesn't run on the ec2 instance. Its static assets such as DOM elements and Javascript get served to the browser and it runs there.

答案2

得分: 1

如@Daniel所说,JavaScript会被提供给浏览器并在浏览器中运行。所以当你的浏览器请求地址localhost时,实际上是指你的计算机的本地地址。要访问golang服务器,你需要将docker容器中的9234端口转发出来。

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

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

axios.post('http://{{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服务器。以下是一个可以添加到你的docker-compose中使用nginx的yaml示例:

nginx:
    image: nginx:latest
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on: 
      - web #你的web应用服务名称
    ports: 
      - "80:80"
    networks: 
      - "web.network" #你现有的网络名称

nginx配置文件应为:

user nginx;
# 可以处理1000个并发连接
events {
    worker_connections   1000;
}
# 转发http请求
http {
        # http服务器
        server {
              # 监听来自端口80的请求
              listen 80;
              access_log  off;
              # /表示所有请求都要转发到api服务
              location / {
                # 使用Docker内部DNS解析api的IP
                proxy_pass http://web:9234;
              }
        }
}
英文:

As @Daniel said javascript gets served to the browser and it runs there. So when your browser requesting the address localhost it actually means your computer's localhost. To access the golang server you need to forward the 9234 port from the docker container.

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

And then also you need to open the 9234 port in firewall of your ec2 instance then you can access your go server using the public address of your ec2 from your browser.

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) });

But this is not recommended to expose the ports to access your server. You may use nginx to listen on your port 80 and then load balance the requests to your go server. Here is a yaml you can add in your docker-compose to use 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

And the nginx conf should be:

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;
              }
        }
}

huangapple
  • 本文由 发表于 2021年8月28日 05:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/68959705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定