Backend Development 12 min read

Understanding Nginx Reverse Proxy: Configuration and Practical Examples

This article explains the concepts of forward and reverse proxy, walks through the structure of Nginx configuration files, and provides step‑by‑step code examples for setting up reverse proxy, path‑based routing, and location directives for backend development.

Top Architect
Top Architect
Top Architect
Understanding Nginx Reverse Proxy: Configuration and Practical Examples

1. Introduction

Before diving in, you need a Linux environment with Nginx installed. This guide focuses on learning Nginx reverse proxy configuration.

2. What is a Reverse Proxy?

A forward proxy is used by clients inside a LAN to access the Internet through a proxy server. In contrast, a reverse proxy sits in front of one or more backend servers; clients are unaware of it and do not need any configuration. The reverse proxy receives requests, forwards them to the appropriate backend server, and returns the response, effectively hiding the real server IP.

3. Nginx Configuration File

The Nginx configuration is divided into three main sections: the global block, the events block, and the http block.

Global Block

worker_processes  1;

This block sets directives that affect the whole Nginx process, such as the number of worker processes, user, PID path, and log locations.

Events Block

events {
    worker_connections  1024;
}

The events block controls how Nginx handles network connections, including the maximum number of simultaneous connections per worker.

http Block

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

The http block contains most of the functional configuration, including virtual hosts, proxy settings, caching, and logging.

4. Reverse Proxy Configuration Examples

Example 1: Simple Reverse Proxy

Goal: Access www.123.com and have Nginx forward the request to 127.0.0.1:8080 .

server {
    listen       80;
    server_name  192.168.17.129;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass  http://127.0.0.1:8080;
    }
}

This configuration listens on port 80 and proxies all traffic to the backend service at 127.0.0.1:8080 .

Example 2: Path‑Based Routing

Goal: Use Nginx on port 9001 to route /edu/ to 127.0.0.1:8080 and /vod/ to 127.0.0.1:8081 .

server {
    listen       9001;
    server_name  192.168.17.129;

    location ~ /edu/ {
        proxy_pass  http://127.0.0.1:8080;
    }

    location ~ /vod/ {
        proxy_pass  http://127.0.0.1:8081;
    }
}

When a request arrives, Nginx matches the URI and forwards it to the corresponding backend service.

5. location Directive Syntax

The location directive matches request URIs. Its syntax is:

location [ = | ~ | ~* | ^~ ] uri { }

= : Exact match; stops further searching.

~ : Case‑sensitive regular expression match.

~* : Case‑insensitive regular expression match.

^~ : Prefix match without regular expression; if matched, Nginx stops searching for regex locations.

The location with the highest match priority is used to process the request.

6. Full Nginx Configuration File

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

The article concludes with a complete configuration example and references to additional interview questions and resources.

backend developmentload balancingconfigurationnginxhttp serverReverse Proxy
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.