Operations 4 min read

Understanding Nginx Proxy Pass Path Matching and Configuration Details

This article explains how the presence or absence of a trailing slash in an Nginx proxy_pass directive affects request routing, demonstrates correct configurations with examples, and provides additional proxy and upstream settings for reliable backend forwarding.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Understanding Nginx Proxy Pass Path Matching and Configuration Details

This article explains the importance of the trailing slash in an nginx proxy_pass directive when using a ^~ location match, showing how it changes the way the matched path is forwarded.

Example with a trailing slash:

location ^~ /hahashen/ {
    proxy_cache js_cache;
    proxy_set_header Host js.test.com;
    proxy_pass http://js.test.com/;
}

# Request http://servername/hahashen/test.html is proxied to http://js.test.com/test.html

Example without a trailing slash:

location ^~ /hahashen/ {
    proxy_cache js_cache;
    proxy_set_header Host js.test.com;
    proxy_pass http://js.test.com;
}

# Request http://servername/hahashen/test.html is proxied to http://js.test.com/hahashen/test.html

Using rewrite to achieve the desired slash behavior:

location ^~ /hahashen/ {
    proxy_cache js_cache;
    proxy_set_header Host js.test.com;
    rewrite /hahashen/(.+)$ /$1 break;
    proxy_pass http://js.test.com;
}

Additional configuration details include defining an upstream group and a server block with extensive proxy settings:

upstream app {
    server 192.168.20.40:9081 max_fails=3 fail_timeout=15s;
    server 192.168.20.39:9081 max_fails=3 fail_timeout=15s;
}

server {
    listen 80;
    server_name image.hahashen.com;
    access_log /data/nginx/logs/hahashen-access.log main;
    error_log /data/nginx/logs/hahashen-error.log;
    location / {
        proxy_pass http://app;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 600;
        proxy_buffer_size 256k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_max_temp_file_size 128m;
    }
    location /api/ {
        proxy_pass http://blog.hahashen.com;
    }
    location /sub/ {
        proxy_pass http://dt.hahashen.com/;
    }
}

Explanation of request routing:

Requests to http://image.hahashen.com are load‑balanced between 192.168.20.40:9081 and 192.168.20.39:9081 .

Requests to http://image.hahashen.com/api/ are proxied to http://blog.hahashen.com/api/ .

Requests to http://image.hahashen.com/sub/ are proxied to http://dt.hahashen.com/ .

backendoperationsconfigurationnginxproxy_pass
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

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.