Docker容器运行nginx,提供静态文件和反向代理



我正试图将FE(angularjs(与BE(nodejs(分离,这样UI将从不同的容器作为后端提供。设置非常简单,但我对NGINX绝对是个新手,尽管我浏览了很多帖子并尝试了几种配置,但我并没有达到预期的效果。

当我启动容器并在浏览器中点击localhost时,它开始加载index.html,但实际上要加载完整的UI,首先要等待来自后端的配置响应。对配置后端的API调用为http://localhost/configuration并且这将在http状态代码502上失败坏网关:

192.168.192.1 - - [12/Jun/2021:21:18:42 +0000] "GET /configuration HTTP/1.1" 502 559 "http://localhost/" 

我不知道如何使它工作。下面列出了有关设置的一些详细信息。

Three containers

  • 研究生
  • 后端(nodejs(
  • 前端(nginx提供静态文件+同时充当反向代理(

docker-compose.yml

version: '1'
services:
backend:
build: .
depends_on:
- postgres
ports:
- '8085:80'
postgres:
image: postgres
ports:
- '35432:5432'
volumes:
- ./core/db/initdb.sql:/docker-entrypoint-initdb.d/init.sql
environment:
POSTGRES_PASSWORD: ****redacted**** 
frontend:
build:
context: .
dockerfile: Dockerfile_UI
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- '80:80'
restart: always

NGINX config

server {
listen       80;
server_name  localhost;
location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
}
location /configuration {
proxy_pass http://app:8085/;
}
location /cm/2 {
proxy_pass http://app:8085/;
}
}

示例​对后端的API调用:

  • http://localhost:8085/cm/2/purpose
  • http://localhost:8085/configuration
location /location/ {
rewrite ^/location/(.*) /$1 break;
proxy_pass http://localhost:5008;
}

我希望这个脚本有帮助,默认路径">可以像您所写的那样正常工作,但对于特定路径,如"strong>"/api"或"/位置">您需要将rewrite关键字与正则表达式一起使用,以便前一个路径保持不变"http://localhost/">和随后的匹配路径"strong>"的转发路径/位置/某个路径"将写入默认路径。

最新更新