0

I have owned a VPS running Ubuntu for nearly half a decade, and I have a working knowledge of the networking aspect - but there are many holes in my understanding.

When I visit DOMAIN:PORT I understand that the request is routed to the IP in my A-record specified in my nameservers, and consequently reaches my VPS - but I don't completely understand what happens after that:

  1. How does nginx then receive the request? Is there some hardware level functionality that routes it to the configured web server?
  2. If I have multiple webservers running, say nginx and apache - what decides what takes precedence?
  3. Does nginx then intercept all incoming requests? Say I have ufw allowing 8000 - then is that DOMAIN:8000 request routed through nginx? Or can it hit some other say node server running locally on that port?

These are questions I have puzzled over without being able to figure out what to google for.

Any pointers in the right direction will be much appreciated.


P.S. Mods, I looked at the suggested related questions and I feel like none address my specific set of questions. Feel free to correct me in the comments, if I'm wrong.

Vidur
  • 113
  • 6

1 Answers1

2

nginx (or Apache) tells the kernel that it is listening on a specific port by calling listen(2). The port number is configured with listen directive in the nginx config (Listen for Apache.). Only one process can listen for a specific port, so there is no order, the second program attempting to listen on the same port will fail with EADDRINUSE (Address already in use).

AlexD
  • 8,747
  • 2
  • 29
  • 38
  • Ah I see. So if I don't set nginx to listen to say port 8080 - and spin up some other service to run on that port - should I be able to reach that service directly from the outside? – Vidur Feb 08 '22 at 06:21
  • If that service listens on the external IP or all IPs and the port isn't blocked by firewalls then you should be able to connect to that port from outside. – AlexD Feb 08 '22 at 15:11