【发布时间】:2020-11-08 02:47:46
【问题描述】:
目标
我的目标是在同一个 nginx 服务器上设置多个公开的后端 api 容器:
- http://localhost:80/api/account -> 调用http://account-service:9000/
- http://localhost:80/api/cart -> 调用http://cart-service:9000/
- http://localhost:80/api/order -> 调用http://order-service:9000/
- http://localhost:80/api/product -> 调用http://product-service:9000/
- ...
我的后端容器基于 php:7.2-fpm (托管在每个 apache 容器上的 symfony)并且它们没有任何名为 api/${NAME_SERVICE} 的路由(我不想创建一些我的后端中无用的父路由)。
所以,我的要求很简单,例如,当我打电话时,我希望这样:
- http://localhost:80/api/account/profile
我的后端 帐户容器 提供此 uri:
- http://account-service:9000/profile
到目前为止我尝试了什么
-
rewrite(对 fastcgi 参数没有帮助) - 使用
proxy_pass设置upstream服务器 - 调整
fastcgi_param REQUEST_URI(没有任何成功) -
alias(禁止访问)
会议
这是我的 nginx.conf :
...
server {
server_name ~.*;
client_max_body_size 50m;
location / {
try_files $uri /index.php$is_args$args;
}
# work if i want to serve account-service on http://localhost:80/
# location ~ ^/index\.php(/|$) {
# fastcgi_pass account-service:9000;
# fastcgi_buffers 16 16k;
# fastcgi_buffer_size 32k;
# fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
# include fastcgi_params;
# }
# my last attempt with alias
location ~* ^/api/account {
alias /;
index index.php;
location ~ index\.php(/|$) {
fastcgi_pass account-service:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
}
...
docker-compose.yml:
nginx:
image: nginx:1.15.3-alpine
restart: on-failure
volumes:
- "./build/nginx/default.conf:/etc/nginx/nginx.conf:ro"
- "./logs/nginx:/var/log/nginx"
ports:
- "80:80"
depends_on:
- account-service
- account-db
- cart-service
- cart-db
- order-service
- order-db
- product-service
- product-db
account-service:
env_file:
- config/account.env
build: apps/account-service
restart: on-failure
expose:
- "9000"
volumes:
- "./apps/account-service:/usr/src/app"
depends_on:
- account-db
cart-service:
...
PS: 我知道您可以将 nginx conf 拆分为多个服务器块,以侦听不同的端口/主机名,但这不是我想要在这里实现的。
【问题讨论】:
标签: docker nginx nginx-location nginx-config