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 reverse proxy for nginx

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

Share

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

This article mainly shows you "nginx how to configure reverse proxy", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "nginx how to configure reverse proxy" this article.

Nginx configuration instance-reverse proxy reverse proxy instance 1

Virtual machine IP:192.168.116.129

Effect: use nginx reverse proxy to access www.123.com to jump directly to the 192.168.116.129 virtual machine of 192.168.116.129

Experimental code

1) launch a tomcat, and enter 192.168.116.129 8080 in the browser address bar, and the following interface appears

2) Map www.123.com to 192.168.116.129 by modifying the local host file

After the configuration is complete, we can access the Tomcat initial interface that appears in the first step through www.123.com:8080. So how can you just type www.123.com to jump to the Tomcat initial interface? The reverse proxy of nginx is used.

3) add the following configuration to the nginx.conf configuration file

Note: after modifying the configuration file, you need to restart nginx

As configured above, we listen on port 80, and the access domain name is www.123.com. If the port number is not added, the default value is port 80, so when accessing the domain name, we will jump to the 127.0.0.1 8080 path. Enter www.123.com on the browser and the result is as follows:

Reverse proxy example 2

Virtual machine ip:192.168.116.129

Effect: use nginx reverse proxy to jump to different ports according to the access path. The nginx listening port is 9001 in the service.

Visit the http://192.168.116.129:9001/edu/ and jump directly to 192.168.116.129.0.0.1purl 8080

Visit http://192.168.116.129:9001/vod/ and jump directly to 192.168.116.129VR 8082

Experimental code

1. Preparatory work

(1) prepare two tomcat servers, one port 8080 and one port 8082

(2) create folders and test pages

2. Specific matching

Modify the configuration file of nginx

Add server {} to the http block

Server {

Listen 9001

Server_name localhost

Location ~ / edu/ {

Proxy_pass http://localhost:8080;

}

Location ~ / dev/ {

Proxy_pass http://localhost:8082;

}

}

Restart

Location instruction description

The syntax is as follows:

1. =: before using uri without regular expressions, the request string is required to match the uri exactly. If the match is successful, stop searching down and process the request immediately.

2. ~: used to indicate that uri contains regular expressions and is case-sensitive.

3. ~ *: used to indicate that uri contains regular expressions and is not case-sensitive.

4. ^ ~: before using uri without regular expressions, the Nginx server is required to find the location that identifies the uri and has the highest matching degree with the request string, and immediately uses this location to process the request instead of using the regular uri in the location block to match the request string.

Note: if uri contains regular expressions, it must be identified by ~ or ~ *.

I have changed one line of configuration and will make the following changes.

Interested friends can try (guess the place I changed)

These are all the contents of the article "how to configure reverse proxy for 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