A proxy is a server used by the client to indirectly access other servers. From the perspective of the server, it will view the proxy server as the client, and be oblivious to the original client. Proxy servers are the intermediary servers that your request passes through when it tries to get from your machine to the remote server.
A reverse proxy is the same, but the scheme is flipped. This is how a reverse proxy works:
- The reverse proxy receives a request
- It relays the request to the proxied service (for example, anĀ application server, such as our Express application)
- It receives the response from the service
- It sends the response back to the client(s)
The client is oblivious to the fact that there's an internal service; in the client's view, the response came directly from the reverse proxy.
The most popular reverse proxy today is NGINX, and that's what we'll use in this book. NGINX is also a generic web server, which provides the following benefits:
- We can host multiple services on the same server; this provides greaterĀ flexibility if we are to add extra services running on the same server later.
- It can handle SSL encryption, which is required for setting up HTTPS.
- It supports features such as caching and GZIP compression.
- It can also act as a load balancer; this allows us to run multiple instances of our Node application, all on different ports, and have NGINX distribute the requests across these processes. It'll do so in a way that minimizes the load on any particular process, and thus maximizes the speed at which a response can be generated.
- Configuration as code; since all HTTP traffic goes through NGINX, it's easy to see a list of all the services that we are exposing to the external world simply by reading NGINX's configurations.
- It has an additional layer of abstraction; we can change how we structure the application internally, and all we have to do is update the NGINX settings. For example, we can have the service run on a different machine within a private network, and our external users would not know the difference.