【发布时间】:2018-02-06 00:06:34
【问题描述】:
我正在尝试使用 NGINX 容器来托管静态 Web 应用程序。此容器还应将某些请求(即 www.example.com/api/)重定向到同一网络上的另一个容器。
我在调用 docker-compose build 时遇到了“在上游找不到主机”的问题,尽管我强制要求 NGINX 容器是最后一个构建的。
我尝试了以下解决方案:
- 强制使用网络名称和别名(根据Docker: proxy_pass to another container - nginx: host not found in upstream)
- 为 8.8.8.8 和 127.0.0.11 添加“解析器”指令(根据 Docker Networking - nginx: [emerg] host not found in upstream 等)。
- 重写 nginx.conf 文件以在将重定向到它的位置之前或之后具有上游定义。
我正在使用 mobylinux VM 运行相关容器的 Docker for Windows 机器上运行。有什么我想念的吗?对我来说,“http://webapi”地址应该正确解析并不明显,因为当您调用 docker-compose 时图像已构建但未运行。
nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream docker-webapi {
server webapi:80;
}
server {
listen 80;
server_name localhost;
location / {
root /wwwroot/;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://docker-webapi;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
码头工人撰写:
version: '3'
services:
webapi:
image: webapi
build:
context: ./src/Api/WebApi
dockerfile: Dockerfile
volumes:
- /etc/example/secrets/:/app/secrets/
ports:
- "61219:80"
model.api:
image: model.api
build:
context: ./src/Services/Model/Model.API
dockerfile: Dockerfile
volumes:
- /etc/example/secrets/:/app/secrets/
ports:
- "61218:80"
webapp:
image: webapp
build:
context: ./src/Web/WebApp/
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- webapi
Dockerfile:
FROM nginx
RUN mkdir /wwwroot
COPY nginx.conf /etc/nginx/nginx.conf
COPY wwwroot ./wwwroot/
EXPOSE 80
RUN service nginx start
【问题讨论】:
-
您有
nginx -t来验证您构建中的配置吗?如果是这样,你不应该拥有它。这些只有在环境启动时才有用 -
你真的在构建时得到那个错误吗????
-
@TarunLalwani 不,没有明确说明。我在帖子中添加了 Dockerfile。
-
@whites11 是的,在调用 docker-compose build 或 docker-compose up --build 命令后,会立即构建前两个镜像,而 webapp 镜像在最后一步失败。
-
你能发布
docker-compose build的输出吗?
标签: docker nginx docker-compose