0

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?

Jon Harding
  • 4,928
  • 13
  • 51
  • 96

1 Answers1

1

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?

Community
  • 1
  • 1
Marc
  • 1,350
  • 2
  • 11
  • 29
  • But the goal is for both of them to listen on port 80. (I'm new to this stuff) – Jon Harding Feb 08 '16 at 22:02
  • They will, visitors on your website will only use " corporatewebsite.com " and " sub.corporatewebsite.com " (on port 80). Nginx will then send the request either to port 1234 or 4567 according to the subdomain requested. – Marc Feb 08 '16 at 22:05
  • You can't have two servers listening to the same port. But you can have one reserve proxy listening to port 80 and then forwarding the request to another port (where either your apache or nodejs is listening) – Marc Feb 08 '16 at 22:08
  • I edited my answer to give you a link speaking about nginx and nodejs. – Marc Feb 08 '16 at 22:14
  • perfect, I'll give it a shot – Jon Harding Feb 08 '16 at 22:27