When I first started working with web servers, reverse proxies sounded far more complicated than they actually were. Once I understood that Nginx simply receives a visitor’s request and forwards it to the correct backend application, the entire setup became much easier to manage.
Learning How to Configure Nginx Reverse Proxy can help you run applications behind a single domain, protect backend ports, manage HTTPS, and create a cleaner server architecture.
What Is an Nginx Reverse Proxy?
An Nginx reverse proxy sits between users and your backend application. Instead of visitors connecting directly to an application running on a port such as 3000, 8000, or 8080, they connect to Nginx through the normal HTTP or HTTPS ports.
Nginx then forwards the request to the appropriate backend server and returns the application’s response to the visitor.
This architecture is commonly used with Node.js, Python, PHP, Java, Docker containers, APIs, and other web applications.
Why Use Nginx as a Reverse Proxy?
A reverse proxy makes server management easier because users do not need to know where an application is actually running.
Nginx can also terminate SSL connections, forward traffic to multiple applications, manage request headers, compress responses, cache certain content, apply rate limits, and distribute requests between multiple backend servers.
Another important advantage is security. Your application can listen only on localhost while Nginx remains the public-facing server. This prevents visitors from directly accessing backend application ports.
Prerequisites Before Configuration

Before configuring the proxy, make sure Nginx is installed and your backend application is already running.
You should also know the backend address and port. For example, an application might be available internally at http://127.0.0.1:3000.
If you plan to enable HTTPS, you will also need a domain pointing to your server.
How to Configure Nginx Reverse Proxy
The basic setup requires creating a server block, defining the domain, forwarding requests to the backend, and passing useful request headers.
Step 1: Install Nginx
On Ubuntu or Debian-based systems, update the package list by running sudo apt update, and then install Nginx with sudo apt install nginx.
Check whether Nginx is running by entering sudo systemctl status nginx.
You should also confirm that your backend application responds correctly before introducing the proxy.
Step 2: Create a Server Block
Create a configuration file for your website inside the /etc/nginx/sites-available/ directory.
A simple configuration begins with a server block that listens on port 80. Set server_name to example.com and www.example.com. Inside the server block, create a location / block and set proxy_pass to http://127.0.0.1:3000.
Here, server_name identifies the domain, while location / tells Nginx that requests beginning at the root path should be handled by this block.
The proxy_pass directive determines where Nginx sends those requests.
Step 3: Add Forwarded Headers
A more production-friendly location / block should set proxy_pass to http://127.0.0.1:3000 and include the following forwarded-header settings:
Set the Host header to $host. Set X-Real-IP to $remote_addr. Set X-Forwarded-For to $proxy_add_x_forwarded_for. Finally, set X-Forwarded-Proto to $scheme.
These headers help backend applications identify the requested host, client IP address, proxy chain and original protocol. When you configure a reverse proxy, it is also important to secure SSH on Ubuntu server to protect administrative access and reduce the risk of unauthorised changes.
Without them, applications may produce incorrect redirects, misleading logs or unreliable HTTPS detection.
Step 4: Enable the Configuration
Create a symbolic link inside sites-enabled by running sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/.
Always test the configuration before reloading Nginx. Run sudo nginx -t to check it.
If the test succeeds, reload the service by entering sudo systemctl reload nginx.
Your domain should now forward requests to the backend application.
Understanding proxy_pass and Trailing Slashes

One detail that frequently causes confusion in a reverse proxy setup is the URI used with proxy_pass.
For example, a location /api/ block containing proxy_pass http://127.0.0.1:3000/; is not always equivalent to a location /api/ block containing proxy_pass http://127.0.0.1:3000;.
Adding or removing the trailing slash can affect how Nginx rewrites the request URI before forwarding it.
When applications rely on specific paths, test the exact URL reaching the backend rather than assuming both configurations behave identically.
Add HTTPS to the Reverse Proxy
Production websites should normally use HTTPS.
A common approach is obtaining a TLS certificate through Let’s Encrypt and configuring Nginx to listen on port 443.
After HTTPS is active, Nginx can handle encrypted connections while communicating with an internal backend over HTTP when appropriate.
Remember to set the X-Forwarded-Proto header to $scheme. This allows your application to recognise that the original visitor connected securely.
Configure WebSockets Through Nginx
Applications using WebSockets often require additional settings. Set proxy_http_version to 1.1, set the Upgrade header to $http_upgrade and set the Connection header to “upgrade.”
Without these settings, ordinary HTTP requests may work while live chat, dashboards, notifications or other real-time features fail.
Common Nginx Reverse Proxy Errors

502 Bad Gateway
A 502 error usually means Nginx cannot successfully communicate with the backend.
Check whether the application is running, verify the port number, confirm the application is listening on the expected interface, and inspect the Nginx and application logs.
Also, test the backend directly from the server.
Too Many Redirects
Redirect loops frequently occur when the backend does not correctly recognise HTTPS.
Forwarding the X-Forwarded-Proto header and configuring the application to trust proxy headers can solve this issue.
Incorrect Client IP Address
If application logs show the proxy address instead of the visitor’s real IP address, verify the X-Real-IP and X-Forwarded-For settings.
WebSocket Connection Failures
If normal pages work but real-time features do not, check the Upgrade and Connection headers and ensure that HTTP/1.1 proxying is enabled.
Nginx Reverse Proxy Best Practices
Once you understand How to Configure Nginx Reverse Proxy, the next goal should be making the setup reliable rather than simply making it work.
Keep backend services inaccessible from the public internet whenever possible. Use HTTPS for public traffic, test every configuration change with the nginx -t command, maintain useful access and error logs, and avoid excessively generous timeout settings unless your application genuinely requires them.
You can also use Nginx for rate limiting, caching, compression, load balancing, and security headers when your application architecture requires additional protection or performance improvements.
Frequently Asked Questions
1. What Does Proxy Pass Do in Nginx?
The proxy_pass directive defines the backend server or application that should receive requests matching a particular Nginx location block.
2. How Do I Fix a 502 Bad Gateway Error?
Verify that your backend is running, confirm its IP address and port, check firewall rules, and inspect both Nginx and application error logs.
3. Can Nginx Proxy Multiple Applications?
Yes. Different domains, subdomains, or URL paths can use separate server or location blocks that send requests to different backend services.
4. Is How to Configure Nginx Reverse Proxy Difficult for Beginners?
No. The basic setup requires only a server block, the proxy_pass directive, forwarded headers, configuration testing, and a reload. HTTPS and WebSockets can then be added as needed.
Final Takeaways
I find Nginx reverse proxy setups easiest to manage when they are built gradually. I start with a small working configuration, verify that Nginx can communicate with the backend, add the required headers, and only then introduce HTTPS, WebSockets, caching, or other production features.
That approach also makes troubleshooting much faster. Instead of dealing with a large configuration full of unknowns, I can isolate each layer and confirm that it works before moving forward. A well-configured reverse proxy ultimately gives you a cleaner public interface, safer backend services, and far more flexibility as your application infrastructure grows.

Leave a Reply