Nginx动静分离

7 篇文章 0 订阅
6 篇文章 1 订阅

Nginx动静分离

一、概述

动态页面与静态页面区别
  • 静态资源: 当用户多次访问这个资源,资源的源代码永远不会改变的资源。
  • 动态资源:当用户多次访问这个资源,资源的源代码可能会发送改变。
什么是动静分离
  • 动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路
  • 动静分离简单的概括是:动态文件与静态文件的分离。
  • 伪静态:网站如果想被搜索引擎搜素到,动态页面静态技术freemarker等模版引擎技术
为什么要用动静分离
  • 在我们的软件开发中,有些请求是需要后台处理的(如:.jsp,.do等等),有些请求是不需要经过后台处理的(如:css、html、jpg、js等等文件),这些不需要经过后台处理的文件称为静态文件,否则动态文件。因此我们后台处理忽略静态文件。这会有人又说那我后台忽略静态文件不就完了吗。当然这是可以的,但是这样后台的请求次数就明显增多了。在我们对资源的响应速度有要求的时候,我们应该使用这种动静分离的策略去解决。
  • 动静分离将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署,提高用户访问静态代码的速度,降低对后台应用访问。这里我们将静态资源放到nginx中,动态资源转发到tomcat服务器中。
  • 因此,动态资源转发到tomcat服务器我们就使用到了前面讲到的反向代理了。

二、Nginx实现动静分离

主机IP需要安装的服务
nginx服务端192.168.240.60nginx
客户端1192.168.240.70lnmp
客户端2192.168.240.50http
服务安装
nginx服务端安装:

创建系统用户nginx

[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

安装依赖环境

[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
上次元数据过期检查:2:59:11 前,执行于 2021年10月25日 星期一 01时04分32秒。
依赖关系解决。
=============================================================
 软件包       架构        版本            仓库          大小
=============================================================
安装组:
 Development Tools
                                                            

事务概要
=============================================================

完毕!

创建日志存放目录

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

下载nginx

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
--2021-10-25 04:06:16--  http://nginx.org/download/nginx-1.12.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 

编译安装

[root@localhost src]# ls
debug  kernels  nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
lnmp源码安装:

准备安装包

[root@localhost package]# ls
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
nginx-1.20.1.tar.gz
php-8.0.10.tar.gz

解压

[root@localhost package]# tar xf nginx-1.20.1.tar.gz -C /usr/local/
[root@localhost package]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost package]# tar xf php-8.0.10.tar.gz -C /usr/local/
[root@localhost package]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.34-linux-glibc2.12-x86_64  nginx  nginx-1.20.1  php-8.0.10  sbin  share  src

关闭防火墙

[root@localhost ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@localhost ~]# setenforce 0

nginx安装

创建系统用户nginx

[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

安装依赖环境

[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
上次元数据过期检查:2:59:11 前,执行于 2021年10月25日 星期一 01时04分32秒。
依赖关系解决。
=============================================================
 软件包       架构        版本            仓库          大小
=============================================================
安装组:
 Development Tools
                                                            

事务概要
=============================================================

完毕!

创建日志存放目录

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

编译安装

[root@localhost ~]# cd /usr/local/nginx-1.20.1
    ./configure \
     --prefix=$nginx_installdir/nginx \
     --user=nginx \
     --group=nginx \
     --with-debug \
     --with-http_ssl_module \
     --with-http_realip_module \
     --with-http_image_filter_module \
     --with-http_gunzip_module \
     --with-http_gzip_static_module \
     --with-http_stub_status_module \
     --http-log-path=/var/log/nginx/access.log \
     --error-log-path=/var/log/nginx/error.log  && make && make install

配置环境变量

[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh

mysql安装

创建MySQL用户

[root@localhost ~]# useradd -r -M -s /sbin/nologin  mysql

下载依赖包

[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

创建软连接

[root@localhost local]# ln -sv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64/'
[root@localhost local]# ll
总用量 4
drwxr-xr-x.  2 root root    6 5月  18 2020 bin
drwxr-xr-x.  2 root root    6 5月  18 2020 etc
drwxr-xr-x.  2 root root    6 5月  18 2020 games
drwxr-xr-x.  2 root root    6 5月  18 2020 include
drwxr-xr-x.  2 root root    6 5月  18 2020 lib
drwxr-xr-x.  3 root root   17 9月  30 05:17 lib64
drwxr-xr-x.  2 root root    6 5月  18 2020 libexec
lrwxrwxrwx.  1 root root   36 10月 26 02:33 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/

修改目录/usr/local/mysql的属主属组

[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 10月 26 02:33 /usr/local/mysql -> mysql-5.7.34-linux-glibc2.12-x86_64/

添加环境变量

[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# . /etc/profile.d/mysql.sh 
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

创建数据存放目录

[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/

初始化数据存放目录

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
·····
2021-10-26T06:39:03.254065Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T06:39:03.593420Z 1 [Note] A temporary password is generated for root@localhost: cCRvI!Cgu2up

配置mysql

[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig

生成配置文件

[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

配置服务启动脚本

[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

启动

[root@localhost ~]#  service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
. SUCCESS! 

登录并更改密码

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('lq123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

PHP安装

下载依赖包

[root@localhost ~]# yum -y install sqlite-devel libzip-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel
[root@localhost ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

编译安装

[root@localhost]# cd /usr/local/php-8.0.10/
[root@localhost php-8.0.10]#  ./configure --prefix=/usr/local/php8  \
    --with-config-file-path=/etc \
    --enable-fpm \
    --disable-debug \
    --disable-rpath \
    --enable-shared \
    --enable-soap \
    --with-openssl \
    --enable-bcmath \
    --with-iconv \
    --with-bz2 \
    --enable-calendar \
    --with-curl \
    --enable-exif  \
    --enable-ftp \
    --enable-gd \
    --with-jpeg \
    --with-zlib-dir \
    --with-freetype \
    --with-gettext \
    --enable-mbstring \
    --enable-pdo \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-readline \
    --enable-shmop \
    --enable-simplexml \
    --enable-sockets \
    --with-zip \
    --enable-mysqlnd-compression-support \
    --with-pear \
    --enable-pcntl \
    --enable-posix
[root@localhost php-8.0.10]# make && make install

安装后配置

[root@localhost php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-8.0.10]# source /etc/profile.d/php.sh 
[root@localhost php-8.0.10]# which php
/usr/local/php8/bin/php
[root@localhost php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Oct 26 2021 03:20:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies

配置php-fpm

[root@localhost php-8.0.10]# cp -f /usr/local/php-8.0.10/php.ini-production /etc/php.ini
[root@localhost php-8.0.10]# cp /usr/local/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f
[root@localhost php-8.0.10]# chmod +x /etc/init.d/php-fpm 
[root@localhost php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

启动

[root@localhost ~]# service php-fpm start
Starting php-fpm  done
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128         127.0.0.1:9000        0.0.0.0:*                                                                 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*                                                                 
LISTEN 0      80                  *:3306              *:*                                                                 
LISTEN 0      128              [::]:22             [::]:*
http源码安装:
##安装必要的工具
[root@localhost ~]# yum -y install gcc gcc-c++
[root@localhost ~]# yum -y install make
[root@localhost ~]# yum -y install wget
[root@localhost ~]# yum -y install pcre-devel
##下载
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz		
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
##解压
[root@localhost ~]# tar xf apr-1.7.0 		
[root@localhost ~]# tar xf apr-util-1.6.1
[root@localhost ~]# tar xf httpd-2.4.48
[root@localhost ~]# ls
apr-1.7.0  apr-1.7.0.tar.gz  apr-util-1.6.1  apr-util-1.6.1.tar.gz  httpd-2.4.48  httpd-2.4.48.tar.gz
##进入apr-1.7.0目录,将配置文件configure中的 "$RM \$cfgfile" 删除或注释掉
[root@localhost ~]# cd apr-1.7.0/
[root@localhost apr-1.7.0]# vi configure
##./configure  --prefix=安装路径  --with
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make  && make install               #编译和安装  
##./configure  --prefix=安装路径  --with
[root@localhost ~]# cd apr-util-1.6.1/
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install           #编译和安装
##./configure  --prefix=安装路径  --with
[root@localhost ~]# cd httpd-2.4.48/
[root@localhost httpd-2.4.48]# ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
[root@localhost httpd-2.4.48]# make && make install            #编译和安装 
##关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# /usr/local/httpd/bin/apachectl start       #启动程序
nginx服务端配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 
 ##开启动静分离
    upstream static {
        server 192.168.240.50;
    }

    upstream dynamic {
        server 192.168.240.70;
    }

##设置静态页面获取地址
    location / {
        proxy_pass http://static;
     }

##设置动态页面获取地址
        location ~ \.php$ {
            proxy_pass   http://dynamic;
        }

##重新加载
[root@localhost ~]# nginx -s reload

浏览器访问测试
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值