- 一、动静分离是什么
- 1. 概念
- 二、配置动静分离
- 1.1环境准备
- 1.2 准备工作
- 1.3 安装httpd服务
- 1.4 开启服务
- 1.5 修改agent主机配置文件
- 1.6 使用agent主机IP地址访问测试
- 1.6.1 访问静态资源
- 1.6.2 访问动态资源
动静分离,通过中间件将动态请求和静态请求进行分离;可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
| 主机名 | IP | 服务 | 系统 |
|---|---|---|---|
| lnmp | 192.168.47.169 | lnmp架构 | centos8 |
| agent | 192.168.47.160 | nginx | centos7 |
| httpd | 192.168.47.130 | httpd | redhat8 |
nginx安装步骤
lnmp安装步骤
//下载和解压httpd、apr以及apr-util
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.49.tar.gz
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@httpd src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@httpd src]# tar xf httpd-2.4.49.tar.gz
[root@httpd src]# tar xf apr-util-1.6.1.tar.gz
[root@httpd src]# tar xf apr-1.7.0.tar.gz
[root@httpd apr-1.7.0]# vim configure
31878 cfgfile=${ofile}T
31879 trap "$RM "$cfgfile"; exit 1" 1 2 15
31880 # $RM "$cfgfile" 将此行加上注释,或者删除此行
31881
31882 cat <<_LT_EOF >> "$cfgfile"
// 安装EPEL rpm 包
[root@httpd apr-1.7.0]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
[root@httpd apr-1.7.0]# ls /etc/yum.repos.d/
epel-modular.repo epel-testing-modular.repo wjj.repo
epel-playground.repo epel-testing.repo
epel.repo redhat.repo
[root@httpd apr-1.7.0]# yum clean all //清理缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
33 文件已删除
[root@httpd apr-1.7.0]# yum makecache //重新建立缓存
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Extra Packages for Enterprise Linux 563 kB/s | 955 kB 00:01
Extra Packages for Enterprise Linux 616 kB/s | 10 MB 00:17
baseOS 164 MB/s | 2.3 MB 00:00
AppStream 126 MB/s | 5.8 MB 00:00
元数据缓存已建立。
// 安装开发工具包
[root@httpd apr-1.7.0]# yum groups mark install 'Development Tools' -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:01:26 前,执行于 2021年09月23日 星期四 17时49分10秒。
依赖关系解决。
=====================================================================
软件包 架构 版本 仓库 大小
=====================================================================
安装组:
Development Tools
事务概要
=====================================================================
完毕!
// 创建apache服务的用户和组
[root@httpd src]# useradd -r -M -s /sbin/nologin apache
// 安装依赖包
[root@httpd src]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
// 编译安装apr-1.7.0、apr-util-1.6.1、httpd-2.4.49
[root@httpd apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@httpd apr-1.7.0]# make && make install
[root@httpd apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@httpd apr-util-1.6.1]# make && make install
[root@httpd httpd-2.4.49]# ./configure --prefix=/usr/local/apache
> --enable-so
> --enable-ssl
> --enable-cgi
> --enable-rewrite
> --with-zlib
> --with-pcre
> --with-apr=/usr/local/apr/
> --with-apr-util=/usr/local/apr-util/
> --enable-modules=most
> --enable-mpms-shared=all
> --with-mpm=prefork
[root@httpd httpd-2.4.49]# make && make install
// 安装后配置
[root@httpd ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@httpd ~]# source /etc/profile.d/httpd.sh
[root@httpd ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@httpd ~]# vim /etc/man_db.conf
20 MANDATORY_MANPATH /usr/man
21 MANDATORY_MANPATH /usr/share/man
22 MANDATORY_MANPATH /usr/local/share/man
23 MANDATORY_MANPATH /usr/local/apache/man // 把apache加进去
//取消ServerName前面的注释
[root@httpd ~]# vim /usr/local/apache/conf/httpd.conf
203 ServerName www.example.com:80
//启动apache
[root@httpd ~]# apachectl start
[root@httpd ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 *:3306 *:*
// 配置开机自启
[root@httpd ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/httpd.service
[root@httpd ~]# vim /usr/lib/systemd/system/httpd.service
[root@httpd ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=Httpd server daemon
documentation=man:httpd(8)
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@httpd ~]# systemctl daemon-reload
[root@httpd ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@httpd ~]# systemctl status httpd.service
● httpd.service - Httpd server daemon
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
Active: active (running) since Thu 2021-09-23 18:47:11 CST; 10s a>
Docs: man:httpd(8)
// 配置apache
## 启用代理模块
// 启用httpd的相关模块
[root@httpd ~]# vim /usr/local/apache/conf/httpd.conf
119 #LoadModule remoteip_module modules/mod_remoteip.so
120 LoadModule proxy_module modules/mod_proxy.so // 取消注释
121 #LoadModule proxy_connect_module modules/mod_proxy_connect.so
122 #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
123 #LoadModule proxy_http_module modules/mod_proxy_http.so
124 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so // 取消注释
125 #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
1.4 开启服务
//httpd主机
[root@httpd ~]# systemctl start httpd
[root@httpd ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
//lnmp主机
[root@lnmp ~]# nginx
[root@lnmp ~]# systemctl start php-fpm.service
[root@lnmp ~]# systemctl start mysqld.service
[root@lnmp ~]# 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:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
// nginx主机
[root@agent ~]# nginx
[root@agent ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
1.5 修改agent主机配置文件
[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
33 #gzip on;
## 添加以下6行
34 upstream static {
35 server 192.168.47.130; ## httpd的IP
36 }
37
38 upstream dynamic {
39 server 192.168.47.169; ## lnmp的IP
40 }
41
42 server {
43 listen 80;
44 server_name localhost;
45
46 #charset koi8-r;
47
48 #access_log logs/host.access.log main;
49
50 location / {
51 #root html; # 注释或删掉
52 #index index.html index.htm; # 注释或删掉
53 proxy_pass http://static; #访问静态资源会自动跳转到进行访问
54 }
55
56 #error_page 404 /404.html;
57
58 # redirect server error pages to the static page /50x.h tml
59 #
60 error_page 500 502 503 504 /50x.html;
61 location = /50x.html {
62 root html;
63 }
64
65 # proxy the PHP scripts to Apache listening on 127.0.0. 1:80
66 #
## 取消以下三行注释,并修改
67 location ~ .php$ {
68 proxy_pass http://dynamic; #访问动态资源会自动跳转到进行访问
69 }
[root@agent ~]# nginx -s reload
[root@agent ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1.6 使用agent主机IP地址访问测试
1.6.1 访问静态资源
1.6.2 访问动态资源



