Nginx configuring reverse proxy

Posted by Chris_Mc_1985 on Thu, 13 Jan 2022 01:37:33 +0100

What is? Reverse proxy

The reverse proxy server determines which server provides services. The return proxy server does not provide a server. Just request forwarding.
The forward proxy is as follows

Reverse proxy is as follows

 

Nginx The process of implementing reverse proxy

First, install two tomcat servers and put them into the nginx server. The two ports are 8081 and 8082 respectively

First, Download tomcat and use the following command

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.94/bin/apache-tomcat-7.0.94.tar.gz

Then load the compressed package

tar -xvf apache-tomcat-7.0.94.tar.gz

Then copy apache-tomcat-7.0.94 into two copies, one is tomcat8081 and the other is tomcat8082. The process is as follows

cp -r apache-tomcat-7.0.94 tomcat8081
cp -r apache-tomcat-7.0.94 tomcat8082

 

Then modify the server in tomcat8081 through Editplus remote connection Modify the port number in the XML configuration

<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

Then modify the server in tomcat8082 through Editplus remote connection Modify the port number in the XML configuration

<Server port="8007" shutdown="SHUTDOWN">
<Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />

Then put the index in the ROOT folder in the webapps folder in tomact8081 The JSP changes to the following. At this time, you can see that the following input is 8081

<!DOCTYPE html>
<%@ page session="false" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
request.setAttribute("tomcat7DocUrl", "/docs/");
request.setAttribute("tomcat7ExamplesUrl", "/examples/");
%>
<html lang="en">
    <head>
        <title><%=request.getServletContext().getServerInfo() %></title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
		<h1>tomcat8081index.jsp<h1>
    </body>

</html>

Then put the index in the ROOT folder in the webapps folder in tomact8082 The JSP changes to the following. At this time, you can see that the following input is 8082

<!DOCTYPE html>
<%@ page session="false" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcat7Url", "http://tomcat.apache.org/");
request.setAttribute("tomcat7DocUrl", "/docs/");
request.setAttribute("tomcat7ExamplesUrl", "/examples/");
%>
<html lang="en">
    <head>
        <title><%=request.getServletContext().getServerInfo() %></title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
		<h1>tomcat8082index.jsp<h1>
    </body>

</html>

Then start tomcat8081 and tomcat8082, as shown below

/root/tomcat8081/bin/startup.sh
/root/tomcat8082/bin/startup.sh

Then visit http://47.91.248.236:8081/ The path results are as follows, successful, and then access http://47.91.248.236:8082/ The path results are as follows, and it is successful

 

Then we configure the host file in the local computer to look like this

The default is in the following directory (Windows 10)

C:\Windows\System32\drivers\etc

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
127.0.0.1       activate.navicat.com
192.168.199.131     www.sina.com
192.168.199.131     www.guojun.com
192.168.199.131     www.shaonian.com

Linux is added in the following directory, similar to the above

/etc/hosts

Then configure nginx in the conf folder of the nginx server Conf configuration file. Remember to restart the nginx server after configuration
When visiting www.sina.com COM, you will access the host file, and then you will find the linux server corresponding to the ip 47.91.248.236, and then www.sina.com Com the default port is 80, so visit www.sina.com COM, the following upstream tomcat1 will be found, and then the following upstream tomcat1 will go to server 47.91.248.236:8081, and the Tomcat server on port 8081 will be found. Then, because the default access page of upstream tomcat1 is index JSP, so it will access the index of the Tomcat server on port 8081 JSP page (that is http://47.91.248.236:8081/index.jsp )

When visiting www.huohu.com COM, you will access the host file, and then you will find the linux server corresponding to the ip 47.91.248.236, and then www.huohu.com Com the default port is 80, so visit www.huohu.com COM, the following upstream tomcat2 will be found, and then the following upstream tomcat2 will go to server 47.91.248.236:8082, and the Tomcat server on port 8082 will be found, because the default access page of upstream tomcat2 is index JSP, so it will access the index of the Tomcat server on port 8082 JSP page (that is http://47.91.248.236:8082/index.jsp )

user  root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #Configure www.sina.com COM: 80 corresponding server listening port
    upstream tomcat1 {
    server 192.168.199.131:8081;
    }
    server {
        listen       80;
        server_name  www.sina.com;
        location / {
            proxy_pass   http://tomcat1;
            #Configure the default access page, and the index in tomcat1 will be accessed here JSP file
            index  index.jsp;
        }
    }        
	
		#Configure web server
	    server {
        listen       80;
        server_name  www.shaonian.com;
		location / {
          root   /data/app/ruoyi-ui;
              try_files $uri $uri/ /index.html;
          index index.html index.htm;
		}
       
		location /prod-api/ {
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header REMOTE-HOST $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://localhost:8080/;		
		}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }
			}
	
	
	
    #Configure www.houhu.com COM: 80 corresponding server listening port
    upstream tomcat2 {
    server 192.168.199.131:8082;
    }
    server {
        listen       80;
        server_name  www.guojun.com;
        location / {
            proxy_pass   http://tomcat2;
            #Configure the default access page, and the index in tomcat2 will be accessed here JSP file
            index  index.jsp;
        }
    }
}

Then we visit www.sina.com com

At this time, the Tomcat server corresponding to tomcat8081 is accessed

 

Then we visit www.guojun.com com

At this time, the Tomcat server corresponding to tomcat8082 is accessed

At this point, the reverse proxy of nginx is completed. Thank you for watching!

Topics: Nginx server http