【发布时间】:2017-09-25 00:48:16
【问题描述】:
我想在一台主机虚拟机上运行多个 docker 容器,这些容器只能通过一个域访问。我想使用请求 url 来区分容器。 为了实现这一点,我尝试将 nginx 服务器设置为反向代理,并在容器中运行它,同时监听端口 80。
假设我有两个容器在端口 3000 和 4000 上运行。 路由如下:
docker-host.example.com/3000 -> this will access container exposing port 3000
docker-host.example.com/4000 -> this will access container exposing port 4000
问题是即使尝试为此类反向代理定义静态规则,我目前也处于堆栈状态。 没有任何位置也能正常工作:
upstream application {
server <docker container>:3000;
}
server {
listen 80;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://application/;
}
}
但是当我添加端口位置并尝试使用 localhost 访问它时:{nginx port}/3000/
upstream application {
server <docker container>:3000;
}
server {
listen 80;
location /3000/ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://application/3000/;
}
}
似乎正确请求了第一个资源(主 html),但缺少任何其他依赖资源(例如该站点所需的 js 或 css)。 如果我检查日志中对这些资源的请求:
09:19:20 [error] 5#5: *1 open() "/etc/nginx/html/public/css/fonts.min.css" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /public/css/fonts.min.css HTTP/1.1", host: "localhost:8455", referrer:"http://localhost:8455/3000/"
所以请求网址是http://localhost:8455/public/css/fonts.min.css
而不是http://localhost:8455/3000/public/css/fonts.min.css
我可以请教您任何建议吗?这种情况可能吗?
【问题讨论】:
标签: nginx docker reverse-proxy