Jon Harding February 2016

ExpressJS Server - respond to host header with shared port

Lets say I have corporatewebsite.com listening on port 80. Which is an appache / WordPress site.

I have a node application that I'd like to respond to sub.corporatewebsite.com

The node application is running Express currently. I can get it to listen to a separate port at the moment, but my service won't start if it's pointed to port 80 since it's already in use.

How can I have both apache and node listening to port 80, but having node responding to the subdomain?

Answers


Marc February 2016

You could use a reverse proxy like Nginx to route your subdomains.

Here is an example of nginx configuration, you might probaly have to complete according to your project and server :

server {
    listen          80;
    server_name     corporatewebsite.com;

    location / {
        [ ... some parameters ... ]
        include         proxy_params; // Import global configuration for your routes
        proxy_pass      http://localhost:1234/; // One of your server that listens to 1234
    }
}

server {
    listen          80;
    server_name     sub.corporatewebsite.com;

    location / {
        [ ... some parameters ... ]
        include         proxy_params;
        proxy_pass      http://localhost:4567/; // The other server that listens to 4567
    }
}

You have to configure, for example, apache2 listening to port 1234 while nodejs is listening to port 4567.

If you do like this, a good practice is to block direct access from the outside to your ports 1234 and 4567 (using iptables for example).

I think this post could be useful to you : Node.js + Nginx - What now?

Post Status

Asked in February 2016
Viewed 2,357 times
Voted 8
Answered 1 times

Search




Leave an answer