[Nginx] Chapter 4 Nginx Configuration Example - Reverse Proxy

4.1 Reverse proxy example 1 

Realization effect: use nginx reverse proxy, visit www.123.com and jump directly to localhost :8080

4.1.1 Experiment preparation _

  1. Start a tomcat, enter localhost :8080 in the browser address bar , and the following interface appears

Install tomcat on the liunx system, using the default port 8080

Put the tomcat installation file in liunx system/opt, unzip it

Enter the bin directory of tomcat, ./startup.sh starts the tomcat server 

Open ports for external access

firewall-cmd --add-port=8080/tcp --permanent

firewall-cmd --reload

View open port numbers

firewall-cmd --list-all

  1. linux native access

  1. Access the tomcat server through a browser in the windows system

  1. Map www.123.com  to 192.168.6.100 by modifying the local host file

  1. After the configuration is complete, we  access the Tomcat initial interface that appears in the first step  through www.123.com:8080 .

4.1.2 Reverse Proxy Example 1 Demonstration

So how to jump to the Tomcat initial interface just by entering www.123.com  ? The reverse proxy of nginx is used.

  • Add the following configuration to the nginx.conf configuration file

As configured above, we listen to port 80 and access the domain name www.123.com. If no port number is added, the default port is 80. Therefore, when accessing this domain name, it will jump to the l ocalhost :8080 path.

4.2 Reverse Proxy Example 2

Realization effect: Use nginx reverse proxy to jump to services on different ports according to the access path

The nginx listening port is 900 0 ,

Visit http://127.0.0.1:9000/edu/  to jump directly to 127.0.0.1:808 0

Visit http://127.0.0.1:9000/vod/  and jump directly to 127.0.0.1:808 1

4.2.1 Experiment preparation

The first step is to prepare two tomcats, one port 8080, one port 8081, and prepare the test page

webapps/edu/index.html

webapps/vod/index.html 

The second step is to modify the nginx configuration file and add server{} in the http block

4.2.2 Demonstration of reverse proxy instance 2

4.2.3 Description of location command

This directive is used to match URLs.

The syntax is as follows:

1. = : Before using the uri without regular expressions, the request string is required to be strictly matched with the uri . If the match is successful, the search will stop and the request will be processed immediately.

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

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

4. ^~: Before using the uri without regular expressions, the Nginx server is required to find the location with the highest matching degree between the identification uri and the request string, and immediately use this location to process the request instead of using the regular uri in the location block The request string for matching.

Note: If the uri contains a regular expression, it must be identified by ~ or ~*.

Guess you like

Origin blog.csdn.net/weixin_45481821/article/details/131407572