NancyFX + SSL-如何使“ this.RequireHttps()”在Linux上工作?

use*_*366 6 c# linux mono nancy

我的自托管NancyFX应用程序使用SSL,并且我使用“ this.RequiresHttps() ”将某些模块标记为“仅SSL”。在Windows上,我遵循了本教程:

https://github.com/NancyFx/Nancy/wiki/Accessing-the-client-certificate-when-using-SSL

后:

netsh http add sslcert ipport=0.0.0.0:1234 certhash=303b4adb5aeb17eeac00d8576693a908c01e0b71 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable
Run Code Online (Sandbox Code Playgroud)

我使用以下代码:

public static void Main(string[] args)
{
    List<Uri> uri2 = new List<Uri>();
    uri2.Add(new Uri("http://localhost:80"));
    uri2.Add(new Uri("https://localhost:1234"));

    HostConfiguration hc = new HostConfiguration()
    {
        EnableClientCertificates = true
    };

    using (var host = new NancyHost(hc,uri2.ToArray()))
    {
        host.Start();

        string runningOn = "\n\n";
        foreach(var item in uri2)
        {
            runningOn += item+"\n";
        }

        Console.WriteLine("Your application is running on " + runningOn/*uri2.First()*/);
        Console.WriteLine("Press any [Enter] to close the host.");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

而且效果很好-可以在端口80上访问未加密的数据,而在端口1234上可以访问SSL。

问题是-我想在Linux主机上做同样的事情,但是似乎找不到与Windows“ netsh ” 等效的命令。

现在,通过遵循本教程,我最终使用nginx提供了SSL:

https://github.com/NancyFx/Nancy/wiki/Hosting-Nancy-with-Nginx-on-Ubuntu

然后将nginx config修改为以下内容(不要介意路径,这只是我的开发虚拟机):

server {
    listen       80;
    listen       443 ssl;
    ssl_certificate    /home/james/sslCert2/server.crt;
    ssl_certificate_key /home/james/sslCert2/server.key;


    server_name  localhost;
    root /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/;

    location /Content/ {
        alias /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/Content/;
        location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf)$ {
            expires 365d;
        }
    }

    location / {
            proxy_pass http://127.0.0.1:8080;
    }       
}
Run Code Online (Sandbox Code Playgroud)

然后修改Nancy代码以仅侦听端口8080。

尽管上述方法有效(nginx正在管理SSL连接并将请求重定向至端口8080的Nancy),但它使“ this.RequiresHttps() ”一文不值-当我这样使用时:

this.RequiresHttps(true, 443)
Run Code Online (Sandbox Code Playgroud)

Chrome报告ERR_TOO_MANY_REDIRECTS

所以我的问题是-如何/是否有可能在Linux上配置Nancy来制作“ this.RequiresHttps() ”?

Nancy团队的成员还可以解释一下“ EnableClientCertificates ”主机配置选项的作用吗?是否需要启用SSL?文档相当稀缺...

提前致谢。

*当我以自托管方式启动项目时,如有必要,可以将其修改为使用nginx或任何其他托管形式。

use*_*366 4

结合:南希团队的帮助、一些其他资源和我空虚的头脑,我终于成功解决了这个问题。

首先 - 错误ERR_TOO_MANY_REDIRECTS实际上是我第一篇文章中 nginx 配置的逻辑结果 - 当用户尝试访问受 SSL 保护的资源时,Nancy 将他重定向到端口443,这是正确的行为,然后 nginx 在该端口上收到请求,建立了 SSL 连接。 .并将其发送回端口 8080 处的 Nancy。由于 nginx 始终通过不安全的连接 ( http://127.0.0.1:8080 ) 与 Nancy 通信,Nancy 无法知道正在使用 SSL,因此它再次重定向请求到端口 443,nginx 再次接收它,建立 SSL 并将其发送到http://127.0.0.1:8080 - 这是我们的无限循环。在 Windos 上它可以工作,因为应用程序可以直接访问httphttps端点,并且管理它们。

修复方法相当简单(尽管我花了一些时间才找到它)并且涉及两个步骤:

  1. 将以下行添加到Nancy bootstrapper 中的 RequestStartup 方法

    SSLProxy.RewriteSchemeUsingForwardedHeaders(管道);

这将使 Nancy 侦听X-Forwarded-Proto 标头- 当它存在时 - 此方法会将请求 url 方案覆盖为 https - 所以现在:

this.RequireHttps()
Run Code Online (Sandbox Code Playgroud)

将检测请求是否启用了 SSL。

  1. 用这样的东西配置 nginx(仍然 - 不要介意路径 - 这只是开发机器):

    服务器{听80;

    server_name  localhost;
    root /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/;
    
    location /Content/ {
        alias /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/Content/;
        location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf)$ {
            expires 365d;
        }
    }
    
    location / {
    proxy_pass http://127.0.0.1:8080;
    }    
    
    Run Code Online (Sandbox Code Playgroud)

    }

    服务器 { 监听 443 ssl; ssl_certificate /home/james/sslCert2/server.crt; ssl_certificate_key /home/james/sslCert2/server.key;

    server_name  localhost;
    root /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/;
    
    location /Content/ {
        alias /home/james/nancywebpageroot/NancyWebPage.DPL.Services/bin/Debug/Content/;
        location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf)$ {
            expires 365d;
        }
    }
    
    location / {
    proxy_pass http://127.0.0.1:8080;
    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;
    }    
    
    Run Code Online (Sandbox Code Playgroud)

    }

注意:我对 nginx 没有经验- 虽然上面的方法似乎有效,但可能会出现错误(如果有人看到任何错误 - 请指出)或更好的方法来做到这一点 - 你已被警告:)

那么这里发生了什么?“正常”请求将到达端口80并被简单地重定向到 Nancy 标准路径,当 nginx 在端口443上收到某些请求时,它将包含X-Forwarded-For 标头,Nancy 会检测到它并且不会再重定向- 如它应该是。

我希望这可以帮助别人。

此致。