我想在我的微服务架构中使用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/