简体   繁体   English

wget可以运行,但是无法从React应用请求

[英]wget works but can't request from React app

I have two linked Docker containers api and frontend . 我有两个链接的Docker容器apifrontend frontend is a ReactJS app inside nginx container. frontendnginx容器内的ReactJS应用。 This is the Dockerfile: 这是Dockerfile:

FROM nginx:alpine

COPY ./build /usr/share/nginx/html/

and the run command: 和运行命令:

docker run --name frontend --link api:api -p 80:80 frontend_img

When I sh inside frontend and run: 当我sh内部frontend和运行:

$ wget http://api:8080/api/users

works fine and I'm able to fetch the users. 工作正常,我能够获取用户。 However, when my app tries to fetch the data, I'm getting ERR_NAME_NOT_RESOLVED error in the Chrome console. 但是,当我的应用尝试获取数据时,我在Chrome控制台中收到ERR_NAME_NOT_RESOLVED错误。

This is my JS code: 这是我的JS代码:

fetchUsers() {
    return axios.get('http://api:8080/api/users')
      .then(response => response.data)
      .then(users => this.setState({ users }));
}

What am I missing? 我想念什么? Do I need to configure something in NGINX in order to be able to fetch the data from the api or something? 我是否需要在NGINX中配置某些内容以便能够从api或其他内容中获取数据?

Solved! 解决了! Thanks to this accepted answer docker nginx ERR_NAME_NOT_RESOLVED 感谢这个公认的答案, 码头工人nginx ERR_NAME_NOT_RESOLVED

When connecting to containers with a reverse proxy, all the URL's used by the application need to point to that reverse proxy and not the application. 当使用反向代理连接到容器时,应用程序使用的所有URL都必须指向该反向代理,而不是应用程序。 Typically you do this by giving a path in the URL without a hostname. 通常,您可以通过在URL中提供不带主机名的路径来实现。

First, I created a default.conf nginx file to add a location statement to redirect calls to my api container: 首先,我创建了default.conf nginx文件,以添加location声明以将调用重定向到我的api容器:

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

Then, in my webapp, I change my JS code so I request against /api/users , without hostname: 然后,在我的web应用程序中,更改我的JS代码,以便针对/api/users请求,而不使用主机名:

fetchUsers() {
    return axios.get('/api/users')
      .then(response => response.data)
      .then(users => this.setState({ users }));
}

Finally, in the Dockerfile, I replace default default.conf nginx file with the edited one: 最后,在Dockerfile中,我将默认的default.conf nginx文件替换为已编辑的文件:

FROM nginx:alpine
COPY default.conf /etc/nginx/conf.d
COPY ./build /usr/share/nginx/html/

That's all! 就这样!

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

相关问题 React 应用程序无法通过其请求在生产中访问我的快速应用程序,但在开发中运行良好 - React app can't reach my express app in production with its requests but works fine in development 无法从Google App Engine内部构建React应用 - Can't build React app from inside Google App Engine 无法从反应方法发送 POST 请求 - Can't send POST request from react method 无法从 axios POST 请求(反应)得到响应 - Can't get response from axios POST request (React) React 站点,尝试嵌入应用程序,但我不知道如何集成到组件中(适用于静态页面) - React site, trying to embed app, but I can't figure out how to integrate into a component (works on the static page) React Native + fetch + API:Delete请求在App中失败,在Postman中工作 - React Native + fetch + API : DELETE request fails in App, works in Postman React.js - 从 npm 开始运行时应用程序工作,但不会构建 - React.js - app works when running from npm start, but won't build 我无法在反应应用程序中显示来自 API 的项目 - I can't display the items from the API in react App 无法在 React App 中渲染从 API 获得的图像 - Can't render image got from API inside React App 无法使用React显示API请求的结果 - Can't display result of API request with React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM