Share your love
Ubuntu 24.04 Nginx Reverse Proxy Tutorial – Comprehensive Guide
This guide intends to provide Ubuntu 24.04 Nginx Reverse Proxy Tutorial. Nginx is a powerful and lightweight web server. It is commonly used as a reverse proxy. A reverse proxy is a server that forwards client requests to one or more backend servers. Using Nginx as a reverse proxy can improve your web application’s performance, security, and scalability.
You can now proceed to the following steps provided by the Orcacore team to complete this Ubuntu 24.04 Nginx Reverse Proxy Tutorial.
Table of Contents
Steps To Configure Ubuntu 24.04 Nginx Reverse Proxy Tutorial
Before you start your Ubuntu 24.04 Nginx Reverse Proxy Tutorial, you need some requirements.
First, you must access your server as a non-root user with sudo privileges and set up a basic firewall. Then, you must have a domain name that is pointed to your server’s IP address.
Now you can start the Ubuntu 24.04 Nginx Reverse Proxy Tutorial.
Step 1 – Installing Nginx Web Server
First, you must run the system update and install Nginx on your server with the following commands:
# sudo apt update
# sudo apt install nginx -y
Then, you need to allow HTTP traffic through your UFW firewall. To do this, you can run the command below:
sudo ufw allow 'Nginx HTTP'
Tips: If you don’t have UFW installed and enabled, you can check this guide on Basic UFW Firewall Configuration on Ubuntu 24.04.
Verify your Nginx web server is active and running with the command below:
sudo systemctl status nginx
In your output, you will see:
Note: If your Nginx is not started, you can run the commands below:
# sudo systemctl start nginx
# sudo systemctl enable nginx
At this point, you can start Configuring Ubuntu 24.04 Nginx Reverse Proxy Tutorial.
Step 2 – Nginx Config As a Reverse Proxy on Localhost
At this step, you can configure Nginx to forward requests to a backend service. For example, we assumed that the backend server is running on localhost at port 8080. To do this, follow the steps below:
First, you need to open the default Nginx configuration file. You can use the Vi Editor or Nano Editor:
sudo vi /etc/nginx/sites-available/default
Then, you need to add the following configuration to your file:
server {
listen 80;
server_name your_domain.com; # Replace with your domain name or server IP
location / {
proxy_pass http://localhost:8080;
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;
}
}
Once you are done, save and close the file.
Note: This configuration tells Nginx to forward all requests from port 80 (HTTP) to the backend server running on localhost:8080.
Now check for any syntax error:
sudo nginx -t
If everything is OK, restart Nginx to apply the changes:
sudo systemctl restart nginx
Step 3 – Configure Nginx Server Blocks
At this step of the Ubuntu 24.04 Nginx Reverse Proxy Tutorial, if you plan to host multiple websites or applications on the same Nginx server, you can use server blocks (virtual hosts). To do this, you can follow the steps below:
First, you need to create a new configuration file for your domain. To do this, you can run the command below:
sudo vi /etc/nginx/sites-available/your_domain.com
Then, add the following configuration to your file:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:8080;
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;
}
}
Once you are done, save and close the file.
Next, use the following command to enable your config file:
sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/
Then, check for your syntax error and restart your Nginx web server:
# sudo nginx -t
# sudo systemctl restart nginx
Step 4 – Testing the Nginx Reverse Proxy
At this point, Nginx should be functioning as a reverse proxy. If you had an application as the backend you can verify it by using the following method:
- Access your domain or server’s IP in a web browser.
http://your_domain.com
- Verify that requests are being forwarded to the backend service running on localhost:8080.
If everything is working correctly, you should see the backend application.
For more information, you can visit the Nginx Official Docs page.
Conclusion
At this point, you have learned to easily complete the Ubuntu 24.04 Nginx Reverse Proxy Tutorial. With Nginx, you can manage traffic to your backend services and improve application performance.
Hope you enjoy it. Also, you may like to read the following articles:
Nginx Proxy Manager on Ubuntu 22.04
Check Nginx Version Installed on Linux Terminal
Create Nginx Password Authentication on Ubuntu 22.04
FAQs
Why should We use Nginx as a reverse proxy on Ubuntu 24.04?
Using Nginx as a reverse proxy provides many benefits, including:
– Load balancing across multiple backend servers.
– Improved security by hiding the identity of backend servers.
– Caching to speed up responses for static or frequently requested content.
Can We use Nginx to reverse proxy multiple applications on Ubuntu 24.04?
Yes, you can reverse proxy multiple applications using Nginx by setting up server blocks (also known as virtual hosts). The guide steps are mentioned in the Ubuntu 24.04 Nginx Reverse Proxy Tutorial.
What is proxy_pass in Nginx?
The proxy_pass directive is used to forward requests from Nginx to a backend server. For example, if Nginx is set to listen on port 80 and the proxy_pass directive points to http://localhost:8080. Nginx will forward all incoming requests to the service running on port 8080.