How to Set Up a NGINX Reverse Proxy on Ubuntu
How to Set Up a NGINX Reverse Proxy on Ubuntu
In the world of web servers, NGINX stands out as a powerful and versatile tool. One of its most popular uses is as a reverse proxy server. This article will guide you through setting up a NGINX reverse proxy on Ubuntu, ensuring you have a clear understanding of the process and its advantages.
What is a Reverse Proxy?
A reverse proxy acts as an intermediary for requests from clients seeking resources from other servers. Instead of clients directly accessing the application server, they connect to the reverse proxy, which then forwards the request to the appropriate server. This setup has numerous benefits:
- Load Balancing: Distributes traffic across multiple servers, ensuring no single server is overwhelmed.
- SSL Termination: Handles SSL encryption, offloading this resource-intensive task from application servers.
- Increased Security: Hides the backend server’s IP address and protects it from direct attacks.
- Caching: Stores copies of responses to reduce load times for clients and lessen server strain.
Prerequisites
Before you start, ensure you have:
- An Ubuntu server (18.04 or later) with root access.
- NGINX installed. If it’s not installed, you can do so with the following commands:
sudo apt update
sudo apt install nginx
You can verify the installation by checking the NGINX version:
nginx -v
Step 1: Configure NGINX
The first step is to create a new configuration file for your reverse proxy. This file will define how NGINX handles incoming requests.
Navigate to the NGINX Configuration Directory:
cd /etc/nginx/sites-available/
Create a New Configuration File:
Use a text editor (e.g., nano or vim) to create a new configuration file:
sudo nano my_reverse_proxy.conf
Add Configuration Settings:
Insert the following configuration settings into your file, replacing example.com with your domain name and http://localhost:3000 with the backend service you want to proxy to:
server {
listen 80;
server_name example.com;location / {
proxy_pass http://localhost:3000; # Change this to your backend service
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;
}
}
Explanation of the Configuration
- listen 80;: This tells NGINX to listen for incoming connections on port 80 (HTTP).
- server_name example.com;: Specifies the domain name for your server.
- location /: This block defines how to handle requests to the root of your domain.
- proxy_pass http://localhost:3000;: The address of your backend server. Adjust this to match your specific application.
- proxy_set_header: These directives ensure that the necessary headers are passed to the backend server for proper handling.
Step 2: Enable the Configuration
After creating the configuration file, you need to enable it:
Create a Symbolic Link:
sudo ln -s /etc/nginx/sites-available/my_reverse_proxy.conf /etc/nginx/sites-enabled/
Test the NGINX Configuration:
Run the following command to ensure there are no syntax errors in your configuration:
sudo nginx -t
If the test is successful, you will see a message indicating that the configuration is okay.
Reload NGINX:
Apply your changes by reloading NGINX:
sudo systemctl reload nginx
Step 3: Verify the Reverse Proxy Setup
To confirm that your NGINX reverse proxy is working correctly, open a web browser and navigate to your domain (e.g., http://example.com). If everything is set up properly, you should see the response from your backend application.
Step 4: (Optional) Set Up SSL with Let’s Encrypt
For a secure connection, you may want to set up SSL using Let’s Encrypt. This is a free certificate authority that provides SSL certificates.
Install Certbot:
sudo apt install certbot python3-certbot-nginx
Obtain an SSL Certificate:
Run the following command and follow the prompts:
sudo certbot –nginx
Certbot will automatically configure your NGINX to use SSL. Once complete, you should be able to access your site via https://example.com.
Thank you for visiting our page! Be sure to explore our other article via the link below to boost your Linux expertise. Also, don’t miss our guide on How to Install Linux on M1 and M2 Macs in 2024! 🙂