
- nginx安装:
~]# yum -y install nginx
- 修改nginx配置文件
~]# cd /etc/nginx/
nginx]# vim nginx.conf
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
#修改location /进行反代,由于我测试时nginx和tomcat是在同一台server上,所以使用localhost
    location / {
        proxy_pass :8080/;
    }
- 验证配置文件并启动nginx
nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl start nginx.service
nginx]# ss -tnl
State      Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN     0      50                        *:3306                                  *:*                  
LISTEN     0      128                       *:80                                    *:*                  
LISTEN     0      128                       *:22                                    *:*                  
LISTEN     0      100               127.0.0.1:25                                    *:*                  
LISTEN     0      100                      :::8009                                 :::*                  
LISTEN     0      128                      :::80                                   :::*                  
LISTEN     0      100                      :::8080                                 :::*                  
LISTEN     0      128                      :::22                                   :::*                  
LISTEN     0      100                     ::1:25                                   :::*                  
LISTEN     0      1          ::ffff:127.0.0.1:8005                                 :::*
此时进行访问测试,我访问http时已经不需要指定8080端口,使用默认端口即可访问我后端的tomcat

对HOST主机进行反代
- 服务器端
服务器端本地做好node的解析
~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.150.138 node2
~]# vim /etc/nginx/nginx.conf
location / {
       proxy_pass :8080/;
    }
修改完配置文件后验证并reload nginx服务
~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
~]# systemctl reload nginx.service
- 客户端访问测试,无需在添加8080端口号
~]# curl 
<html>
    <head>
        <title>JSP Test Page</title>
    </head>
    <body>
        hello world from /data/webapps
    </body>
</html>
动静分离测试:
- 服务器端:
~]# vim /etc/nginx/nginx.conf
修改nginx配置文件中的location
        location / {
    }
#如果用户访问jsp资源再进行反代,静态资源nginx自己解析
    location ~* \.(jsp|do)$ {
        proxy_pass :8080;
    }
nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx]# systemctl reload nginx.service
提供一个静态资源给nginx
~]# mv 18.png /usr/share/nginx/html/
访问静态资源,会自动去nginx资源目录:

访问动态jsp资源,会自动去访问tomcat资源目录:

LAMT:

使用httpd的reverse_proxy_module模块反代至tomcat
1、proxy_module, proxy_http_module
- httpd的安装
yum -y install httpd
- 修改httpd配置文档
注释掉主配置文档中的DocumentRoot
~]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"
创建http_tomcat.conf的子配置文档
~]# vim /etc/httpd/conf.d/http_tomcat.conf
<VirtualHost *:80>
ServerName tc1.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
    Require all granted
</Proxy>
ProxyPass / :8080/
ProxyPassReverse / :8080/
<Location />
    Require all granted
</Location>
</VirtualHost>
- 验证配置并启动httpd
~]# httpd -t
Syntax OK
~]# systemctl start httpd.service
访问此服务所有的网页均反代至tomcat 8080


2、proxy_module, proxy_ajp_module
httpd反代至tomcat的ajp 8009端口
~]# cd /etc/httpd/conf.d/
conf.d]# ls
autoindex.conf  http_tomcat.conf  README  userdir.conf  welcome.conf
conf.d]# cp http_tomcat.conf ajp_tomcat.conf
conf.d]# mv http_tomcat.conf http_tomcat.conf.bak
conf.d]# vim ajp_tomcat.conf 
<VirtualHost *:80>
    ServerName tc1.com
    ProxyRequests Off
    ProxyVia On
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / ajp://localhost:8009/
    ProxyPassReverse / ajp://localhost:8009/
    <Location />
        Require all granted
    </Location>
</VirtualHost>
- 验证配置并启动httpd
~]# httpd -t
Syntax OK
~]# systemctl restart httpd.service
访问此服务所有的网页均反代至tomcat 8009

