66 Linux Tomcat nginx reverse agent

Keywords: Nginx Tomcat JSP curl

Experimental catalogue

Please be sure to check the basic environment settings of the previous blog

Please refer to the previous blog for basic environment and software package installation

Reference content ends at "visit Tomcat custom page"

#The basic environment in the above blog content is completed by default

Experimental content

nginx reverse proxy

#Append a new line
[root@C7-4 ~]# vim /etc/hosts
127.0.0.1 www.dushan.com 

#Change the contents of the original page file
[root@C7-4 ~]# echo '<h1>tomcat ROOT static index.html page from nginx !</h1>' > /usr/local/tomcat/webapps/ROOT/index.html

[root@C7-4: ~]# vim /usr/local/tomcat/webapps/ROOT/myindex.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
            <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
<%
out.println("hello jsp");
%>
</body>
</html> 

#Set directory main group
[root@C7-4 ~]# chown -R java:java /usr/local/tomcat

#Install nginx
[root@C7-4: ~]# yum -y install nginx

#View the current version of nginx after installation
[root@C7-4: ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64

#Check related documents of nginx
[root@C7-4: ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
..........
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
#Find the following location and add the line: proxy ﹣ pass http://www.dushan.com:8080;   
[root@C7-4: ~]# vim /etc/nginx/nginx.conf

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / { 
                proxy_pass http://www.dushan.com:8080;                                                                       
        }   


[root@C7-4: ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


#Restart nginx and tomcat services
[root@C7-4: ~]# systemctl start nginx

[root@C7-4: ~]# su - java -c '/usr/local/tomcat/bin/startup.sh'
su: warning: cannot change directory to /home/java: No such file or directory
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/default
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.


#View port
[root@C7-4 ~]# ss -ntl
State       Recv-Q Send-Q  Local Address:Port                 Peer Address:Port              
LISTEN      0      100         127.0.0.1:25                              *:*                  
LISTEN      0      128                 *:80                              *:*                  
LISTEN      0      128                 *:22                              *:*                  
LISTEN      0      100             [::1]:25                           [::]:*                  
LISTEN      0      1      [::ffff:127.0.0.1]:8005                         [::]:*                  
LISTEN      0      100              [::]:8009                         [::]:*                  
LISTEN      0      100              [::]:8080                         [::]:*                  
LISTEN      0      128              [::]:80                           [::]:*                  
LISTEN      0      128              [::]:22                           [::]:*                  


#View port 80 as nginx listening
[root@C7-4: ~]# lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   3744  root    6u  IPv4  29163      0t0  TCP *:http (LISTEN)
nginx   3744  root    7u  IPv6  29164      0t0  TCP *:http (LISTEN)
nginx   3745 nginx    6u  IPv4  29163      0t0  TCP *:http (LISTEN)
nginx   3745 nginx    7u  IPv6  29164      0t0  TCP *:http (LISTEN)
nginx   3746 nginx    6u  IPv4  29163      0t0  TCP *:http (LISTEN)
nginx   3746 nginx    7u  IPv6  29164      0t0  TCP *:http (LISTEN)

#Access page
[root@C7-4 ~]# curl www.dushan.com:8080/index.html
<h1>tomcat ROOT static index.html page from nginx !</h1>

[root@C7-4 ~]# curl www.dushan.com:8080/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html>
#Copy a session box, one to monitor the log, and one to access the host page
#Conversation 1
[root@C7-4 ~]# curl www.dushan.com/index.html
<h1>tomcat ROOT static index.html page from nginx !</h1>
[root@C7-4 ~]# curl www.dushan.com/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html> 

#Conversation two
[root@C7-4 ~]# tail -f /var/log/nginx/access.log 
127.0.0.1 - - [13/Feb/2020:13:35:12 +0800] "GET /index.html HTTP/1.1" 200 57 "-" "curl/7.29.0" "-"
127.0.0.1 - - [13/Feb/2020:13:35:34 +0800] "GET /myindex.jsp HTTP/1.1" 200 180 "-" "curl/7.29.0" "-"

Dynamic and static separation based on nginx reverse agent

#Locate and modify
[root@C7-4 nginx]# vim /etc/nginx/nginx.conf

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {                index index.html;
        }

        location ~* \.jsp$ {
                proxy_pass http://www.dushan.com:8080;                                         
        }

[root@C7-4 nginx]# mv /usr/local/tomcat/webapps/ROOT/index.html /usr/share/nginx/html/
mv: overwrite '/usr/share/nginx/html/index.html'? Y

[root@C7-4 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@C7-4 nginx]# systemctl restart nginx


#Conversation 1
[root@C7-4 nginx]# curl www.dushan.com:8080/index.html
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b ................................................Omitted.. resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.50</h3></body></html>


[root@C7-4 nginx]# curl www.dushan.com:8080/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html> 
[root@C7-4 nginx]# curl www.dushan.com/index.html
<h1>tomcat ROOT static index.html page from nginx !</h1>

[root@C7-4 nginx]# curl www.dushan.com/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html> 


#Conversation two
[root@C7-4 nginx]# > /var/log/nginx/access.log 
[root@C7-4 nginx]# tail -f /var/log/nginx/access.log 
127.0.0.1 - - [13/Feb/2020:16:15:51 +0800] "GET /index.html HTTP/1.1" 200 57 "-" "curl/7.29.0" "-"
127.0.0.1 - - [13/Feb/2020:16:15:59 +0800] "GET /myindex.jsp HTTP/1.1" 200 180 "-" "curl/7.29.0" "-"


[root@C7-4 ~]# tail -f /usr/local/tomcat/logs/localhost_access_log.2020-02-13.txt 
127.0.0.1 - - [13/Feb/2020:16:11:16 +0800] "GET /index.html HTTP/1.1" 404 719
127.0.0.1 - - [13/Feb/2020:16:12:32 +0800] "GET /index.jsp HTTP/1.1" 200 11215
127.0.0.1 - - [13/Feb/2020:16:12:52 +0800] "GET /myindex.jsp HTTP/1.1" 200 180
127.0.0.1 - - [13/Feb/2020:16:13:53 +0800] "GET /myindex.jsp HTTP/1.0" 200 180
127.0.0.1 - - [13/Feb/2020:16:15:33 +0800] "GET /index.html HTTP/1.1" 404 719
127.0.0.1 - - [13/Feb/2020:16:15:42 +0800] "GET /myindex.jsp HTTP/1.1" 200 180
127.0.0.1 - - [13/Feb/2020:16:15:59 +0800] "GET /myindex.jsp HTTP/1.0" 200 180

httpd reverse proxy tomcat

#Uninstall nginx
[root@C7-4 ~]# systemctl stop nginx
[root@C7-4 ~]# yum -y remove nginx

#Install httpd
[root@C7-4 ~]# yum -y install httpd

[root@C7-4 ~]# httpd -M|grep proxy
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe23:8226. Set the 'ServerName' directive globally to suppress this message
 proxy_module (shared)
 proxy_ajp_module (shared)
 proxy_balancer_module (shared)
 proxy_connect_module (shared)
 proxy_express_module (shared)
 proxy_fcgi_module (shared)
 proxy_fdpass_module (shared)
 proxy_ftp_module (shared)
 proxy_http_module (shared)
 proxy_scgi_module (shared)
 proxy_wstunnel_module (shared)
#Add a virtual host for httpd
[root@C7-4 ~]# vim /etc/httpd/conf.d/tomcat.conf

<VirtualHost *:80>
        ServerName          www.dushan.com                                                     
        ProxyRequests       Off
        ProxyVia            On
        ProxyPreserveHost   On
        ProxyPass         / http://127.0.0.1:8080/
        ProxyPassReverse  / http://127.0.0.1:8080/
</VirtualHost>

[root@C7-4 ~]# echo 'tomcat static page by httpd !' > /usr/local/tomcat/webapps/ROOT/index.html

[root@C7-4 ~]# systemctl start httpd

[root@C7-4 love]# curl 127.0.0.1/index.html
tomcat static page by httpd !
[root@C7-4 love]# curl 127.0.0.1:8080/index.html
tomcat static page by httpd !
[root@C7-4 love]# curl www.dushan.com/index.html
tomcat static page by httpd !
[root@C7-4 love]# curl www.dushan.com:8080/index.html
tomcat static page by httpd !


[root@C7-4 love]# curl www.dushan.com/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html> 
[root@C7-4 ~]# tail -f /var/log/httpd/access_log
127.0.0.1 - - [13/Feb/2020:16:46:57 +0800] "GET /index.html HTTP/1.1" 200 30 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:47:39 +0800] "GET / HTTP/1.1" 200 11195 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:47:44 +0800] "GET /index.html HTTP/1.1" 200 30 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:48:02 +0800] "GET /index.html HTTP/1.1" 200 30 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:48:48 +0800] "GET /myindex.jsp HTTP/1.1" 200 180 "-" "curl/7.29.0"


[root@C7-4 ~]# tail -f /usr/local/tomcat/logs/localhost_access_log.2020-02-13.txt 
127.0.0.1 - - [13/Feb/2020:16:47:44 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:47:47 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:48:02 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:48:11 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:48:48 +0800] "GET /myindex.jsp HTTP/1.1" 200 180

Using ajp protocol to reverse proxy tomcat in httpd

[root@C7-4 ~]# vim /etc/httpd/conf.d/tomcat.conf 

<VirtualHost *:80>
        ServerName          www.dushan.com
        ProxyRequests       Off
        ProxyVia            On
        ProxyPreserveHost   On
        ProxyPass         / ajp://127.0.0.1:8009/
</VirtualHost> 


[root@C7-4 ~]# systemctl restart httpd


[root@C7-4 love]# curl 127.0.0.1/index.html
tomcat static page by httpd !

[root@C7-4 love]# curl www.dushan.com/index.html
tomcat static page by httpd !

[root@C7-4 love]# curl www.dushan.com/myindex.jsp

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
      <title>myapp of dushan</title>
</head>
<body>
<h1> test jsp in default ROOT dir ! </h1>
hello jsp

</body>
</html> 


[root@C7-4 ~]# tail -f /var/log/httpd/access_log 
127.0.0.1 - - [13/Feb/2020:16:57:14 +0800] "GET /index.html HTTP/1.1" 200 30 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:57:28 +0800] "GET /index.html HTTP/1.1" 200 30 "-" "curl/7.29.0"
127.0.0.1 - - [13/Feb/2020:16:57:49 +0800] "GET /myindex.jsp HTTP/1.1" 200 180 "-" "curl/7.29.0"



[root@C7-4 ~]# tail -f /usr/local/tomcat/logs/localhost_access_log.2020-02-13.txt 
127.0.0.1 - - [13/Feb/2020:16:57:14 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:57:28 +0800] "GET /index.html HTTP/1.1" 200 30
127.0.0.1 - - [13/Feb/2020:16:57:49 +0800] "GET /myindex.jsp HTTP/1.1" 200 180

Jump to the end of the article

66 original articles published, 105 praised, 7930 visited
Private letter follow

Posted by boyakasha on Thu, 13 Feb 2020 06:32:54 -0800