Monday, February 5, 2018

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

Leave a Comment

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?

2 Answers

Answers 1

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.

Answers 2

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 :)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment