Linux系统LNMT架构系统与nginx的动静分离

一、概述

  所谓的LNMT架构指的就是Linux操作系统上部署Nginx web服务器、MySQL数据库服务器、Tomcat中间件服务器

             L    linux

             N   nginx

             M   mysql

             T    tomcat

             A    apache

             P    PHP

二、实验

  部署tomcat

       单机部署:

               安装两个tomcat

               修改第二个tomcat主配置文件的所有端口号

[root@localhost ~]# cd /usr/local

[root@localhost ~]# cp tomcat8 tomcat0

[root@localhost ~]# vim tomcat0/conf/server.xml

Linux系统LNMT架构系统与nginx的动静分离_第1张图片

 

 

 

 

   部署Mariadb

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
总用量 4
-rw-r--r--. 1 root root 630 5月  23 23:13 CentOS-Media.repo
drwxr-xr-x. 2 root root 195 5月  23 23:13 zhang
[root@localhost yum.repos.d]# ll zhang/
总用量 36
-rw-r--r--. 1 root root 1664 10月 23 2020 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 10月 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root  649 10月 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 10月 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 1331 10月 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 10月 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root  616 10月 23 2020 CentOS-x86_64-kernel.repo
[root@localhost yum.repos.d]# mv CentOS-Media.repo zhang                           

                                        #将本地源移动到zhang目录内(不使用本地yum源下载)
[root@localhost yum.repos.d]# mv zhang/CentOS-Base.repo .

                                       #使用base源安装软件
[root@localhost yum.repos.d]# ll
总用量 4
-rw-r--r--. 1 root root 1664 10月 23 2020 CentOS-Base.repo
drwxr-xr-x. 2 root root  196 5月  30 19:31 zhang

[root@localhost ~]# yum -y install mariadb-server

        部署nginx

            安装epel源

[root@localhost ~]# yum -y install epel-release

               yum安装nginx

[root@localhost ~]# yum -y install nginx

实现反向代理、负载均衡

三、反向代理负载均衡

  配置nginx

 例:第一种:

[root@localhost ~]# vim /etc/nginx/nginx.conf

    写到http区域

(轮询)默认

upstream  tomcat {

           server 192.168.108.199:8080;       #本机IP及端口

           server 192.168.108.199:8081;       #另一个端口

  }

写到server字段

location / {

         root /usr/share/nginx/html;

         proxy_pass http://tomcat;

      # porxy_set_hrader  Host $host;

  }

[root@localhost ~]# vim /usr/local/tomcat0/webapps/ROOT/index.html 

                                                          #修改tomcat0主页文件
[root@localhost ~]# vim /usr/local/tomcat8/webapps/ROOT/index.html

                                                         # 修改tomcat8主页文件

重启服务

[root@localhost ~]# systemctl restart nginx    重启nginx服务

[root@localhost ~]# tomcat0down && tomcat0up    #重启tomcat0的端口服务
[root@localhost ~]# tomcatdown && tomcatup      #重启tomcat8的端口服务

测试

Linux系统LNMT架构系统与nginx的动静分离_第2张图片Linux系统LNMT架构系统与nginx的动静分离_第3张图片

  第二种

[root@localhost ~]# vim /etc/nginx/nginx.conf

写到http区域

(加权轮询)

upstream  tomcat {

         server 192.168.108.199:8080 weight=1;       #添加访问次数

         server 192.168.108.199:8081 weight=2;

  }

写到server字段

location / {

     root /usr/share/nginx/html;

     proxy_pass http://tomcat;

  # porxy_set_hrader  Host $host;

  }

重启服务测试

[root@localhost ~]# systemctl restart nginx

测试

Linux系统LNMT架构系统与nginx的动静分离_第4张图片Linux系统LNMT架构系统与nginx的动静分离_第5张图片

三、LNMT与nginx动静分离

  配置nginx

[root@localhost ~]# vim /etc/nginx/nginx.conf

    location  ~\.jsp$ {

proxy_pass http://tomcat;

proxy_set_header  Host $host;

  }

location / {

root /usr/share/nginx/html;

index index.html;

  }

Linux系统LNMT架构系统与nginx的动静分离_第6张图片

  重启服务

[root@localhost ~]# systemctl restart nginx

  测试

   使用站点不存在的资源进行访问,查看报错信息

   http://192.168.108.199/img.jsp(访问不存在的配置文件以.jsp结尾的动态文件)

tomcat报错

Linux系统LNMT架构系统与nginx的动静分离_第7张图片

http://192.168.108.199/img.html(访问不存在的配置文件以.html结尾的静态文件)

nginx报错

Linux系统LNMT架构系统与nginx的动静分离_第8张图片

你可能感兴趣的:(服务器,tomcat,运维)