【问题标题】:Binding HttpListener to https on specific port in Linux/Ubuntu在 Linux/Ubuntu 的特定端口上将 HttpListener 绑定到 https
【发布时间】:2019-04-18 16:29:23
【问题描述】:

我在 .netcore 中创建了一个 HttpListener 来侦听特定端口上的传入请求,我需要将 ssl 证书绑定到 Ubuntu(版本 18.04.2 LTS)上的端口 8202,但不确定如何。我的谷歌搜索使用 Apache(或另一个我没有运行的网络服务器)或使用 httpcfg 但我没有使用单声道的虚拟主机。

我在我的 Windows 机器上创建了这个应用程序,一旦我使用“netsh http add sslcert”,一切都运行良好,在 linux 端,应用程序在列出到 http: 但不是 https: 时运行良好。如果需要,我愿意安装 Apache 并设置虚拟主机,但我觉得我遗漏了一些东西/它过于复杂。 下面是侦听器的创建,应用程序的其余部分与处理请求/数据有关,因此不相关(尽管会在需要时发布)

                  // start listing on port
            HttpListener listener = new HttpListener();
            string url = "https://serverfqdn:8202/middleman/";
            listener.Prefixes.Add(url);
            try
            {
                listener.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine("bruh i broke");
                Console.WriteLine(e.Message);
            }

现在,当我在 https 上的 Ubuntu 框列表上运行此应用程序并运行测试时,我收到错误:“无法从传输连接读取数据:现有连接被远程主机强行关闭。”

如前所述,如果它在 http 上列出,一切正常,我会得到适当的响应。

【问题讨论】:

  • @Renat 我不确定这是否真的适用,除非我对这一切如何运作有根本的误解。这似乎没有将证书绑定到端口,而只是 .netcore 应用程序获取证书的位置/方式。

标签: c# ubuntu ssl .net-core


【解决方案1】:

我知道这可能会迟到。但我希望可以帮助别人。我阅读了几种在 Linux 中运行的 C# 代码上实现/绑定 SSL 的方法。但他们中的大多数人建议创建 SSL 证书,这非常复杂。

这里我分享了我如何仅使用 Nginx/Apache 设置将非 SSL http(c# 代码)重新路由到 https 地址。

保持代码非 ssl

server.Prefixes.Add("http://your-host-name.com:11110/");

然后为地址创建一个 Nginx/Apache config(我使用 Nginx):

server {
    listen      11111 ssl http2; #port number for ssl
    server_name  your-host-name:11110;
    
    ssl_certificate "/home/ssl/fullchain.pem";
    ssl_certificate_key "/home/ssl/privkey.pem";
    
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    
    client_max_body_size 128m;
    fastcgi_read_timeout 3000;
    proxy_read_timeout 3000;
    proxy_set_header Forwarded $proxy_add_forwarded;
    proxy_buffering off;
    
    include /etc/nginx/default.d/*.conf;
    
    location / {
        proxy_pass http://your-host-name:11110; #set default route
        proxy_redirect off;
    }
}

此配置适用于 Nginx 服务器。 Apache 配置更简单。您可以关注/etc/apache2/sites-available 目录中的000-default.conf 示例。

【讨论】:

    猜你喜欢
    • 2018-03-02
    • 2016-01-17
    • 2015-11-05
    • 2023-03-09
    • 2018-06-02
    • 1970-01-01
    • 2019-11-28
    • 2018-11-10
    • 2011-06-11
    相关资源
    最近更新 更多