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

user2384366:

我的自托管* 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

我使用以下代码:

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();
    }
}

而且效果很好-可以在端口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;
    }       
}

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

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

this.RequiresHttps(true, 443)

Chrome报告ERR_TOO_MANY_REDIRECTS

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

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

提前致谢。

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

user2384366:

结合起来:在南希团队的帮助下,其他一些资源和我空洞的头脑终于解决了。

首先-错误ERR_TOO_MANY_REDIRECTS实际上是我第一篇博文中nginx配置的逻辑结果-当用户尝试访问受SSL保护的资源时,Nancy将他重定向到端口443,这是正确的行为,然后nginx在该端口上获得了请求,建立了SSL连接。然后通过端口8080将其发送回Nancy。由于nginx始终在不安全的连接上与Nancy通信(http://127.0.0.1:8080),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()

将检测到请求已启用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;
    }    
    

    }

    服务器{监听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;
    }    
    

    }

注意:我对nginx没有经验 -尽管上述方法似乎可行,但可能存在错误(如果有人看到任何错误-请指出这些错误或更好的方法-有人警告您:)

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

我希望这可以帮助别人。

最好的祝福。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章