【问题标题】:How can I enforce SSL using ASP.NET Core and nginx如何使用 ASP.NET Core 和 nginx 强制执行 SSL
【发布时间】:2016-12-09 12:41:07
【问题描述】:

我启动了一个运行 Ubuntu 16.04 的新 VM 并运行了命令。 dotnet new -t web 创建一个新的基本 MVC Web 模板。接下来我运行应用程序,连接成功。

之后我修改了 nginx.conf 以使用 SSL

server {
    listen                      443 http2 ssl default;

    ssl_certificate             /etc/ssl/certs/testCert.crt;
    ssl_certificate_key         /etc/ssl/certs/testCert.key;
    ssl_protocols               TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers   on;
    ssl_ciphers                 "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_ecdh_curve              secp384r1;
    ssl_session_cache           shared:SSL:10m;
    ssl_session_tickets         off;
    ssl_stapling                on;
    ssl_stapling_verify         on;

    location / {
        proxy_pass              http://localhost:5000;
        proxy_cache_bypass      $http_upgrade;
        proxy_redirect          off;
        proxy_set_header        Host $host;
        proxy_http_version      1.1;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection keep-alive;
        client_max_body_size    10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout   90;
        proxy_send_timeout      90;
        proxy_read_timeout      90;
        proxy_buffers           32 4k;

    }
}

再次运行应用程序,它在 HTTPS 下运行且没有错误。

然后我在 Startup.cs 中配置 MVC 服务以强制执行 HTTPS。

services.AddMvc(config =>
{
    config.Filters.Add(new RequireHttpsAttribute());
});

最后我尝试再次连接但得到ERR_TOO_MANY_REDIRECTS。但是,如果我只在 Kestrel 上运行并配置一些选项,它就可以正常工作。

services.Configure<KestrelServerOptions>(options =>
{
    options.AddServerHeader = false;
    options.UseHttps("devcert.pfx", "password");
});

它似乎必须是 nginx,但它可能是 MVC/ASP.NET Core 的问题。如何进一步诊断或解决此问题?

【问题讨论】:

    标签: c# asp.net-mvc ssl nginx asp.net-core


    【解决方案1】:

    您的 SSL 连接作为 nginx 终止,它以纯 http 与 Kestrel 通信。 Kestrel 将用户重定向到 https,它在 nginx 中再次终止,并以 http 的形式一次又一次地传递给 Kestrel。那是无限循环。

    将 nginx 配置为需要 https(将 http 重定向到 https),不要碰 Kestrel。您的站点本身将始终在没有 SSL 的情况下工作,而 nginx 将使用 SSL 将其交付给用户(并返回)。

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2019-11-06
      相关资源
      最近更新 更多