DEV Community

Cover image for NGINX Proxying Requests from One Server to Another Server
Suresh Ramakrishnan
Suresh Ramakrishnan

Posted on • Edited on

NGINX Proxying Requests from One Server to Another Server

Description

This guide explains how to configure Nginx to forward requests from one server to another while maintaining the correct URL structure. It covers key concepts such as using proxy_pass, handling path modifications, and ensuring smooth request forwarding. Additionally, it highlights common pitfalls like incorrect trailing slashes and how to avoid them. By following this guide, you can efficiently set up a reverse proxy for static content, APIs, or any other resources across different servers.

Configuration Breakdown

location ~ ^/cdn {     
    set $backend https://cloud.cdn.com; 
    proxy_pass $backend;
}
Enter fullscreen mode Exit fullscreen mode

How It Works

1. Matching Requests for /static

The location ~ ^/static block applies to any request where the path starts with /static.

The ~ indicates that a regular expression is used (^/static ensures it matches from the beginning of the path).

2. Setting a Backend Variable

set $backend https://cloud.cdn.com;
Enter fullscreen mode Exit fullscreen mode

Defines a variable $backend with the value https://cloud.cdn.com.

This provides flexibility for modifying the backend dynamically in the future.

3. Proxying Requests

proxy_pass $backend;
Enter fullscreen mode Exit fullscreen mode

This forwards requests matching /static to https://cloud.cdn.com.

Example:

Request: https://sitea.com/static/image.png

Proxies to: https://cloud.cdn.com/static/image.png

The response is then returned to the original client.

Possible Issues & Fixes

1. Missing Trailing Slash in proxy_pass

If proxy_pass is set to https://cloud.cdn.com without a trailing slash, it behaves as expected:

The request path remains unchanged.

Example:

Request: https://sitea.com/static/image.png

Proxies to: https://cloud.cdn.com/static/image.png(✅ Expected)

2. Adding a Trailing Slash (/) in proxy_pass

If proxy_pass is https://cloud.cdn.com/, Nginx replaces /static with /:

proxy_pass https://cloud.cdn.com/;
Enter fullscreen mode Exit fullscreen mode

Example:

Request: https://sitea.com/static/image.png

Proxies to: https://cloud.cdn.com/image.png (🚫 Incorrect if /static/ is required)

Correct Approach (If /static Must Be Included)

If cloud.cdn.com expects /static in the request, ensure:

proxy_pass https://cloud.cdn.com;
Enter fullscreen mode Exit fullscreen mode

This keeps /static in the proxied request.

If cloud.cdn.com does not need /static, use:

proxy_pass https://cloud.cdn.com/;
Enter fullscreen mode Exit fullscreen mode

This strips /static before forwarding.

Summary

Matches any request starting with /static.

Proxies to https://cloud.cdn.com.

Ensure correct trailing slash usage in proxy_pass to maintain the intended request path.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)