4

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.

Danh Nguyen
  • 41
  • 1
  • 2
  • I can't write a detailed enough solution to post an answer right now, but you should check out http://stackoverflow.com/questions/10440965/using-socket-io-with-nodejs-on-a-server-with-apache-as-a-reverse-proxy and https://gist.github.com/kassius/954e0787d6893c5ab8e1 – austinian Aug 20 '15 at 00:08
  • You can open any port on EC2 and use it for HTTPS traffic. When adding that to the security group select 'Custom TCP Rule' and enter 3333 for the port. If you want to host both on 443 and have them answer to different host names the simplest solution would be to front both with nginx. Move Apache to 3334 and put nginx on 443. You can set up nginx to send requests for one domain to Apache and the other to Node. It will use SNI for negotiating SSL. I've set this up before and it works well. – Nathan V Aug 26 '15 at 18:46

1 Answers1

0

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>
Algo7
  • 297
  • 1
  • 8
  • Also, don't forget to enable `mod_proxy` under Apache2. Debian flavors example = `sudo a2enmod proxy proxy_html && sudo systemctl restart apache2` -- mod_proxy ref: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html – B. Shea Nov 17 '19 at 15:38