不幸的是,没有太多关于如何将 Nginx 或 Apache 代理注入(inject) ASP.NET Core Docker 容器的文档。 这是一份很好用的手册,但它有针对 ASP.NET Core 应用程序和 Nginx 的单独图像。
ASP.NET Core API behind the Nginx Reverse Proxy with Docker.
我想在 Azure 上托管我的 Docker 镜像,因此我需要在我的 Docker 容器中安装 Nginx。
基于这篇文章Nginx Reverse Proxy to ASP.NET Core – Same Docker Container我创建了这个配置:
nginx.conf
worker_processes 4;
events { worker_connections 1024; }
http {
sendfile on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
large_client_header_buffers 4 16k;
upstream app_servers {
server 127.0.0.1:5000;
}
server {
listen 80;
location / {
proxy_pass http://app_servers;
proxy_redirect off;
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-Host $server_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}
}
Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nginx
WORKDIR /src
COPY MyApp.csproj MyApp.csproj
RUN dotnet restore
COPY . .
WORKDIR /src
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN rm -f /app/startup.sh
COPY startup.sh /app
RUN chmod 755 /app/startup.sh
RUN rm -f /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx
ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000 80
CMD ["sh", "/app/startup.sh"]
Startup.sh
#!/bin/bash
service nginx start
dotnet /app/MyApp.dll
仍然收到“服务不可用” 可能是因为我有 Azure AAD 身份验证。 有人可以推荐一些东西或提供另一种工作配置吗?
最佳答案
曾俊的评论是对的。将 nginx 与应用程序一起托管在同一个容器中是不可能或不建议的。
我最终创建了 Ingress来自 Nginx 镜像。
已安装 Helm .
由于我的 Kubernetes 集群位于 Azure 中,我使用了下一本手册:
Create an HTTPS ingress controller on Azure Kubernetes Service
关于azure - ASP.NET Core Docker 容器内的 Nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735041/