19

I would like to run a XAMPP server, and a Nodejs server on the port 80.

If the server get a HTTP request, then XAMPP will handle it, if the server get a Websocket request, then Nodejs

How is it possible? If a port is already in use, then I can't start the other server program.

iter-ator
  • 211
  • 2
  • 5

3 Answers3

45

You would need to use a reverse proxy to do this, e.g. Apache 2.4. with mod_proxy_wstunnel.

Use it as a frontend and then tunnel the connections to the appropriate backend.

Sven
  • 97,248
  • 13
  • 177
  • 225
22

Only a single process can listen on a particular port for the same IP address and protocol, because otherwise operating system wouldn't know which process to send the received packet to.

To achieve what you want you will have to use a reverse proxy which will listen on port 80 and forward traffic to correct destination. There are multiple ways to do this. For example you can set up your web server (Apache) to forward WebSocket requests or you may configure Node.js to forward HTTP traffic. Research available options and choose the one that fits your needs best.

grekasius
  • 2,046
  • 11
  • 15
3

As well as the reverse proxy option others have already mentioned, you could have multiple IP addresses assigned to the machine. This may cost for IPv4 (or simply not be available depending where your server is hosted) but should not if you are in a position to use IPv6 (i.e. if the people connecting to one or both or your services are going to be IPv6 capable). This way both services can listen on port 80 on different addresses.

As you only mention port 80 I assume that you are not using HTTPS. If you are then if using the reverse proxy option you need to either need to have a multi-name SSL certificate or configure SNI and live with the fact that some old clients (IE on XP, old Android versions) won't be able to connect without errors. The multiple address option circumvent this issue at the expense of needing the extra address(es) assigned.

David Spillett
  • 22,534
  • 42
  • 66
  • Just a small point: if you're already using NAT to translate the IP, you can do the port redirection there as well. E.g. if you're running an Internet-accessible Apache server & want to use XAMPP for testing, run Apache on a non-standard port and have the router NAT external port 80 requests to the non-standard port of the internal IP. This would allow running XAMPP on port 80. – Calrion Aug 31 '14 at 06:41