nginx、httpd动静分离

nginx、httpd动静分离


整体思路:

  • 三台服务器
  • 一台装nginx用来做负载均衡,通过区别url的方式将不同的url导向下面两台服务器
  • 一台装Lnmp用来做动态资源访问
  • 一台装httpd用来做静态访问

⽤nginx做负载均衡服务器,配置动静分离

后端RS服务器⼀台部署 LNMP(nginx1.22+mysql8.0+php8.1),

⼀台部署httpd。

要求nginx和php使⽤编译安装 最后要通过访问nginx负载均衡 服务器的IP看到动静分离的效 果

实验环境:

名称ip部署服务
stream1192.168.245.128nginx
stream2192.168.245.131lnmp
stream3192.168.245.132httpd

介绍:

什么是动静分离?

  • 将动态请求和静态请求区分访问

为什么做动静分离?

  • 静态由httpd或者nginx处理(这里使用httpd),动态由php处理

如何实现动静分离?

  • nginx负载均衡器根据客户端请求的url来判断是否是静态资源,如果url包含jpg、png等都是由相应的静态资源处理器处理
  • 如果请求的url是.php或者.jsp等待,这个时候就需要将请求转发给动态资源处理器处理
  • 总结:通过区分url来决定转发目标

部署服务

首先,与三台服务器中执行以下操作

#关闭三台服务器防火墙
[root@stream2 ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config &&  systemctl stop firewalld.service && systemctl disable firewalld.service

动态资源处理器部署:

编译安装nginx:
#创建系统用户nginx
[root@stream2 ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖
[root@stream2 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@stream2 ~]# yum -y groups mark install 'Development Tools'
#存放目录创建
[root@stream2 ~]# mkdir -p /var/log/nginx
[root@stream2 ~]# chown -R nginx.nginx /var/log/nginx
#下载源码包
[root@stream2 src]# pwd
/usr/src
[root@stream2 src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@stream2 src]# ls
debug  kernels  nginx-1.22.0.tar.gz
#编译安装
[root@stream2 src]# tar xf nginx-1.22.0.tar.gz 
[root@stream2 src]# cd nginx-1.22.0/
[root@stream2 nginx-1.22.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

#nginx配置,服务控制方式,使用nginx命令
[root@stream2 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@stream2 nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@stream2 nginx-1.22.0]# source /etc/profile.d/nginx.sh 
[root@stream2 nginx-1.22.0]# which nginx 
/usr/local/nginx/sbin/nginx
#使用 nginx -t 检查配置文件语法
[root@stream2 nginx-1.22.0]# 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
[root@stream2 nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0

#写service文件
[root@stream2 nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@stream2 nginx-1.22.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx  server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@stream2 nginx-1.22.0]# systemctl daemon-reload 
[root@stream2 nginx-1.22.0]# systemctl restart nginx.service 
[root@stream2 nginx-1.22.0]# systemctl enable nginx.service 
--
[root@stream2 nginx-1.22.0]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
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           128                       [::]:22                     [::]:*    
#随便输入几个字符,方便待会访问测试
[root@stream2 php-8.1.11]# echo sssss > /usr/local/nginx/html/index.html 
编译安装php:
#下载源码包
[root@stream2 nginx-1.22.0]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
#安装依赖
[root@stream2 nginx-1.22.0]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ wget make ncurses-devel openssl cmake libxm12 libxm12-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel php-mysqlnd libsqlite3x-devel libzip-devel https://dl.rockylinux.org/pub/rocky/9/CRB/x86_64/os/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm http://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/libzip-devel-1.7.3-7.el9.x86_64.rpm http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm epel-release --allowerasing --skip-broken --nobest
[root@stream2 nginx-1.22.0]# dnf --enablerepo=powertools install oniguruma-devel

#解压缩包,然后开始编译安装
[root@stream2 nginx-1.22.0]# tar -xf php-8.1.11.tar.gz 
[root@stream2 nginx-1.22.0]# cd php-8.1.11/
[root@stream2 nginx-1.22.0]# ./configure --prefix=/usr/local/php8 \
 --with-config-file-path=/etc \
 --enable-fpm \
 --enable-inline-optimization \
 --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-json \
 --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@stream2 php-8.1.11]# make install
#环境变量
[root@stream2 php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@stream2 php-8.1.11]# source /etc/profile.d/php8.sh
[root@stream2 php-8.1.11]# which php
/usr/local/php8/bin/php
[root@stream2 php-8.1.11]# php -v
PHP 8.1.11 (cli) (built: Oct 19 2022 13:47:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies

#配置php-fpm
[root@stream2 php-8.1.11]# cp php.ini-production  /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@stream2 php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@stream2 php-8.1.11]# chmod +x /etc/init.d/php-fpm
[root@stream2 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@stream2 php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

#编写service文件启动服务并设为开机自启
[root@stream2 php-8.1.11]# cat > /usr/lib/systemd/system/php.service <<EOF
 [Unit]
 Description=php server daemon
 After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
#设置开机自启
[root@stream2 php-8.1.11]# systemctl daemon-reload
[root@stream2 php-8.1.11]# systemctl restart php.service 
[root@stream2 php-8.1.11]# systemctl enable php.service 
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@stream2 php-8.1.11]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
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           128                  127.0.0.1:9000                0.0.0.0:*                      
LISTEN      0           128                       [::]:22                     [::]:*      

yum安装mysql8.0

[root@stream2 php-8.1.11]# yum -y install mysql mysql-server mysql-devel
[root@stream2 php-8.1.11]# mysql -V
mysql  Ver 8.0.26 for Linux on x86_64 (Source distribution)
[root@stream2 php-8.1.11]# systemctl enable mysqld.service 
[root@stream2 php-8.1.11]# systemctl restart mysqld.service 
[root@stream2 php-8.1.11]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port            Peer Address:Port     Process      
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           128                  127.0.0.1:9000                 0.0.0.0:*                     
LISTEN      0           128                       [::]:22                      [::]:*                     
LISTEN      0           70                           *:33060                      *:*                     
LISTEN      0           128                          *:3306                       *:*         

静态资源处理器部署:

[root@stream3 ~]# yum -y install httpd
[root@stream3 ~]# systemctl restart httpd.service 
[root@stream3 ~]# systemctl enable httpd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@stream3 ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*                      
LISTEN      0           128                          *:80                        *:*                      
LISTEN      0           128                       [::]:22                     [::]:*     
#随便找个图片上传,这里找了个狗头,方便待会测试
[root@stream3 html]# ls
1.png

负载均衡器配置:

#创建系统用户nginx
[root@stream3 ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖
[root@stream3 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@stream3 ~]# yum -y groups mark install 'Development Tools'
#存放目录创建
[root@stream3 ~]# mkdir -p /var/log/nginx
[root@stream3 ~]# chown -R nginx.nginx /var/log/nginx
#下载源码包
[root@stream3 src]# pwd
/usr/src
[root@stream3 src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@stream3 src]# ls
debug  kernels  nginx-1.22.0.tar.gz
#编译安装
[root@stream3 src]# tar xf nginx-1.22.0.tar.gz 
[root@stream3 src]# cd nginx-1.22.0/
[root@stream3 nginx-1.22.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

#nginx配置,服务控制方式,使用nginx命令
[root@stream3 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@stream3 nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@stream3 nginx-1.22.0]# source /etc/profile.d/nginx.sh 
[root@stream3 nginx-1.22.0]# which nginx 
/usr/local/nginx/sbin/nginx
#使用 nginx -t 检查配置文件语法
[root@stream3 nginx-1.22.0]# 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
[root@stream3 nginx-1.22.0]# nginx -v
nginx version: nginx/1.22.0

#写service文件
[root@stream3 nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@stream3 nginx-1.22.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx  server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@stream3 nginx-1.22.0]# systemctl daemon-reload 
[root@stream3 nginx-1.22.0]# systemctl restart nginx.service 
[root@stream3 nginx-1.22.0]# systemctl enable nginx.service 
--
[root@stream3 nginx-1.22.0]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port      Process      
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           128                       [::]:22                     [::]:*    

修改配置文件

[root@stream3 nginx-1.22.0]# vim /usr/local/nginx/conf/nginx.conf
#修改如下内容
    #下面定义两个负载均衡集群,因为设备有限,这里只有两台设备
    #如果条件允许可以在nginx—1代表的动态资源集群中加入设备做负载均衡,这里只是做动静分离
    upstream nginx_1 {
        server 192.168.245.131:80;
        }
    upstream httpd_1 {
        server 192.168.245.132:80;
        }
server {
        listen       80;
        #监听着80端口,后面通过80端口访问
        server_name  xuan.cn;
        include /etc/nginx/default.d/*.conf;
       #下面来匹配访问请求url,带png|gif|jpeg的访问静态资源,其他的默认访问动态资源
    location / {
        proxy_pass http://nginx_1;
        }
    location  ~*/\.(png|gif|jpeg)$ {
        proxy_pass http://httpd_1 ;
        }

访问测试:

image-20221019160920944

image-20221019161036212

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值