Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure location and proxy_pass paths in Nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to configure location and proxy_pass paths in Nginx, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

1. Nginx location basic configuration 1.1.The Nginx configuration file upstream test1 {server 127.0.0.1 upstream test1 8000;} upstream test2 {server 127.0.0.1 upstream test1 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 get the content of the request. This serves as the backend, that is, the backend of the proxy_pass agent.

#! / usr/bin/env pythonimport SimpleHTTPServerimport SocketServerPORT = 8000class GetHandler (SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET (self): print (self.headers) self.send_response ") 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 = GetHandlerhttpd = SocketServer.TCPServer ((", PORT), Handler) httpd.serve_forever () II, Test 2.1, Test location

End exists / and proxy_pass end exists /

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

Back-end content

What is printed:

Host: test1Content-Length: 0User-Agent: PostmanRuntime/7.26.8Accept: * / * Postman-Token: f2bfe770-4f44-4ee9-91c4-060f59dfb26cAccept-Encoding: gzip, deflate, br127.0.0.1-[10/Apr/2021 16:54:26] "POST / test.html HTTP/1.1" 200-

Small conclusion: if the proxy_pass address is added /, the actual request for test.com/user/test.html is http://test1/test.html.

2.2.Test location

End exists / and proxy_pass end does not exist /

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

Back-end 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-

Small conclusion: if the proxy_pass address is not added, the actual request for test.com/user/test.html is http://test1/user/test.html.

2.3.Test 3 location

No end / and no end / for 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

Back-end 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.No location.

End / and proxy_pass plus end /

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

Back-end 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, the end of location

There is a path at the end of / proxy_pass, with / at the end

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

Back-end 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 / /test.html HTTP/1.1" 200-

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

2.6.The end of location

There is a path at the end of / proxy_pass, 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/;}

Request url

Test.com/user/test.html

Back-end 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 / test.html HTTP/1.1" 200-

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

III. Summary

Serial number access URLlocation configuration proxy_pass configuration request received by the backend Note 1test.com/user/test.html/user/ http://test1//test.html

2test.com/user/test.html/user/ http://test1/user/test.html

3test.com/user/test.html/user http://test1/user/test.html

4test.com/user/test.html/user http://test1///test.html

5test.com/user/test.html/user/ http://test1////test.html

6test.com/user/test.html/user/ http://test1//test.html

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

In the daily deployment of web websites, nginx's proxy_pass reverse proxy is often used. There is a configuration to be clear: when configuring proxy_pass

When a / appears after the subsequent upstram_name, which is equivalent to the absolute root path, the nginx will not proxy part of the matching path in the location

If there is no /, the matching part of the path is also given to the agent.

These are all the contents of the article "how to configure location and proxy_pass paths in Nginx". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report