skip to Main Content

I’ve been trying to set up SSL on NodeJS, serving index.html through an Apache2 server using CertBot certificates.

Unfortunetly, when I try to make the client to connect to the Server via https, It throws the following error

[index.js:83 GET https://pxlloewe.de:3000/socket.io/?EIO=3&transport=polling&t=NJ9t2YZ net::ERR_SSL_PROTOCOL_ERROR][1]

console

So, is it possible to still provide the files via appache2 and ssl and connect to the Server with the page loaded https:youDomain.com?

I tried to get https running on the NodeJS Server but I am don’t want to provide the browser files via Express

Here is my code, if someone got the chance to read sth out of it:

Serverside:

var express = require("express");
var socket = require("socket.io");

var app = express();
const port = 3000
var server = app.listen(port, () => {
  console.log("Express App auf Port: ", port)
});

//Socket Setup
var io = socket(server);

io.on("connection", function(){
    console.log("Verbunden auf websocket")
})

Clientside:

<html>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" integrity="sha512-v8ng/uGxkge3d1IJuEo6dJP8JViyvms0cly9pnbfRxT6/31c3dRWxIiwGnMSWwZjHKOuY3EVmijs7k1jz/9bLA==" crossorigin="anonymous"></script>
    <title>Einfache Chat App</title>
    <script src="chat.js"></script>
</html>

JS:

// Make Connection
var socket = io.connect('pxlloewe.de:3000', {secure: true});

2

Answers


  1. Chosen as BEST ANSWER

    Here are the steps I follow to get things working:

    1. added a subdomain to my main domain (websocket.pxllloewe.de)

    2. routing to my dyndns (got my Server running at home on a raspberryPi)

    3. setting up apache2/LetsEncrypt via Certbot and Proxy. MyConfig:

      ServerName websocket.pxlloewe.de ServerAdmin [email protected] DocumentRoot /var/www/html/pxlloewe_de ProxyPreserveHost On ProxyRequests Off ProxyPass /socket.io http://localhost:3000/socket.io ProxyPassReverse /socket.io http://localhost:3000/socket.io
           #ProxyPass /socket.io http://localhost/
           #ProxyPassReverse /socket.io http://localhost/
      
           Include /etc/letsencrypt/options-ssl-apache.conf
           SSLCertificateFile /etc/letsencrypt/live/pxlloewe.de-0001/fullchain.pem
           SSLCertificateKeyFile /etc/letsencrypt/live/pxlloewe.de-0001/privkey.pem
      

    Thanks for your help Aviv Lo! Working out fine now.


  2. I am grabbing this apache site conf. straight from my site in production. It is then modified to suite your need.

    <VirtualHost *:443>
            ServerName pxlloewe.de
            #ServerAlias www.pxlloewe.de
            ServerAdmin [email protected]
            DocumentRoot /var/www/pxlloewe_de
    
            LogLevel debug  ssl:info
            SSLEngine on
            SSLCertificateFile /etc/apache2/ssl/pxlloewe.de.crt
            SSLCertificateKeyFile /etc/apache2/ssl/pxlloewe.de.key
    
            RewriteEngine On
            RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
            RewriteCond %{QUERY_STRING} transport=websocket    [NC]
            RewriteCond %{HTTP:Upgrade} websocket              [NC]
            RewriteCond %{HTTP:Connection} upgrade             [NC]
            RewriteRule /(.*)           ws://127.0.0.1:3000/$1 [P,L]
    
            ProxyPass "/socket.io" "http://127.0.0.1:3000/socket.io"
            ProxyPassReverse "/socket.io" "http://127.0.0.1:3000/socket.io"
    
    
            ErrorLog ${APACHE_LOG_DIR}/knct_error_https.log
            CustomLog ${APACHE_LOG_DIR}/knct_access_https.log combined
    
    </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search