草庐IT

c# - NancyFX + SSL - 如何让 "this.RequireHttps()"在 Linux 上工作?

coder 2023-06-19 原文

我的自托管* 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 上访问,SSL 在端口 1234 上工作。

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

现在我按照本教程使用 nginx 来提供 SSL:

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

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

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()”变得毫无值(value) - 当我这样使用它时:

this.RequiresHttps(true, 443)

Chrome 报告 ERR_TOO_MANY_REDIRECTS

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

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

提前致谢。

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

最佳答案

结合:Nancy 团队的帮助、其他一些资源和我空洞的头脑,我终于设法解决了这个问题。

首先 - 错误 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 - 这是我们的无限循环。它在 Windows 上运行良好,因为应用程序可以直接访问 httphttps 端点,并管理它们。

修复相当简单(虽然我花了一些时间才找到它)并且包括两个步骤:

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

    SSLProxy.RewriteSchemeUsingForwardedHeaders(管道);

这将使 Nancy 监听 X-Forwarded-Proto header - 当它存在时 - 此方法会将请求 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 - 对于 header ,Nancy 会检测到它并且它不会再重定向 - 它应该是这样。

我希望这对某人有帮助。

最好的问候。

关于c# - NancyFX + SSL - 如何让 "this.RequireHttps()"在 Linux 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634033/

有关c# - NancyFX + SSL - 如何让 "this.RequireHttps()"在 Linux 上工作?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  5. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  6. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  7. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  8. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  9. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  10. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

随机推荐