这应该是非常简单的,但看起来像我缺少一些非常基本的东西,任何帮助将不胜感激。在本地 - Nginx容器上的React应用代理到Node Express容器
我在本地MacBook上运行POC。基本思想是使用create-react-app
build
脚本构建React应用程序,并将其部署到本地的nginx-alpine
容器中。我能够做到这一点。我能够访问index.html并查看当客户端容器正在运行时应该看到的内容。
我有一个node-express
后端运行在同一台本地机器上,该机器使用node
码头图像站起来。我可以进行设置,并且可以通过浏览器访问这些图像的端点。
问题是当我的api
反应称这些表达端点。我已在我的nginx.conf
中设置了proxy_pass
,并尝试了多种方式进行设置。它没有工作。我尝试过使用localhost
,使用127.0.0.1
累,尝试使用码头集装箱的IP地址,尝试使用docker service name
,尝试在我的nginx.conf
中将其设置为upstream
,尝试直接在proxy_pass
中使用后端URL。什么都没有
这里是代码...
http://localhost/
- >加载UI
http://localhost:3001/features
- >返回一个JSON有效载荷我需要在UI
http://localhost/admin
- >是一个组件在我的UI上使用fetch
API加载路径/features
。
nginx.conf
upstream app_servers {
server localhost:3001;
}
# Configuration for the server
server {
# Running port
listen 80;
root /usr/share/nginx/html;
location/{
try_files $uri /index.html;
}
# Proxying the connections connections
location /features {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
有什么明显的错误在我的配置文件?
使用此docker-compose
启动后端API,我不相信这是一个问题,因为我可以在浏览器中使用端点。但在这里它是无辜的。
version: '2'
services:
thing1:
image: toggler-backend
container_name: thing1
volumes:
- ./backend:/src/backend
ports:
- "3001:3000"
的UI的Dockerfile
是这样的......
FROM nginx:alpine
COPY nginx.conf /etc/nginx
COPY build /usr/share/nginx/html
我先从docker run
命令UI现在。
docker run -it -p 80:80 toggler-client
谢谢塔伦。我会尝试并让你知道。我有点预感到它与你提到的有关。我不知道的是解决方案。你给了我选择。我会尽快尝试。谢谢。 –