66-Linux-Tomcat-nginx反向代理


請務必查看上期博客基礎環境設置

基礎環境及軟件包安裝參照上期博客內容

參照內容截止至“#訪問Tomcat自定義頁面

#默認已完成上述博客內容中的基礎環境


實驗內容

nginx反向代理

#追加一條新行
[root@C7-4 ~]# vim /etc/hosts
127.0.0.1 www.dushan.com 

#更改原先頁面文件內容
[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> 

#設置目錄屬主屬組
[root@C7-4 ~]# chown -R java:java /usr/local/tomcat

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

#查看當前安裝後的nginx版本
[root@C7-4: ~]# rpm -qa nginx
nginx-1.16.1-1.el7.x86_64

#查閱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
#找到如下位置,添加該行: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


#重啓nginx和tomcat服務
[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.


#查看端口
[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                           [::]:*                  


#查看80端口爲nginx在監聽
[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)

#訪問頁面
[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>
#複製一個會話框,一個用以監控日誌,另一個訪問主機頁面
#會話一
[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> 

#會話二
[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" "-"

基於nginx反向代理實現動靜分離

#找到如下位置,並修改
[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


#會話一
[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 …………………………………………省略………………………………………….. 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> 


#會話二
[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反向代理tomcat

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

#安裝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)
#爲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

在httpd中使用ajp協議反向代理tomcat

[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

跳至文章尾部

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章