Detailed explanation of the murder caused by a / slash in Nginx proxy_pass

Detailed explanation of the murder caused by a / slash in Nginx proxy_pass

background

An nginx server module needs to proxy to two servers, so different paths of location are used to distinguish and forward to different servers.

This is what it was written at the beginning

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

location /index {
   proxy_pass http://server2/;
}

But I forgot that there is a service path /indexNew on server1, so it was proxied to server1, resulting in a 404 problem. Then I urgently modified the configuration as follows:

location /indexNew {
   proxy_pass http://server1/;
}

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

location /index {
   proxy_pass http://server2/;
}

Problem phenomenon

As a result, the request reaches server1, but the error message is POST not supported.

{
	"status": 500,
	"message": "http://172.28.72.117/-Request method 'POST' not supported",
	"result": {}
}

This was the error returned by the application at that time. I checked nginx and there was no error either. It was very strange. I saw in the code that /indexNew was indeed the POST method, so why did it report an error saying it was not supported?

First, here is the matching order of various location writing methods in nginx:

analyze

There is no error in the nginx log, so try to capture the packet from nginx to the application

Capture packets using the tcpdump command

tcpdump -w dataAll_normal.pcap -i eth0 -s0 port 8888

Similar to the above command to capture the packet, and then through wireshark, it is found that no http traffic packets related to /indexNew are found at all.

Try to modify the location as follows

location /indexNew {
   proxy_pass http://server1;
}

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

location /index {
   proxy_pass http://server2/;
}

The only difference is that the last slash / in proxy_pass of /indexNew is removed. Continue to capture packets and find that the /indexNew packet can be searched

This indicates that the modification is correct.

Continue to change the error back and try to capture the package, but still can't search for the /indexNew package, then remote debug the application through IDE

I found that there was no /indexNew in the URL of the application, so of course I couldn’t find it in the wireshark package. . .

This is because when nginx forwards the application, the access path is only / .

The interface with the request path / in the project is indeed the GET method

Take a closer look at the syntax of proxy_pass in location. It is indeed like this. There is a / at the end of proxy_pass, which will remove the path in the matching location and intercept the subsequent URL PATH for forwarding.

Therefore, we must pay attention to the meaning of the last / in proxy_pass and use it with caution. It will change the path request information instead of 100% information forwarding.

This is the end of this article about the bloody case caused by a / slash in Nginx proxy_pass. For more related Nginx proxy_pass slash content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to the problem of two slashes // appearing after the domain name when nginx is configured for domain name access
  • Problems with nginx URLs automatically adding slashes and 301 redirects
  • How to solve the problem of WordPress path not automatically adding slashes in Nginx server
  • Solution to Nginx backslash automatic decoding problem
  • Detailed explanation of slash (/) in nginx

<<:  Detailed explanation of how to manually deploy a remote MySQL database in Linux

>>:  JS realizes picture digital clock

Recommend

Vue.js implements music player

This article shares the specific code of Vue.js t...

Basic use of subqueries in MySQL

Table of contents 1. Subquery definition 2. Subqu...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

Nginx content cache and common parameter configuration details

Use scenarios: The project's pages need to lo...

How to process blob data in MySQL

The specific code is as follows: package epoint.m...

CSS to achieve zoom in and out close button (example code)

This effect is most common on our browser page. L...

Vue implements simple data two-way binding

This article example shares the specific code of ...

Introduction to building a DNS server under centos7

Table of contents 1. Project environment: 2: DNS ...

MYSQL uses Union to merge the data of two tables and display them

Using the UNION Operator union : Used to connect ...

Ubuntu starts the SSH service remote login operation

ssh-secure shell, provides secure remote login. W...

Mysql keeps the existing content and adds content later

This command modifies the data table ff_vod and a...

Tutorial on installing JDK Tomcat MySQL on Linux (remote access using Mac)

One environment Alibaba Cloud Server: CentOS 7.4 ...

Detailed explanation of Linux text processing command sort

sort Sort the contents of a text file Usage: sort...