Summary of Nginx location and proxy_pass path configuration issues

Summary of Nginx location and proxy_pass path configuration issues

This article tests whether the location match ends with / and whether the proxy_pass match ends with / , and completely restores the entire test process. Help understand the specific situation.

1. Basic configuration of Nginx location

1.1 Nginx configuration file

upstream test1{
server 127.0.0.1:8000;
}
upstream test2{
server 127.0.0.1:8000;
}
server{
	server_name test.com;
	listen 80;
        access_log /usr/local/openresty/nginx/logs/test.com_access.log latest;
        error_log /usr/local/openresty/nginx/logs/test.com.log error;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 3s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;
	
        location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}
        location / {
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://test2/;
        }
}

1.2 Python script

python2 can run

This script is used to obtain the request content. This serves as the backend, which is the backend of the proxy_pass proxy.

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer

PORT = 8000

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.headers)
        self.send_response(200, "")
    def do_POST(self):
        print(self.headers)
        content_length = self.headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0
        print(self.rfile.read(length))
        self.send_response(200, "")

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()

2. Testing

2.1、Test location

There is / at the end and there is / at the end of proxy_pass

The nginx configuration is as follows

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: f2bfe770-4f44-4ee9-91c4-060f59dfb26c
Accept-Encoding: gzip, deflate, br


127.0.0.1 - - [10/Apr/2021 16:54:26] "POST /test.html HTTP/1.1" 200 -

Conclusion: If / is added to the proxy_pass address, the actual request for requesting test.com/user/test.html is http://test1/test.html .

2.2. Test location

There is / at the end and there is no / at the end of proxy_pass

The nginx configuration is as follows

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: e33d0a2c-1965-4152-b87c-94fca50f2899
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:57:18] "POST /user/test.html HTTP/1.1" 200 -

Conclusion: If the proxy_pass address does not have / , the actual request to test.com/user/test.html is http://test1/user/test.html

2.3. Test three locations

No trailing / and proxy_pass no trailing /

The nginx configuration is as follows

 location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 31cd33c6-4c95-41b5-a095-28cdc7113dcd
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 16:59:34] "POST /user/test.html HTTP/1.1" 200 -

Request test.com/user/test.html The actual request is http://test1/user/test.html

2.4, location not added

The last / and proxy_pass plus the last /

The nginx configuration is as follows

  location /user {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: d0f4b83f-6482-41ba-8a01-c059eececc2d
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:00:21] "POST //test.html HTTP/1.1" 200 -

Request test.com/user/test.html The actual request is http://test1//test.html

2.5. Location end

There is a / proxy_pass at the end of the path, and add / at the end

The nginx configuration is as follows

   location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha/;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 6447cf0b-5988-4f96-81a4-2b621fe32604
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:03:27] "POST /haha/test.html HTTP/1.1" 200 -

Request test.com/user/test.html The actual request is http://test1/haha/test.html

2.6. End of location

There is a / proxy_pass at the end of the path, and there is no / at the end

The nginx configuration is as follows

 location /user/ {
			proxy_set_header Connection "";
        	proxy_http_version 1.1;
			proxy_pass http://test1/haha;
		}

Request URL

test.com/user/test.html

Backend content

What is printed:

Host: test1
Content-Length: 0
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: 32fb2a50-1e7c-4131-9804-1828e21ca841
Accept-Encoding: gzip, deflate, br

127.0.0.1 - - [10/Apr/2021 17:05:03] "POST /hahatest.html HTTP/1.1" 200 -

Request test.com/user/test.html The actual request is http://test1/hahatest.html

Conclusion

Serial number Access URL Location configuration proxy_pass configuration Requests received by the backend Remark
1 test.com/user/test.html /user/ http://test1/ /test.html
2 test.com/user/test.html /user/ http://test1 /user/test.html
3 test.com/user/test.html /user http://test1 /user/test.html
4 test.com/user/test.html /user http://test1/ //test.html
5 test.com/user/test.html /user/ http://test1/haha/ /haha/test.html
6 test.com/user/test.html /user/ http://test1/haha /hahatest.html

Note that the backend in the table above refers to the web service corresponding to the Python script.

In daily web site deployment, nginx 's proxy_pass reverse proxy is often used. There is a configuration that needs to be clarified: when configuring proxy_pass ,

  • When / appears after upstram_name , it is equivalent to the absolute root path, and nginx will not proxy the path part matched in location ;
  • If there is no / , the matching path part will also be proxied.

This is the end of this article about the detailed configuration of Nginx location and proxy_pass path. For more relevant Nginx location and proxy_pass path configuration content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of various usages of proxy_pass in nginx
  • Detailed explanation of the difference between url ending with / and without / in nginx proxy_pass configuration
  • Proxy_pass method in multiple if in nginx location
  • Implementation of proxy_pass in nginx reverse proxy
  • Differences between proxy_pass in two modules in nginx

<<:  How to embed other web pages in a web page using iframe

>>:  25 CSS frameworks, tools, software and templates shared

Recommend

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

MySql knowledge points: transaction, index, lock principle and usage analysis

This article uses examples to explain the princip...

Summary of MySQL basic common commands

Table of contents MySQL basic common commands 1. ...

CentOS 8 installation diagram (super detailed tutorial)

CentOS 8 is officially released! CentOS fully com...

Springboot+Vue-Cropper realizes the effect of avatar cutting and uploading

Use the Vue-Cropper component to upload avatars. ...

MySQL Query Cache and Buffer Pool

1. Caches - Query Cache The following figure is p...

Transplanting the mkfs.vfat command in busybox under Linux system

In order to extend the disk life for storing audi...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

How to enable MySQL remote connection

For security reasons, MySql-Server only allows th...

Detailed explanation of JavaScript timers

Table of contents Brief Introduction setInterval ...

Add a startup method to Linux (service/script)

Configuration file that needs to be loaded when t...

How to regularly clean up docker private server images

Using CI to build docker images for release has g...

Detailed explanation of the use of CSS pointer-events attribute

In front-end development, we are in direct contac...

The use of vue directive v-bind and points to note

Table of contents 1. v-bind: can bind some data t...