Nginx is one of the most popular tools for hosting websites and routing traffic to applications running on Linux servers.
On an Ubuntu VPS, it is often used as a reverse proxy for Node.js applications, Python services, Docker containers, dashboards, and other self-hosted software.
The basic concept is simple: Nginx accepts incoming HTTP or HTTPS requests and forwards them to an application running on another address or port.
However, a reverse proxy introduces another layer between the visitor and the application. When something stops working, administrators must determine whether the problem originates in Nginx, the backend application, the operating system, or the network.
A website may return 502 Bad Gateway, fail to load WebSocket connections, redirect visitors incorrectly, or work locally while remaining inaccessible from the internet.
This article examines common Nginx reverse proxy mistakes on Ubuntu VPS servers and explains how to diagnose them systematically.
1. Understanding the Reverse Proxy Request Path
Before troubleshooting Nginx, it helps to understand how requests travel through the server.
Consider a web application listening on TCP port 3000.
Without a reverse proxy, a visitor might access the application directly using the server’s IP address and port.
With Nginx, the connection follows a different path:
Visitor → Nginx → Backend Application → Nginx → Visitor
Nginx accepts the public connection on port 80 or 443 and forwards the request to the application.
For example:
- Public website:
https://example.com - Reverse proxy: Nginx on the Ubuntu VPS
- Backend application:
http://127.0.0.1:3000
This arrangement allows applications to remain accessible through internal interfaces while Nginx manages public traffic, HTTPS termination, request headers, and routing.
A properly configured reverse proxy also makes it possible to host several applications behind the same public IP address.
However, each additional component introduces another potential point of failure.
2. Mistake: Incorrect Backend Address or Port
One of the most common Nginx reverse proxy errors is an incorrect upstream destination.
Suppose the configuration contains:
proxy_pass http://127.0.0.1:3000;
This tells Nginx to forward requests to a service listening on port 3000 of the loopback interface in Nginx’s network environment.
If the application actually listens on port 8080, Nginx cannot reach the intended service.
The same problem occurs when the application is stopped or configured to listen on a different address.
How to investigate
First, verify that the backend application is running.
On Ubuntu, you can inspect listening TCP sockets:
sudo ss -lntp
Look for the application’s expected address and port.
Next, test the backend directly:
curl -v http://127.0.0.1:3000/
If the backend does not respond, investigate the application before changing the Nginx configuration.
Check its service status, logs, startup parameters, and network bindings.
A reverse proxy cannot successfully forward requests to an application that is unavailable.
A special case: Docker containers
If Nginx runs directly on the VPS while the backend application runs inside Docker, the correct upstream address depends on the container’s networking configuration.
If Nginx and the application run in separate containers on the same Docker network, they may communicate using the application’s container or service name.
If Nginx runs on the host, it may instead need an appropriately published host port.
Do not assume that 127.0.0.1 always refers to the same network environment when containers are involved.
3. Mistake: Confusing 502 and 504 Gateway Errors
Two particularly common errors are:
502 Bad Gateway and 504 Gateway Timeout.
Although both involve communication with an upstream service, they can indicate different problems.
502 Bad Gateway
A 502 response often appears when Nginx cannot obtain a valid response from its upstream.
Possible reasons include:
- The backend application has stopped.
- The configured upstream port is incorrect.
- The application refuses the connection.
- The backend closes the connection unexpectedly.
- Nginx receives an invalid upstream response.
However, the status code alone does not identify the exact cause.
504 Gateway Timeout
A 504 response generally indicates a timeout while waiting for the upstream connection or response.
Possible reasons include a slow application, blocked dependencies, an overloaded database, or an upstream network problem.
Timeout directives can influence this behavior, but simply increasing them does not necessarily solve the underlying issue.
For example, increasing proxy_read_timeout will not make an application execute an inefficient database query faster.
A useful diagnostic rule
When Nginx reports a gateway error, test the backend directly and inspect both the Nginx error log and application logs.
Do not restart the entire VPS before identifying which component has failed.
4. Mistake: Incorrect proxy_pass Paths
The proxy_pass directive controls where Nginx forwards requests.
A detail that frequently causes unexpected behavior is the URI component of the upstream URL, particularly the trailing slash.
Consider an application exposed under /app/.
These two configurations can produce different upstream request paths:
location /app/ {
proxy_pass http://127.0.0.1:3000;
}
location /app/ {
proxy_pass http://127.0.0.1:3000/;
}
In the first example, the original request URI is generally forwarded.
In the second example, Nginx replaces the matching /app/ prefix with /.
As a result, a request for /app/dashboard may arrive at the backend as /app/dashboard in the first configuration and /dashboard in the second.
Neither behavior is universally correct.
It depends on whether the application expects to operate at the domain root or under a URL prefix.
An incorrect choice can cause broken navigation, missing static files, and unexpected 404 responses.
Before changing proxy_pass, determine which paths the backend application expects.
5. Mistake: Missing or Incorrect Proxy Headers
When Nginx forwards requests, the backend application may need information about the original connection.
This can include the requested hostname, visitor IP address, and whether the public connection used HTTP or HTTPS.
Several common headers help communicate this information:
Host— the requested hostname.X-Real-IP— the client address as seen by Nginx.X-Forwarded-For— information about addresses in the proxy chain.X-Forwarded-Proto— the request protocol.
Without appropriate headers and application-side configuration, some applications may generate redirects to the wrong address.
For example, a visitor opens https://example.com, but the application redirects the browser to an internal HTTP URL or produces links using the wrong domain.
The problem may not be in the application’s routing logic itself. It may be caused by the way the reverse proxy communicates the original request information.
Be careful with forwarded client IP addresses
Forwarded headers also introduce a security consideration.
If the server is behind a CDN or external load balancer, Nginx may initially see the intermediary’s IP address rather than the actual visitor’s address.
Nginx can be configured to accept a real-client-IP header from trusted proxies.
However, trusting forwarded headers from arbitrary internet clients allows IP-address spoofing.
Only the correct intermediary addresses should be trusted, and the backend must apply its own trusted-proxy rules where necessary.
6. Mistake: Incorrect HTTPS and Redirect Configuration
HTTPS is now an expected part of most public web applications.
A common deployment uses Nginx to terminate HTTPS connections while forwarding requests to a backend over HTTP on a local or otherwise trusted network.
This configuration is valid when the connection between Nginx and the application is appropriately protected for the deployment environment.
However, mistakes in HTTPS handling can cause redirect loops or inconsistent application behavior.
Redirect loops
A typical example occurs when Nginx receives an HTTPS request but forwards it to the application using HTTP.
If the backend does not recognize that the original connection used HTTPS, it may repeatedly redirect the user to an HTTPS URL.
The browser returns through Nginx, and the process repeats.
Check the forwarded protocol headers and the application’s trusted-proxy configuration.
Certificate problems
TLS certificate errors should be investigated separately from reverse proxy connection errors.
Verify that the certificate covers the requested hostname, has not expired, and is served by the intended virtual host.
If a CDN is involved, distinguish the browser-to-CDN TLS connection from the CDN-to-origin connection.
A working public HTTPS connection does not automatically prove that the connection to the origin is configured securely.
7. Mistake: Forgetting WebSocket Support
Some applications require persistent, bidirectional communication rather than ordinary HTTP requests.
Common examples include:
- Real-time dashboards
- Browser-based terminals
- Chat applications
- Notification systems
- Interactive administration interfaces
These applications may use WebSocket connections.
A website can load its main HTML page correctly while its real-time functionality fails because WebSocket upgrade requests are not handled properly.
Traditional HTTP/1.1 WebSocket proxying requires the appropriate upgrade information to be passed to the backend.
Nginx does not automatically forward all hop-by-hop request headers.
For this reason, administrators commonly configure the Upgrade and Connection headers for WebSocket-capable locations.
Long-lived connections may also need timeout settings appropriate to the application’s activity.
If an application works when accessed directly but its WebSocket functionality fails behind Nginx, inspect the browser developer tools, upgrade handshake, backend logs, and reverse proxy configuration.
Do not assume that increasing timeouts alone will repair a missing protocol-upgrade configuration.
8. Mistake: Increasing Timeouts Without Investigating Performance
When a website responds slowly, increasing Nginx timeout values can appear to be an easy solution.
However, longer timeouts should match a legitimate application requirement.
Nginx provides several separate proxy timeout settings:
proxy_connect_timeout— time allowed to establish the upstream connection.proxy_send_timeout— timeout between operations while sending the request upstream.proxy_read_timeout— timeout between successive reads from the upstream.
These settings control different stages of communication.
For instance, an application that legitimately streams data may require a different timeout configuration from a conventional website.
But an application that becomes unresponsive because of exhausted database connections requires investigation of the application and database, not only Nginx.
Check CPU usage, available memory, disk performance, application logs, and database response times before increasing limits.
Excessively generous timeouts may also allow slow or stalled requests to occupy resources for longer than necessary.
9. Mistake: Exposing Backend Ports Publicly
One advantage of a reverse proxy is that backend services do not necessarily need direct public exposure.
For an application hosted on the same VPS, binding the service to 127.0.0.1 can restrict access to the local network environment.
Nginx then becomes the public entry point.
A common mistake is allowing the backend to listen on every network interface and opening its port in the firewall without a specific reason.
This can let visitors bypass Nginx entirely.
They may reach an application without the intended HTTPS enforcement, access restrictions, or other reverse-proxy controls.
Review listening sockets with:
sudo ss -lntp
Then check the firewall rules:
sudo ufw status verbose
Remember that cloud firewalls, hosting-provider filtering, and container port publishing can affect exposure independently of UFW.
Only expose backend ports when the application’s architecture requires them.
10. Mistake: Reloading Nginx Without Testing Configuration
Nginx configuration files can become difficult to manage when a server hosts multiple websites and applications.
A missing semicolon, incorrect include path, or unsupported directive can prevent a configuration from loading.
Before applying changes, always run:
sudo nginx -t
If the syntax check succeeds, the configuration can usually be applied with:
sudo systemctl reload nginx
Reloading is generally preferable to a full restart for ordinary configuration changes because it is designed to apply new settings gracefully.
However, a successful syntax test does not guarantee that the backend application is reachable or that the website behaves correctly.
After reloading, test the public hostname, application routes, and relevant functionality.
When investigating configuration conflicts, sudo nginx -T can help display the effective configuration assembled from included files.
For a complete installation and configuration walkthrough, including server blocks, upstream settings, request headers, WebSocket support, and logging, read How to Install Nginx on Ubuntu Server and Configure a Reverse Proxy.
A Practical Nginx Troubleshooting Workflow
When a reverse-proxied application becomes unavailable, follow a consistent diagnostic sequence.
Step 1: Check the Nginx service
sudo systemctl status nginx --no-pager
Confirm that the service is active and review recent errors.
Step 2: Validate the configuration
sudo nginx -t
Resolve syntax errors before making additional changes.
Step 3: Test the backend directly
curl -v http://127.0.0.1:3000/
Use the correct address and port for the application.
A failed direct request suggests that the problem exists before Nginx can successfully proxy it.
Step 4: Test Nginx locally
curl -v -H 'Host: example.com' http://127.0.0.1/
This helps test the intended HTTP virtual host without depending on public DNS. An HTTPS virtual host should also be tested with the appropriate hostname and TLS settings.
Step 5: Review logs
Check recent Nginx errors:
sudo tail -n 50 /var/log/nginx/error.log
If the website uses a separate error log, inspect that file as well.
Review backend service logs to determine whether requests are reaching the application and whether it is producing errors.
Step 6: Check external connectivity
If Nginx and the backend work locally but the public website is unreachable, investigate:
- DNS records
- UFW rules
- Hosting-provider firewall policies
- Public IP addresses and routing
- CDN and external proxy settings
- HTTP and HTTPS listener configuration
This separation helps identify whether the issue exists inside the VPS or elsewhere along the connection path.
Common Nginx Error Codes
| Error | What to investigate |
|---|---|
| 403 Forbidden | Access rules, filesystem permissions, missing index files |
| 404 Not Found | Requested application path, server block selection, proxy_pass URI handling |
| 413 Content Too Large | client_max_body_size and application upload limits |
| 502 Bad Gateway | Backend availability, upstream address, connection refusal, invalid response |
| 504 Gateway Timeout | Upstream connectivity, slow application, dependencies, timeout settings |
The same HTTP status can sometimes originate from Nginx, an upstream application, or another proxy.
Always use logs and direct tests to identify the actual source.
Final Thoughts
Nginx is a flexible reverse proxy for Ubuntu VPS hosting, but successful deployment requires understanding how several components interact.
The most common problems involve incorrect upstream addresses, broken URL routing, missing forwarded headers, HTTPS configuration, WebSocket handling, and backend performance.
A reverse proxy does not automatically make an application secure or reliable. It provides additional control over public access, routing, encryption, and request processing.
The best troubleshooting approach is to investigate each component separately.
Verify that the application works, confirm that Nginx can reach it, validate the proxy configuration, and only then investigate public networking and external services.
A clearly documented reverse proxy configuration makes future application deployments, maintenance, and troubleshooting substantially easier.