Nginx反向代理映射到多个前端和后端



我有一个角前端和一个node.js后端,我想为不同国家的客户部署。我们需要每个国家都有一个前端。后端容器。所以这个想法是nginx应该根据路径重定向到docker组合环境中的正确容器:

example.com/us/frontend→前端集装箱

example.com/us/backend→Us后端容器

example.com/fr/frontend→前端集装箱

example.com/fr/backend→后端集装箱…

我已经实现了:

docker-compose.yml

version: "3"
services:
test-proxy:
container_name: test-proxy
image: nginx:latest
depends_on:
- example-frontend-us # References the service name.
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf
- ./content:/usr/share/nginx/html
ports:
- 80:80 # Publishs only port 80 outside of docker compose context.
example-frontend-us:
container_name: example-frontend-us
image: angular-frontend
ports:
- 8081:80

nginx.conf:

http {
server {
listen 80;
server_name localhost 127.0.0.1;
location = / {
root /usr/share/nginx/html;
try_files $uri /index.html;
}

location /us/frontend {
proxy_pass          http:/example-frontend-us:8081;
proxy_set_header    X-Forwarded-For $remote_addr;
}
}
}

如果我在localhost/us/frontend上打开浏览器,我得到一个502坏网关响应

如果我在localhost:8081上打开浏览器,前端正在工作

怎么了?我如何才能实现nginx路由到正确的容器?

在由docker compose创建的虚拟网络中,使用内部端口号。所以

proxy_pass          http:/example-frontend-us:8081;

应该

proxy_pass          http://example-frontend-us:80;

(在http://)

中还缺少一个正斜杠)最后,你不需要在example-front -us上映射端口,除非你想直接访问它而绕过nginx。

最新更新