A reverse proxy is an application that sits in front of back-end servers and forwards client requests to those servers instead of having the client directly talking to the servers.
You can also check another alternative using Apache:
Here’s the equivalent configuration for Nginx as a reverse proxy and setting up a virtual host with SSL (HTTPS):
Installing Nginx on Ubuntu/Debian
Nginx is a high-performance web server and reverse proxy. Follow these steps to install Nginx on Ubuntu/Debian:
Step 1: Update Package IndexbashCopy code
sudo apt update
Step 2: Install Nginx
sudo apt install nginx
During the installation, confirm by typing ‘y’ and pressing Enter if prompted.
Step 3: Start Nginx Service
After installation, start the Nginx service:
sudo systemctl start nginx
You can enable Nginx to start automatically on boot:
sudo systemctl enable nginx
Setting Up Nginx as a Reverse Proxy
Nginx can act as a reverse proxy to forward requests to backend servers. Here’s a basic configuration example:
Step 1: Create Nginx Configuration File
Create a new Nginx configuration file for your reverse proxy. Open a terminal and use a text editor:
sudo nano /etc/nginx/sites-available/domain-name.conf
Paste the following configuration into the file, adjusting it for your setup:
server {
listen 80;
server_name domain-name.com;
location / {
proxy_pass http://backend-server-ip:backend-port;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
server_name domain-name.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Replace domain-name.com
with your domain, backend-server-ip
with the IP address of your backend server, and backend-port
with the port your backend server is listening on.
Save the file and exit the text editor.
Step 2: Enable the Nginx Configuration
Enable the newly created Nginx configuration using a symbolic link:
sudo ln -s /etc/nginx/sites-available/domain-name.conf /etc/nginx/sites-enabled/
Step 3: Test Nginx Configuration
Check the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the new configuration:
sudo systemctl reload nginx
Setting Up a Virtual Host with Nginx and SSL (HTTPS)
Now, let’s configure a virtual host with Nginx and secure it with SSL using Certbot:
Step 1: Obtain SSL Certificate with Certbot
First, install Certbot:
sudo apt install certbot python3-certbot-nginx
Obtain an SSL certificate for your domain and configure Nginx to use SSL:
sudo certbot --nginx -d domain-name.com
Follow the Certbot prompts to configure SSL for your domain.
After completing these steps, your Nginx server will act as a reverse proxy and serve content securely over HTTPS for your virtual host.
Real Example using Digital Ocean Droptlet :
- Go to your DNS Manager add a DNS record to your static server IP address.
In this example, we are using 209.97.149.245 :
2. Update APT and Install Nginx
3. Check that Nginx is installed
Check the installed version:
nginx -v
Check the status:
And also use the IP address, you should see a similar Nginx welcome page.
4. Add Nginx configuration file, test if the syntax is correct and link to sites-enabled
Do not forget to remove the default configuration file that comes with Nginx. Check it in “/etc/nginx/sites-available” and “/etc/nginx/sites-enabled”
5. Check that your site is running on HTTP
6. Obtain SSL Certificate with Certbot
Your configuration file will be updated automatically:
[1] “What Is A Reverse Proxy? | Proxy Servers Explained | Cloudflare,” Cloudflare. Available: https://www.cloudflare.com/learning/cdn/glossary/reverse-proxy/
[2] “How to Set up a Reverse Proxy (Step-By-Steps for Nginx and Apache),” Kinsta, Aug. 14, 2020. Available: https://kinsta.com/blog/reverse-proxy/
More stories: