SSL – Apache and Node.js on the same Amazon EC2 instance

amazon ec2apache-2.4node.jssocketssl

I hosted my website on an EC2 instance, using Apache. SSL was also set up properly, running on HTTPS, port 443.

Currently, I just added a chat application to the website using Node.js + socket.io. The Node.js server listen on port 3333.

How can I run the two servers (Apache and Node.js) on the same instance with SSL secured? Amazon EC2 doesn't allow me to open another port for HTTPS. It only allows 443 for HTTPS.

Best Answer

You don't need to open an additional port for anything. You can just set up a new virtualhost with a new ServerName using the ProxyPass directive to direct the incoming SSL traffics to your local Node.js server.

For example:

<VirtualHost *:443>
        ServerName nodeapp.com
        ServerAlias www.nodeapp.com app.nodeapp.com
        ServerAdmin webmas@localhost
        DocumentRoot /not/that/important

        SSLEngine on
        SSLCertificateFile      /path/to/cert
        SSLCertificateKeyFile   /path/to/key


        ProxyRequests off
        ProxyPass "/" "http://127.0.0.1:3333/"
        ProxyPassReverse "/" "http://127.0.0.1:3333/"

        ErrorLog ${APACHE_LOG_DIR}/nodeapp_error.log
        CustomLog ${APACHE_LOG_DIR}/nodeapp_access.log combined

</VirtualHost>