I have a scenario where my node.js app is behind a Load Balancing HAProxy, which is forwarding both HTTP and HTTPS traffic to port 8000 on my node servers.
Unfortunately this puts me in a tricky spot: I need to use the SAME port for my http and https servers. I would also be satisfied merely redirecting http to https, if that is easier in some way.
But, right now, I am in a position where I am forced to choose HTTP --OR-- HTTPS
For the sake of including code...
https.createServer(api.credentials, api.server).listen(8000);
http.createServer(api.server).listen(8000); // ERR ADDR IN USE
You cannot make two tasks listen on the same port. HTTP listens on the default port 80 and HTTPS listens on the default port 443. You're going to want to manage your HAProxy program to forward accordingly.
I found this nifty page that recommended you to set up your configuration like so:
global
stats socket ./haproxy.stats level admin
frontend ft_http
bind :80
mode http
default_backend bk_http
frontend ft_https
bind :443
mode tcp
default_backend bk_https
backend bk_http
mode http
balance roundrobin
stick on src table bk_https
default-server inter 1s
server s1 192.168.1.1:80 check id 1
server s2 192.168.1.2:80 check id 2
backend bk_https
mode tcp
balance roundrobin
stick-table type ip size 200k expire 30m
stick on src
default-server inter 1s
server s1 192.168.1.1:443 check id 1
server s2 192.168.1.2:443 check id 2
Note how the configuration above sets the Node.js ports at 80 and 443. Since Node.js doesn't have admin privileges, you might want to edit your ports to work like so:
backend bk_http
mode http
balance roundrobin
stick on src table bk_https
default-server inter 1s
server s1 192.168.1.1:8000 check id 1
server s2 192.168.1.2:8000 check id 2
backend bk_https
mode tcp
balance roundrobin
stick-table type ip size 200k expire 30m
stick on src
default-server inter 1s
server s1 192.168.1.1:8001 check id 1
server s2 192.168.1.2:8001 check id 2
You are also going to want to change your Node.js code to:
https.createServer(api.credentials, api.server).listen(8001); http.createServer(api.server).listen(8000);
I'm not familiar with HAProxy, so I may be off.