Jeffrey Quinn
Jeffrey Quinn

Reputation: 227

Binding HttpListener to https on specific port in Linux/Ubuntu

I have created a HttpListener in .netcore to listen on a specific port for incoming requests, I need to bind an ssl cert to the port 8202 on Ubuntu (version 18.04.2 LTS) but unsure how. My googling brings up virtual hosts with Apache (or another web-server which I am not running) or utilizing httpcfg but I am not using mono.

I created this application on my windows machine, and everything worked perfectly fine once I used "netsh http add sslcert", on the linux side the application works fine when listing to http: but not https:. I am willing to install Apache and set up the virtual hosts if that is whats needed, but I feel that I am missing something/it is being overly complicated. below is the creation of the listener, the rest of the application is concerned with handling the requests/data so shouldn't be relevant (though will post if needed)

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

Right now when I run this app on the Ubuntu box listing on https and run a test I get the error: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

As said before if it is listing on http everything works and I get an appropriate response.

Upvotes: 5

Views: 3338

Answers (1)

Mr Hery
Mr Hery

Reputation: 870

I know this might be late. But I hope can help others. I read several ways to implement/bind SSL on C# code runs in Linux. But most of them suggesting SSL Cert creation which is quite complicated.

Here's I share how I implement this using only Nginx/Apache setting to re-route the non-ssl http (c# code) to https address.

Keep the code non-ssl

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

Then create a Nginx/Apache config for the address (I use 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;
    }
}

This config is for Nginx server. Apache config is more simple. You can follow the 000-default.conf sample in /etc/apache2/sites-available directory.

Upvotes: 4

Related Questions