1
我尝试使用NGINX容器来托管静态Web应用程序。此容器还应将某些请求(即www.example.com/api/)重定向到同一网络上的另一个容器。Docker和NGINX - 在Docker构建时未在上游找到主机
在调用docker-compose构建时,我得到了“上游中找不到主机”的问题,尽管我强制要求NGINX容器是最后构建的。
我曾尝试以下解决方案:
- 强制网络名和别名(按Docker: proxy_pass to another container - nginx: host not found in upstream)
- 添加“解析”指令(按Docker Networking - nginx: [emerg] host not found in upstream等),既为8.8.8.8和127.0.0.11。
- 重写nginx.conf文件以在将重定向到它的位置之前或之后具有上游定义。
我在使用mobylinux虚拟机运行相关容器的Windows机器的Docker上运行。有什么我失踪?我不清楚“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'来验证配置你的构建?如果是这样,你不应该拥有它。这些只会在环境启动时才有用 –
您是否真的在编译时遇到错误? – whites11
@TarunLalwani不明确。我将Dockerfile添加到了帖子中。 –