使用 nginx 进行反向代理 - 使用 docker run 将多个用户端口路由到单个容器端口(需要 nginx.conf 文件配置)

问题描述 投票:0回答:0

这是我关于堆栈溢出的第一个问题.. 所以,基本上我有一个存储库,其中有多个 API 的 JSON 文件,比如说 Offers 和 Continuity。 我创建了 Offers.Dockerfile 和 Continuity.Dockerfile (下面发布了 Offers.Dockerfile 内容) 所以,我使用

docker build -f Offers.Dockerfile -t offers-apis-sandbox  .
创建了 docker 镜像 然后我使用
brew install nginx
安装了 nginx 并在mac上使用
sudo nano /usr/local/etc/nginx/nginx.conf
配置nginx.conf(附上文件) 保存并重新加载 nginx
sudo nginx -s reload
然后我跑了
docker run -p 8081:8081 continuity-apis-sandbox
docker run -p 8080:8081 offers-apis-sandbox   
两者都启动了棱镜并运行.. 为了连续性 `[7:20:22 PM] › [CLI] ...等待启动棱镜... [7:20:22 PM] › [CLI] ℹ 信息获取 http://127.0.0.1:4010/continuity/programs?a=voluptas&t=corrupti [7:20:22 PM] › [CLI] ℹ 信息获取 http://127.0.0.1:4010/continuity/program/balance?a=simiique&t=laudantium [7:20:22 PM] › [CLI] ℹ 信息发布 http://127.0.0.1:4010/continuity/program/optin?a=odio&t=est [7:20:22 PM] › [CLI] ▶ 开始 Prism 正在监听 http://127.0.0.1:4010

I tested
GET http://127.0.0.1:4010/continuity/programs?a=voluptas&t=corrupti`在邮递员上但收到错误消息

Could not send request
Error: connect ECONNREFUSED 127.0.0.1:4010

我想在单个端口 8081 上路由 8080 和 8081。 如果我错了,请纠正我,这是我第一次尝试做某事......

这是 Offers.Dockerfile

# Use the official Node.js LTS (Long Term Support) image
FROM node:14-slim

# Set working directory inside the container
WORKDIR /app

# Install Prism globally
RUN npm install -g @stoplight/prism-cli

# Copy your API JSON files to the container
#COPY ./reference/Offers/Offers-APIs.v1.json /app/Offers-APIs.v1.json
COPY . /app
# Expose the port that Prism will listen on
EXPOSE 8081

# Command to run Prism and serve your API JSON file
CMD ["prism", "mock", "/app/reference/Offers/Offers-APIs.v1.json", "-d"]

这是 nginx.conf 文件

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;

    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name localhost;

        location / {
            root html;
        }
    }

    # Reverse Proxy Configurations for APIs
    server {
        listen 80;
        server_name api-offers.example.com;

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

    server {
        listen 80;
        server_name api-continuity.example.com;

        location / {
            proxy_pass http://localhost:8081;
        }
    }
}
dockerfile prism nginx-reverse-proxy json-api stoplight
© www.soinside.com 2019 - 2024. All rights reserved.