繁体   English   中英

How to setup https when developing localy with webpack and hosting on Azure in Docker container running ASP.NET Core

[英]How to setup https when developing localy with webpack and hosting on Azure in Docker container running ASP.NET Core

我在 Azure 上托管,并将其配置为仅允许 https。 后端在 Linux 容器中运行 ASP .NET Core。 网络服务器 (Kestrel) 在未启用 https 的情况下运行。 我已经配置了 Azure TLS/SSL 设置来强制 https,所以当用户从公共互联网连接时,他们必须使用 https。 我有一个由证书颁发机构签名的证书,它是在 Azure 应用服务 -> TLS/SSL -> 绑定设置中配置的。

但是在我的本地开发环境中,我一直在使用 http 运行 webpack。 因此,当我测试时,我连接到 localhost:8080,并被 webpack 重定向到 localhost:8085。 localhost:8085 是 Kestrel 正在侦听的端口。 我已经决定要使用 https 在本地进行开发,以便我的环境密切模仿生产环境。 为此,我使用--https命令行选项启动了 webpack-dev-server,并在 webpack.config.js 中修改了我的重定向

例如:

'/api/*': {
            target: 'https://localhost:' + (process.env.SERVER_PROXY_PORT || "8085"),
               changeOrigin: true,
               secure: false
           },

这会将 https 请求重定向到端口 8085。

我创建了一个自签名证书,供 Kestrel 在本地开发时使用。 我修改了我的代码以使用此证书,如下所示:

let configure_host (settings_file : string) (builder : IWebHostBuilder) =
    //turns out if you pass an anonymous function to a function that expects an Action<...> or
    //Func<...> the type inference will work out the inner types....so you don't need to specify them.
    builder.ConfigureAppConfiguration((fun ctx config_builder ->
                config_builder
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddEnvironmentVariables()
                    .AddJsonFile(settings_file, false, true)
                    .Build() |> ignore))
           .ConfigureKestrel(fun ctx opt ->
                eprintfn "JWTIssuer = %A" ctx.Configuration.["JWTIssuer"]
                eprintfn "CertificateFilename = %A" ctx.Configuration.["CertificateFilename"]
                let certificate_file = (ctx.Configuration.["CertificateFilename"])
                let certificate_password = (ctx.Configuration.["CertificatePassword"])
                let certificate = new X509Certificate2(certificate_file, certificate_password)
                opt.AddServerHeader <- false
                opt.Listen(IPAddress.Loopback, 8085, (fun opt -> opt.UseHttps(certificate) |> ignore)))
           .UseUrls("https://localhost:8085") |> ignore
    builder

这一切正常,我可以在本地连接到 webpack 并使用 https 将请求重定向到网络服务器。 浏览器抱怨证书不安全,因为它是自签名的,但这是意料之中的。

我的问题是如何在生产环境中进行设置。 我不想在 azure 上使用我在本地创建的嵌入图像的证书运行容器。 在我的生产环境中,我是否应该像使用 localhost 代码一样配置 Kestrel,以使用加载到 Azure 中的证书(如第 1 段所述)? 或者只是使用门户将其绑定到域并通过 Web UI 强制 https 就够了?

在 Azure 上,如果您有 PFX 证书,您可以选择上传证书:见此图

但是,此证书需要来自受信任的证书颁发机构。

如果 URL 是子域,您可以选择免费应用服务托管证书。

之后,您需要做的就是仅在门户中启用 https。

如果它是一个裸域并且您确实需要免费的证书,您可以从 sslforfree.com 获得证书。 sslforfree 将为您提供 .cer 文件和生成 pfx 所需的私钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM