How to Configure Nginx as a Reverse Proxy on Linux: A Practical Step-by-Step Guide
Hello, I'm Arthur. When hosting a web application, the application server doesn't always need to handle incoming internet traffic directly. A reverse proxy can sit between visitors and your application, forwarding reque
Hello, I'm Arthur.
When hosting a web application, the application server doesn't always need to handle incoming internet traffic directly.
A reverse proxy can sit between visitors and your application, forwarding requests to the correct backend service.
Nginx is one of the most commonly used tools for this purpose. It is lightweight, flexible, and provides useful features for managing web traffic.
In this guide, we'll configure Nginx as a reverse proxy on an Ubuntu server and explore some practical troubleshooting techniques.
1. Understand How a Reverse Proxy Works
A reverse proxy receives requests from users and forwards them to backend servers.
For example, imagine an application running on port 3000.
Instead of allowing visitors to access that port directly, Nginx can receive requests on standard HTTP port 80 and forward them to the application.
The basic request flow looks like this:
Visitor → Nginx → Backend Application → Nginx → Visitor
This approach provides several benefits:
- Centralized traffic management
- SSL termination
- Backend application isolation
- Easier routing between services
- Support for load balancing
2. Install Nginx on Ubuntu
Start by updating the package list:
sudo apt update
Install Nginx:
sudo apt install nginx -y
Check whether the service is running:
sudo systemctl status nginx
If it isn't running, start it:
sudo systemctl start nginx
Enable Nginx to start automatically after reboot:
sudo systemctl enable nginx
You can now check the server's IP address in a browser to verify that the default Nginx page is accessible.
3. Create a Simple Backend Application
For this example, assume your application is already running locally on port 3000.
You can test the backend using:
curl http://127.0.0.1:3000
If the application responds successfully, Nginx can be configured to forward incoming requests to it.
Make sure your backend service is running before proceeding.
4. Configure Nginx as a Reverse Proxy
Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/myapp
Add the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
Replace example.com with your actual domain name.
The proxy_pass directive forwards requests to the backend application.
The other headers help the backend identify the original request information.
5. Enable the Nginx Configuration
Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test the configuration:
sudo nginx -t
If the test succeeds, reload Nginx:
sudo systemctl reload nginx
Your domain should now forward requests to the application running on port 3000, provided the DNS record points to your server.
6. Configure SSL for Secure Connections
Websites should use HTTPS to protect communication between visitors and servers.
One common option is Certbot with Let's Encrypt.
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Request an SSL certificate:
sudo certbot --nginx -d example.com
Follow the instructions to complete the certificate setup.
Your domain must already point to the server, and the required HTTP/HTTPS connectivity must be available.
After configuration, verify that your website loads over HTTPS.
7. Troubleshoot Common Nginx Reverse Proxy Errors
Even a correctly configured reverse proxy can encounter problems.
Here are some common issues.
502 Bad Gateway
This often happens when Nginx cannot communicate with the backend application.
Check whether the application is running:
sudo ss -tulnp
Then test the backend directly:
curl http://127.0.0.1:3000
504 Gateway Timeout
A timeout can occur when the backend takes too long to respond.
Investigate application performance, network connectivity, and backend logs before increasing timeout values.
403 Forbidden
Check file permissions, application access rules, and Nginx configuration.
Configuration Errors
Run:
sudo nginx -t
Review the output for incorrect directives, missing semicolons, or configuration mistakes.
8. Monitor Nginx Logs
Logs help administrators understand how Nginx handles incoming requests.
View the error log:
sudo tail -f /var/log/nginx/error.log
View access logs:
sudo tail -f /var/log/nginx/access.log
These logs can help identify failed requests, backend connection problems, and unusual traffic patterns.
For production environments, regular log monitoring should be part of routine server maintenance.
9. Choose Infrastructure That Supports Your Applications
A reverse proxy helps organize application traffic, but the underlying server still needs enough resources to handle its workload.
CPU, memory, storage performance, and network capacity all influence application responsiveness.
When evaluating HelloServer dedicated server hosting, administrators can compare infrastructure options for websites, backend services, and production applications.
For workloads involving machine learning, AI processing, or other GPU-intensive tasks, HelloServer GPU dedicated servers are another option to consider when specialized GPU resources are required.
Final Thoughts
Nginx reverse proxy configuration is a useful skill for Linux administrators and developers managing web applications.
It allows incoming traffic to be handled centrally while backend applications run separately.
With proper configuration, SSL, monitoring, and troubleshooting, Nginx can help create a more organized and reliable application hosting environment.
The most important thing is to understand how requests move between the proxy and backend service. Once you understand that flow, diagnosing common reverse proxy problems becomes much easier.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.