Nginx is a popular web server that can be used as a reverse proxy and load balancer. In this post, we will explore how to configure Nginx as a reverse proxy and a load balancer and add it for two backend servers HTTP and WebSocket.

What is a Reverse Proxy?
A reverse proxy is a server that sits in front of your application and handles incoming requests. It can be used to add security, load balancing, and other features to your application. In this post, we will focus on configuring Nginx as a reverse proxy.
What is a Load Balancer?
A load balancer is a server that distributes incoming requests across multiple servers. It can be used to improve the performance and availability of your application by distributing the load across multiple servers.
Configuring Nginx as a Reverse Proxy
To configure Nginx as a reverse proxy, you need to set up a reverse proxy server block in your Nginx configuration file. Here's an example configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name backend;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
In this configuration, we have two server blocks. The first block listens on port 80 and serves the content from the domain example.com. The second block listens on port 80 and proxies requests to the backend server running on port 3000.
Configuring Nginx as a Load Balancer
To configure Nginx as a load balancer, you need to set up a load balancer server block in your Nginx configuration file. Here's an example configuration:
upstream backend {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend;
}
}
In this configuration, we have an upstream block that defines three backend servers. The load balancer server block listens on port 80 and proxies requests to the backend servers.
Configuring two backend servers HTTP and WebSocket in Nginx

sudo vi /etc/nginx/nginx.conf
events {
# Event directives...
}
http {
server {
listen 80;
server_name httpbe.doodlelabs.space;
# HTTP Backend
location / {
proxy_pass http://localhost:4001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name wsbe.doodlelabs.space;
# WebSocket Backend
location /websocket {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
To verify if the websocket is working
npm i -g wscat
wscat -c ws://wsbe.doodlelabs.space/websocket
Output
Connected (press CTRL+C to quit)
Note: Add an Elastic IP and associate it with the instance so that even if you stop or reboot the instance it points to the same public IP address.
Explanation
Whenever we host our backend services say like an AWS EC2 instance, we configure our security groups to allow incoming traffic on port 443 and 80. This is because we want to make sure that our backend servers are accessible from the internet.
When we configure Nginx as a reverse proxy, we need to configure our security groups to allow incoming traffic on port 80. This is because Nginx listens on port 80 and proxies requests to the backend servers. 443 is used for SSL/TLS connections also known as HTTPS. We don't need to open port 3001, 3002 & 3003 for our backend servers. This is because we don't want to expose our backend servers to the internet. We only want to expose our Nginx server to the internet. This also acts as a security measure to prevent unauthorized access to our backend servers.
Now whenever a request comes to our Nginx server, it will forward the request to one of the backend servers based on the load balancing algorithm we have configured. This way we can distribute the load across multiple servers and improve the performance and availability of our application. Now this also kind of acts as a reverse proxy, as it forwards the request to the backend servers.
Conclusion
Nginx is a powerful web server that can be used as a reverse proxy and load balancer. By configuring Nginx as a reverse proxy and a load balancer, you can improve the performance and availability of your application by distributing the load across multiple servers.
This format should provide a good overview of Nginx, its use case in reverse proxy and load balancer, and a practical solution for handling incoming requests.
Next blog -> How I deployed DoodleDeck on AWS EC2(backend) and AWS Amplify(frontend)