SoFunction
Updated on 2025-03-04

Summary of the use of proxy_pass in Nginx

proxy_passis a very important directive in Nginx to proxy requests to the backend server. This article will introduce in detailproxy_passBasic usage, configuration examples, and some advanced usages of  .

1. Basic concepts

1.1 proxy_pass Overview

proxy_passInstructions are used to forward requests to the backend server. It can be used in HTTP and Stream modules, handling HTTP requests and TCP/UDP traffic respectively.

1.2 Syntax

proxy_pass URL;
  • URL: The address of the backend server, which can be an HTTP/HTTPS address or a TCP/UDP address.

1.3 Use scenarios

  • HTTP Proxy: Forward HTTP requests to the backend server.
  • Stream Agent: Forward TCP/UDP traffic to the backend server.

2. Basic usage

2.1 HTTP Proxy

2.1.1 Basic examples

server {
    listen 80;
    server_name ;

    location / {
        proxy_pass http://backend_server;
    }
}

In this example, all accessesRequests will be forwarded tobackend_server

2.1.2 Absolute root path vs relative path

Absolute root path:existproxy_passThe URL behind is slashed/End, indicating the absolute root path.

location /proxy/ {
    proxy_pass http://127.0.0.1/;
}

For example, visit/proxy/Will be forwarded tohttp://127.0.0.1/

Relative path:existproxy_passThe URL behind is not slashed/End, indicating the relative path.

location /proxy/ {
    proxy_pass http://127.0.0.1;
}

For example, visit/proxy/Will be forwarded tohttp://127.0.0.1/proxy/

2.2 Stream Agent

stream {
    upstream backend {
        server 127.0.0.1:8080;
    }

    server {
        listen 12345;
        proxy_pass backend;
    }
}

In this example, all connections are12345The TCP traffic on the port will be forwarded to127.0.0.1:8080

3. Advanced usage

3.1 Regular Match

whenlocationWhen using regular expressions,proxy_passCannot include the URI part.

location ~ /testc {
    proxy_pass http://127.0.0.1:8801;
}

If the URI part is included, it will cause configuration file parsing errors:

location ~ /testd {
    proxy_pass http://127.0.0.1:8801/;  # mistake}

3.2 Variable usage

Variables can be used to generate forwarding addresses dynamically.

location /novel/ {
    proxy_pass http://book-server/books$request_uri;
}

For example, visit/novel/?page=3Will be forwarded tohttp://book-server/books/novel/?page=3

3.3 Redirection

Nginx will automatically perform 301 redirects in some cases, for example, when the requested URI is not slashed/ended, but Nginx thinks it is a directory.

location /films/nature/ {
    proxy_pass http://film-server;
}

If visit/films/nature, Nginx will return 301 and redirect to/films/nature/

3.4 Exact Match

Exact matches can be used to avoid 301 redirects.

location /films/nature/ {
    proxy_pass http://film-server;
}

location = /films/nature {
    proxy_pass http://film-server;
}

3.5 if statement

existlocationUsed inifWhen a statement,proxy_passCannot include the URI part.

location /google {
    if ($geoip_country_code ~ (RU|CN)) {
        proxy_pass ;
    }
}

3.6 limit_except

existlimit_exceptUsed inproxy_pass, the URI part cannot be included.

location /yongfu/ {
    proxy_pass http://unix:/tmp/:/uri/;

    limit_except PUT DELETE {
        proxy_pass http://127.0.0.1:9080;
    }
}

4. Actual cases

4.1 Forwarding to multiple backend servers

upstream backend_servers {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

server {
    listen 80;
    server_name ;

    location / {
        proxy_pass http://backend_servers;
    }
}

4.2 Forwarding to different paths

server {
    listen 80;
    server_name ;

    location /api/ {
        proxy_pass http://api_server/;
    }

    location /static/ {
        proxy_pass http://static_server/;
    }
}

4.3 Forwarding to Unix Domain Socket

server {
    listen 80;
    server_name ;

    location / {
        proxy_pass http://unix:/tmp/:/uri/;
    }
}

This is the article about the summary of the use of proxy_pass in Nginx. For more information about the use of proxy_pass in Nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!