Nginx Learning 3: Reverse Proxy Example

Nginx configuration example - reverse proxy 1

Target

Open the browser, enter the address www.123.com in the browser address bar, and jump to the main page of the tomcat of the liunx system

Ready to work

After downloading tomcat on the official website, we directly put the compressed package of tomcat in the corresponding directory to compile and decompress it, and then enter the bin directory of tomcat and use the ./startup.shcommand to start tomcat, as shown in the figure below:
insert image description here
Note: You must open port 8080 of the system firewall to access tomcat The main page of , I am too lazy to set it here, so I just turned off the firewall of the virtual machine...

The IP address of the virtual machine plus port 8080 accesses the main page of tomcat, as shown below:
insert image description here
We can also use the following command to view the startup log of tomcat

tail -f catalina.out

insert image description here

host configuration

The first step is to configure the corresponding relationship between the domain name and ip in the host file of the windows system

insert image description here

Map www.123.com to the IP address of our virtual machine by modifying the local hosts file (pretend we have a domain name...)

insert image description here

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

Configure the nginx.conf configuration file

insert image description here
As configured above, we listen on port 80, and the access domain name is www.123.com. If no port number is added, the default is port 80, so when accessing this domain name, it will jump to the 127.0.0.1:8080 path.

Then we enter www.123.com in the host browser and visit the tomcat home page, as shown below:
insert image description here

problems encountered

Note: Some people may have access failure after the last step is completed, that is, entering www.123.com to access the welcome page of nginx, this is because we did not specify the configuration file to start nginx, we need to specify the configuration file to restart nginx again, as follows:

# 指定配置文件重启nginx
# nginx的启动目录/nginx -s reload -c /nginx的配置文件目录/nginx.conf
/usr/local/nginx/sbin/nginx -s reload -c /home/centos7-2/Nginx/nginx-1.20.1/conf/nginx.conf

# 指定配置文件启动nginx
# nginx的启动目录/nginx -c /nginx的配置文件目录/nginx.conf
/usr/local/nginx/sbin/nginx -c /home/centos7-2/Nginx/nginx-1.20.1/conf/nginx.conf

Nginx configuration example - reverse proxy 2

Target

Using nginx reverse proxy, jump to services with different ports according to the access path. The
nginx listening port is 9001,
access http://192.168.126.131:9001/edu/ and directly jump to 127.0.0.1:8080
to access http:/ /192.168.126.131:9001/com/ jumps directly to 127.0.0.1:8081

Ready to work

(1) Prepare two tomcat servers, one port 8080 and one port 8081, as shown in the figure below:
insert image description here
After preparing the two tomcat servers, we modify the ports of the two servers, as shown in the figure below:

insert image description here

Find the configuration file of tomcat and modify the content in server.xml.
First, change the shutdown port to 8006, as shown in the figure below:
insert image description here
and then change the running port of tomcat to 8081, as shown in the figure below:
insert image description here
In short, change all the same ports as another tomcat server to avoid port conflicts.

Finally, go to the bin directory and start both tomcats, as shown in the figure below:
insert image description here
Check again whether the startup is successful, as shown in the figure below:
insert image description here
insert image description here
We can see that both tomcat servers have been successfully started.

(2) Create folders and test pages
Then we create test folders edu, com and test pages a.html, b.html under the webapps directory under the two tomcat servers, as shown below:
insert image description here
insert image description here
Let's test whether the page is created successfully ,As shown below:
insert image description here
insert image description here

Specific placement

(1) Find the nginx configuration file and configure the reverse proxy, as shown below:

insert image description here

Among them, 9001 is the port to be monitored by nginx, and server_name is the ip address of the machine. ~ /edu/The "~" in is a form of regular expression, which means that when there is "eud" in the path, it will be forwarded to the corresponding path.

final test

insert image description here

insert image description here

location configuration instructions

This directive is used to match URLs.
The syntax is as follows:

location [= | ~ | ~* | ^~] uri{
    
    

}
  1. = : Before being used for uri without regular expressions, the request string is required to strictly match the uri. If the match is successful, it will stop continuing to search down and process the request immediately.
  2. ~ : Used to indicate that the uri contains a regular expression and is case sensitive.
  3. ~* : Used to indicate that the uri contains regular expressions and is not case-sensitive.
  4. ^~ : Before being used for uri without regular expressions, the Nginx server is required to find the location with the highest match between the identifier uri and the request string, and then use this location to process the request immediately instead of using the regular uri and request in the location block. String to do matching.

Note: If the uri contains regular expressions, it must be marked with ~ or ~*.



PS: You can go to my personal blog to see more content
Personal blog address: Xiaoguan classmate's blog

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/120475730