66-Linux-Tomcat-nginx反向代理

本文介绍了如何在Linux环境下,通过Nginx进行反向代理配置,实现动静分离,并探讨了httpd使用AJP协议反向代理Tomcat的应用。基础环境和软件包安装请参照上期博客内容。
摘要由CSDN通过智能技术生成


请务必查看上期博客基础环境设置

基础环境及软件包安装参照上期博客内容

参照内容截止至“#访问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

跳至文章尾部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值