docker - nginx无法将请求转发到在其他容器中运行的服务

标签 docker nginx containers microservices

我想在我的微服务架构中使用Nginx反向代理作为APIGateway

问题:Nginx无法将proxy_pass传递给在另一个容器中运行的payment_service。但是,当我尝试从curl payment_service:3000容器内部进行nginx时,它可以工作。这样网络就可以了。

docker-compose.yml

version: '3'

services:

 payment_service:
   container_name: payment_service
   build: ./payment
   ports:
    - "3000:3000"
   volumes:
     - ./payment:/usr/app
   networks:
     - microservice-network

 api_gateway:
   image: nginx:latest
   container_name: api_gateway
   restart: always
   volumes:
     - ./default.conf:/etc/nginx/conf.d/default.conf
   ports:
     - 8080:8080
     - 443:443
   depends_on:
     - payment_service
   networks:
     - microservice-network

networks:
  microservice-network:
  driver: bridge

default.conf
upstream payment_server {
  server payment_service:3000 max_fails=10;
}

server {
  listen 8080;

  location /api/v1/payment {
    proxy_pass http://payment_server;
  }
}


当我使用http://localhost:3000直接访问支付服务时,它运行良好
但不要使用http://localhost:8080/api/v1/payment

最佳答案

根据the docs

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI...



我不知道您的付款服务期待什么,但是我假设您正在尝试尝试根本方法。您需要在proxy_pass后面添加斜杠:
location /api/v1/payment {
    proxy_pass http://payment_server/;
}

否则,请求将作为
http://payment_service:3000/api/v1/payment

关于docker - nginx无法将请求转发到在其他容器中运行的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137531/

相关文章:

php - Laravel + 基本身份验证,除了一个文件夹不起作用

docker - Docker挂载点是如何决定的?

api - "Container is not defined"谷歌图表

sql-server - 复制 SQL Server 2016 数据库的内容

docker - 如果用户可以访问 docker 容器中的终端,他们可以做任何事情来破坏其所在的硬盘吗?

docker build with Ubuntu 18.04 image 在提示输入键盘的原产国后挂起

linux - 为什么 try_files 不强制立即响应 404?

java - 如何使用 docker 运行 shell 脚本和 java 应用程序?

ssl - 通过 Nginx Ingress Controller 和证书管理器启用 SSL 后,TTFB 增加了 200 多毫秒

node.js - 连接到容器内的 Amazon DynamoDB