【发布时间】:2019-05-12 17:15:39
【问题描述】:
第一次使用 docker-compose。尝试将 Nginx 容器设置为 Web 服务器和包含我的 dotnetcore 应用程序的容器。目的是让 nginx 将调用传递给 Kestrel。两个镜像都构建并运行,但在访问“http://localhost:8080”时出错:
proxy_1 | 2019/05/12 16:39:45 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://172.19.0.2:4000/", host: "localhost:8080"
项目结构如下:
- Dockersingleproject
- Dockersingleproject/(dotnetcore 应用程序)
- *应用文件'
- DockerFile
- Nginx/
- nginx.conf
- DockerFile
- docker-compose.yml
- Dockersingleproject/(dotnetcore 应用程序)
我的印象是问题在于 Web 服务器容器和应用程序容器之间的连接拒绝,但我不知道为什么。下面是应用的 Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /Dockersingleproject
COPY bin/Debug/netcoreapp2.2/publish .
ENV ASPNETCORE_URLS http://+:4000
EXPOSE 4000
ENTRYPOINT ["dotnet", "Dockersingleproject.dll"]
应用程序 docker 文件暴露了 4000 端口。nginx.conf:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-nginx {
server app:4000;
}
server {
listen 8080;
location / {
proxy_pass http://docker-nginx;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
proxy_buffers 8 16k; # Buffer pool = 8 buffers of 16k
proxy_buffer_size 16k; # 16k of buffers from pool used for headers
}
}
}
服务器正在侦听端口 8080,并将请求代理到服务器“app”上的端口 4000。在 docker-compose 文件中定义:
version: '2'
services:
app:
build:
context: ./Dockersingleproject
dockerfile: Dockerfile
ports:
- "4000:4000"
proxy:
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- "8080:8080"
links:
- app
应用服务将端口 4000 请求映射到 4000,在我看来这应该可以工作。
nginx容器的IP为:172.19.0.3 应用容器的IP为:172.19.0.2
请让我知道我的困惑所在。我正要指责我的电脑是问题所在。任何信息表示赞赏。
访问站点时拒绝连接导致 nginx 502 网关错误
【问题讨论】:
-
嘿,我试图重现您的问题,但在尝试提取 dotnet 图像时出现错误 503。我错过了什么吗?
-
@EfratLevitan 您好,dotnet 基础映像语句是由 Visual Studio 在添加 Docker 支持时生成的。不知道为什么会收到 503 抱歉。
-
您在主机的
:4000上暴露了app(您可能不应该这样做),但是因为您是这样,所以您可以通过 curl 确认 .NET 容器正常工作从主机获取端点 (curl --request GET http://localhost:4000)。如果可行,那么问题出在您的 Nginx 配置中。 -
因为您打算让 Nginx 代理您的 .NET 容器,所以您不需要将 .NET 暴露给主机。避免这种情况的一种方法是只使用
ports: - ":4000" but it's better to useexpose',因为这不会暴露任何主机点。在其他反馈中,docker-compose现在位于version: 3并且使用3您不需要links: - app,因为这是隐含在服务名称中的。 -
您的 Dockerfile 容器冗余命令:
FROM ... as base未使用。并且FROM ... as build不使用build引用,因此可以删除as build。请参阅 multi-stage builds 了解您将在哪里使用它。
标签: docker nginx .net-core docker-compose nginx-reverse-proxy