nginx 反向代理 docker 容器中的 2 个本地 lambda

nginx 反向代理 docker 容器中的 2 个本地 lambda

我有 2 个 lambda 函数包装在 docker 容器中,并使用 docker compose 将它们一起运行。一个在端口 9000 上运行,另一个在端口 9001 上运行。我尝试使用 nginx 让它们都可以通过本地的同一端口访问。我可以在 postman 中分别在各自的端口上访问每个 lambda

本地主机:9000/2015-03-31/函数/函数/调用

但不在 nginx 映射端口。

这是我的 default.conf


server {
    listen       80;
    server_name  localhost;

    location /user-api/ {
        proxy_pass   http://127.0.0.1:9000/2015-03-31/functions/function/invocations;
    }

    location /cart-api/ {
        proxy_pass   http://127.0.0.1:9001/2015-03-31/functions/function/invocations;
    }

}

这是我的 docker-compose.yml

version: "3.7"

services:
  user-api:
    build: ./app/lambdas/user-api
    ports:
      - 9000:8080
    env_file:
      - .env
  cart-api:
    build: ./app/lambdas/cart-api
    ports:
      - 9001:8080
    env_file:
      - .env
  nginx-proxy:
        depends_on:
            - user-api
            - cart-api
        image: nginx:alpine
        volumes: 
            - $PWD/default.conf:/etc/nginx/conf.d/default.conf
        networks:
            my-network-name:
                aliases:
                    - api-g-way
        ports:
            - 1234:80
  
networks:
    my-network-name:

这是我尝试访问其中一个时的终端输出

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/0
 ⠿ Container my-lambdas-cart-api-1     Created                                                                                         0.0s
 ⠿ Container my-lambdas-user-api-1     Created                                                                                         0.0s
 ⠿ Container my-lambdas-nginx-proxy-1  Created                                                                                         0.0s
Attaching to my-lambdas-cart-api-1, my-lambdas-nginx-proxy-1, my-lambdas-user-api-1
my-lambdas-cart-api-1     | 22 Oct 2022 19:20:35,282 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
my-lambdas-user-api-1     | 22 Oct 2022 19:20:35,301 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
my-lambdas-nginx-proxy-1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
my-lambdas-nginx-proxy-1  | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
my-lambdas-nginx-proxy-1  | /docker-entrypoint.sh: Configuration complete; ready for start up
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: using the "epoll" event method
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: nginx/1.23.2
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219) 
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: OS: Linux 5.10.124-linuxkit
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker processes
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker process 29
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker process 30
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker process 31
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker process 32
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:35 [notice] 1#1: start worker process 33
my-lambdas-nginx-proxy-1  | 172.19.0.1 - - [22/Oct/2022:19:20:40 +0000] "POST /user-api HTTP/1.1" 502 157 "-" "PostmanRuntime/7.29.2" "-"
my-lambdas-nginx-proxy-1  | 2022/10/22 19:20:40 [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XX.XX.XXX, server: localhost, request: "POST /user-api HTTP/1.1", upstream: "http://127.0.0.1:9000/2015-03-31/functions/function/invocations", host: "127.0.0.1:1234"

似乎命中了正确的上游地址,但我的连接被拒绝了。请记住这是我第一次使用 nginx。任何帮助都将不胜感激!

答案1

你做错了:每个容器都有自己的环回,这就是为什么你得到连接被拒绝

尝试这个:

server {
    listen 80;

    location /user-api/ {
        proxy_pass http://user-api:8080/2015-03-31/functions/function/invocations;
    }

    location /cart-api/ {
        proxy_pass http://cart-api:8080/2015-03-31/functions/function/invocations;
    }
}

您还需要熟悉容器中的名称服务。

相关内容