Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure https support in 443 for apache AND node?

Actual state:

http://www.example.com/mypage apache http: OK!
https://www.example.com/mypage apache https: OK!

http://www.example.com:8000 node http: OK!
https://www.example.com:8000 node https: Not working (still)

I've tried to modify node program to be

var express = require('express');
var app = express();
var https = require('https');
var fs = require('fs');

var server = http.createServer(app);

https.createServer({ 
        key: fs.readFileSync("/etc/letsencrypt/live/www.example.com/privkey.pem"),
        cert: fs.readFileSync("/etc/letsencrypt/live/www.example.com/fullchain.pem"),
        ca: fs.readFileSync("/etc/letsencrypt/live/www.example.com/chain.pem")
}, app).listen(443);

The obvious problem here is that apache is ALREADY listenning to port 443, then

Error: listen EADDRINUSE :::443

Is there a way to use Apache 443 to serve SSL for node?

like image 954
Hernán Eche Avatar asked Jan 03 '23 21:01

Hernán Eche


2 Answers

You can only bind one process to a given port on your server.

That said, the correct way to do this is to have Apache listen on 443, then use mod_proxy to forward the traffic to nodejs either on an HTTP port (not w/ SSL, but you're only talking across localhost) or on a unix socket.

A good example of how to do that with port 80/HTTP is here: http://blog.podrezo.com/making-node-js-work-with-apache/

<VirtualHost *:80>
  ServerName pastebin.mydomain.com
  ServerAlias www.pastebin.mydomain.com
  DocumentRoot /var/www/pastebinjs/
  Options -Indexes
  ErrorDocument 503 /maintenance.html

  ProxyRequests on
  ProxyPass /maintenance.html !
  ProxyPass / http://localhost:8000/
</VirtualHost>

But the theory is the same if you add the Proxy* lines to your existing HTTPS endpoint.

like image 184
Paul Avatar answered Jan 05 '23 16:01

Paul


Your node HTTPS server can run on any port - let's say 9090 instead of the 443 you have set. Use Apache to redirect any traffic that comes in https://example.com to your node https server listening on 9090.

Check out apache reverse-proxy for more information on how to set this up :)

like image 24
HRK44 Avatar answered Jan 05 '23 16:01

HRK44