A brief discussion on the magical slash in nginx reverse proxy

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes in location and proxy_pass can cause various problems. Sometimes one more or one less slash will cause completely different results. Therefore, we specially arranged and combined the situations with and without slashes after location and proxy_pass, and conducted a complete test to find out the principle and improve our posture level~

0. Environmental Information

Two nginx servers

nginx A: 192.168.1.48

nginx B: 192.168.1.56

1. Test Method

Configure different rules in nginx A, and then request nginx A: http://192.168.1.48/foo/api

Observe the request received by nginx B by viewing the $request field in the log

2. Test process and results

Case 1

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/;
}

Request received by nginx B: /api

Case 2

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/;
}

Request received by nginx B: //api

Case 3

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/;
}

Request received by nginx B: /foo/api

Case 4

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/;
}

Request received by nginx B: /foo/api

Case 5

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/bar/;
}

Request received by nginx B: /bar/api

Case 6

nginx A configuration:

location /foo {
  proxy_pass http://192.168.1.56/bar/;
}

Request received by nginx B: /bar//api

Case 7

nginx A configuration:

location /foo/ {
  proxy_pass http://192.168.1.56/bar;
}

Request received by nginx B: /barapi

Case 8

nginx A configuration:

location /foo {
  proxy_pass http://192.168.1.56/bar;
}

Request received by nginx B: /bar/api

Are you dizzy after seeing this? Actually, there is a pattern.

Now arrange these cases in a table, and the result shows the request received by nginx B

Table 1

Case location proxy_pass result
1 /foo/ http://192.168.1.48/ /api
2 /foo http://192.168.1.48/ //api
3 /foo/ http://192.168.1.48 /foo/api
4 /foo http://192.168.1.48 /foo/api

Table 2

Case location proxy_pass result
5 /foo/ http://192.168.1.48/bar/ /bar/api
6 /foo http://192.168.1.48/bar/ /bar//api
7 /foo/ http://192.168.1.48/bar /barapi
8 /foo http://192.168.1.48/bar /bar/api

3. Analysis

Original request path: This article uses the same name as "/foo/api"

location: The location column in the table above

proxy_pass: The proxy_pass column in the table above

New request path: the string after nginx processes the original request path

Focus on analyzing proxy_pass, which can be divided into 3 forms

Then, according to whether the string is followed by ip:port, it is classified into two categories. "/" is also a string, so 1 is classified into one category, and 2 and 3 are classified into one category. The following explains these two categories.

When the proxy_pass ip:port is not followed by a string, nginx will forward the original request path intact to the next nginx, as in cases 3 and 4.

When a string is added after the ip:port of proxy_pass, nginx will remove the location from the original request path, and then concatenate the remaining string to proxy_pass to generate a new request path, and then forward the new request path to the next station nginx (the above situation is actually the same as this one, except that the removed string is an empty string~~)

Let’s take the most confusing example: Case 7. The ip:port of proxy_pass is followed by the string "/bar", so the location: "/foo/" is removed from the original request path: "/foo/api" and becomes "api". Then "api" is concatenated to proxy_pass: http://192.168.1.48/bar to generate a new request url: "http://192.168.1.48/barapi", so the request received by nginx at the next stop is "/barapi".

Case 6: The ip:port of proxy_pass is followed by the string "/bar/", so location: "/foo" is removed from the original request path "/foo/api" and becomes "/api", and then "/api" is concatenated to proxy_pass: http://192.168.1.48/bar/ to generate a new request path: "http://192.168.1.48/bar//api", so the request received by nginx at the next stop is /bar//api.

The same can be applied to other cases. Now I finally understand it and I don’t have to be confused anymore.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A brief discussion on Nginx seven-layer reverse proxy and load balancing
  • Detailed explanation of Nginx reverse proxy to implement Kibana login authentication function
  • Detailed explanation of several ways to write Nginx dynamic DNS reverse proxy
  • How to use Nginx as a reverse proxy for Node.js
  • Nginx reverse proxy secondary domain name binding method and precautions
  • CentOS method of implementing load balancing based on nginx reverse proxy
  • Two ways to implement nginx https reverse proxy tomcat
  • Sample code for implementing IP access diversion through Nginx reverse proxy
  • PHP uses Nginx to implement reverse proxy
  • Nginx reverse proxy and cache and cache clearing method

<<:  MySQL Daemon failed to start error solution

>>:  WeChat applet canvas implements signature function

Recommend

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...

How to install mysql via yum on centos7

1. Check whether MySQL is installed yum list inst...

You may need a large-screen digital scrolling effect like this

The large-screen digital scrolling effect comes f...

WeChat applet selects the image control

This article example shares the specific code for...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

Docker installation and configuration image acceleration implementation

Table of contents Docker version Install Docker E...

A brief discussion on the correct approach to MySQL table space recovery

Table of contents Preliminary Notes Problem Repro...

Detailed explanation of memory management of MySQL InnoDB storage engine

Table of contents Storage Engine Memory Managemen...

Summary of how to use bootstrap Table

This article shares with you how to use bootstrap...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

MySQL stored procedures and common function code analysis

The concept of mysql stored procedure: A set of S...

sql script function to write postgresql database to implement parsing

This article mainly introduces the sql script fun...

Detailed Analysis of the Differences between break and last in Nginx

Let's talk about the difference first last, t...