使用Nginx和Docker托管一个由blazor托管的应用程序

时间:2019-07-16 21:08:07

标签: c# docker nginx asp.net-core blazor

目标

在具有保留代理(nginx)和docker(均已容器化)的Ubuntu服务器上部署/托管由blazor托管(.Net Core 3 Preview 6)的应用程序。

配置文件

Visual Studio生成的dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["Website.Server/Website.Server.csproj", "Website.Server/"]
COPY ["Website.Client/Website.Client.csproj", "Website.Client/"]
RUN dotnet restore "Website.Server/Website.Server.csproj"
COPY . .
WORKDIR "/src/Website.Server"
RUN dotnet build "Website.Server.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Website.Server.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Website.Server.dll"]

用于启动应用程序的docker-compose.yml文件:

version: '3'
services:
  nginx:
    image: nginx:1.17-alpine
    volumes:
      - ./data/nginx:/etc/nginx/conf.d
    ports:
      - "80:80"
      - "443:443"
    restart: always
  wesbite:
    image: ImageToTheDockerFileAbove
    expose:
      - "80"
      - "443"
    restart: always

最后是我的Nginx配置:

server {
    listen        80;
    server_name   domain;

    location / {
        return 301 
        https://$host$request_uri;   
    }
}
server {
    listen        443 ssl;
    server_name   domain;

    location / {
        proxy_pass         http://website:80;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

我的ASP.Net Core应用程序的Startup.cs

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().AddNewtonsoftJson();
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBlazorDebugging();
            }

            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            app.UseClientSideBlazorFiles<Client.Startup>();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
                endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
            });
        }
    }

问题

实际上,根本不提供任何内容,我只得到404,但是 容器之间的链接确实存在。因为我在浏览器中输入域后就可以看到以下输出:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET domain  
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Fallback {*path:nonfile}'

关于MSDN,由于blazor应用程序位于常规ASP.Net Core应用程序的后面,因此您无需在nginx的配置中添加以下行。

root /usr/share/nginx/html;
try_files $uri $uri/ /index.html =404;

但是在阅读错误时,在我看来,这仍然是路由问题或类似问题。

0 个答案:

没有答案