Nginx (5): Reverse proxy for nginx configuration examples

Example one

1. Achieve the effect

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

2. Preparation

(1) Install tomcat on the linux system and use the default port 8080.

  • Put the tomcat installation file in the linux system and decompress it
#解压tomcat(采用系统自带JDK)
cd /usr/src
tar -xvf apache-tomcat-7.0.70.tar.gz 

#启动tomcat
cd /usr/src/apache-tomcat-7.0.70.tar.gz/bin
./startup.sh

(2) Ports open to the outside world

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

#查看已经开放的端口号
firewall-cmd --list-all

(3) Access the tomcat server through a browser in the windows system
Insert picture description here

3. Access process analysis

Insert picture description here

4. Specific placement

The first step is to configure the corresponding relationship between the domain name and ip in the host file of the windows system
Insert picture description here
(1) Add content in the host file. The
Insert picture description here
second step is to configure the request forwarding configuration (reverse proxy configuration) in the nginx configuration file.
Insert picture description here
After the configuration is complete, save Configuration file, reload nginx

cd /usr/local/nginx/sbin/
./nginx -s reload

5. Final test

Insert picture description here

Example two

1. Achieve the effect

Use nginx reverse proxy to jump to services of different ports according to the access path.
The listening port of nginx is 9001 to
access http://192.168.16.130:9001/edu/ to jump directly to 127.0.0.1:8081; to
access http://192.168.16.130:9001/vod/ to jump directly to 127.0.0.1:8082 ;

2. Preparation

(1) Prepare two tomcat servers, one port 8080 and one port 8081;

#将已经启动的tomcat,停掉
ps -ef | grep tomcat
kill -9 id号

cd /usr/src/
mkdir tomcat8080
mkdir tomcat8081

#上传tomcat压缩包后,进入目录
cd tomcat8080
#解压
tar -xvf apache-tomcat-7.0.107.tar.gz

#启动tomcat
cd apache-tomcat-7.0.107/
cd bin
./startup.sh

Install the second tomcat

cd /usr/src/
cd tomcat8081

#解压
tar -xvf apache-tomcat-7.0.107.tar.gz

#修改端口号为8081
cd apache-tomcat-7.0.107/
cd conf
vim server.xml

Insert picture description here
Insert picture description here

#启动tomcat8081
cd /usr/src/tomcat8081
cd apache-tomcat-7.0.107/bin
./startup.sh

Then enter ip:8080/8081 in the address bar of the browser to visit the tomcat homepage.

(2) Create a folder and test page

cd /usr/src/tomcat8080/apache-tomcat-7.0.107/webapps
mkdir edu

#新建一个文件
cd edu
touch a.html
vim a.html

#输入如下内容,保存退出
<h1>8080!!!</h1>

Insert picture description here
Repeat the above steps /usr/src/tomcat8081/apache-tomcat-7.0.107/webapps/vodto edit an <h1>8081!!!</h1>a.html file in the directory .
Insert picture description here

3. Specific placement

(1) Find the nginx configuration file, configure the reverse proxy,
Insert picture description here
restart nginx

cd /usr/local/nginx/sbin
./nginx -s reload

(2) The port number for external access is 9001 8080 8081

4. Final test

Insert picture description here
Insert picture description here

5. Description of location instruction

The command used to match the URL
syntax is as follows:

localtion [ = | ~ | ~* | ^~ ] uri {
    
    
	
}
  • =:Before the uri without regular expressions, the request string is required to strictly match the uri. If the match is successful, stop continuing the downward search and process the request immediately;
  • ~: Used to indicate that the uri contains regular expressions and is case sensitive;
  • ~*: Used to indicate that the uri contains regular expressions and is not case sensitive;
  • ^~: Before 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 then use this location to process the request immediately, instead of using the regular uri and request string in the location block Make a match.

Note: If uri contains regular expressions, you must have ~or ~*logo.

Guess you like

Origin blog.csdn.net/houwanle/article/details/112068763