Nginx实现动静分离

Nginx实现动静分离

简介:

反向代理与负载均衡
nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy`模块,功能很多,最常用的是`proxy_pass`和`proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:

./configure --add-module=../ngx_cache_purge-1.0 ......

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

在upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:

upstream idfsoft.com {							# 此字段要写在server字段的前面
  ip_hash;
  server 127.0.0.1:4444 weight=3;				
  server 127.0.0.1:8080 weight=3;
  server 127.0.0.1:1111;
}


 upstream webservers{
        server 192.168.220.10 weight=3;    # weight表示访问三次这个IP后访问一次下面的IP
        server 192.168.220.17;

    }

注:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。

定义好upstream后,需要在server段内添加如下内容:

server {
  location / {
    proxy_pass http://webservers;				# 这里要和upstream段配置的域名一致
  }
}
实验环境说明:
系统IP服务
CentOS8 调度器 DR192.168.220.9Nginx nginx-1.20.1.tar.gz
CentOS8 动态页面处理 Dynamic(动态)192.168.220.10LNMP nginx-1.20.1.tar.gz mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz php-8.0.10.tar.gz
CentOS8 静态页面处理 Static(静态)192.168.220.17httpd apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz

关闭三台防火墙和selinux,改名

# DR
[root@localhost ~]# systemctl disable --now firewalld
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/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# hostnamectl set-hostname DR
[root@localhost ~]# bash
[root@DR ~]#

# Dynamic
[root@localhost ~]# systemctl disable --now firewalld
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/selinux/config

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# hostnamectl set-hostname Dynamic
[root@localhost ~]# bash
[root@Dynamic ~]#

# Static
[root@localhost ~]# systemctl disable --now firewalld
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/selinux/config

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# hostnamectl set-hostname Static
[root@localhost ~]# bash
[root@Static ~]#
DR上安装Nginx
# 安装***Nginx***    
# 安装依赖包和工具包
[root@DR ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@DR ~]# yum -y group mark install "Development Tools"

# 创建用户
[root@DR ~]# useradd -r -M -s /sbin/nologin nginx

# 创建日志存放目录
[root@DR ~]# mkdir /var/log/nginx -p

# 下载nginx,解压
root@DR ~]# mkdir /usr/src/soft
[root@DR ~]# cd /usr/src/soft/
[root@DR soft]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@DR soft]# ls
nginx-1.20.1.tar.gz
[root@DR soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/

# 编译安装Nginx
[root@DR soft]# cd /usr/local/nginx-1.20.1/
[root@DR nginx-1.20.1]#./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@DR nginx-1.20.1]# make && make install
......

# 配置环境变量
[root@DR nginx-1.20.1]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@DR nginx-1.20.1]# which nginx
/usr/local/nginx/sbin/nginx

# 尝试启动Nginx
[root@DR nginx-1.20.1]# /usr/local/nginx/sbin/nginx 
[root@DR nginx-1.20.1]# 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                0.0.0.0:80(Nginx默认端口)                0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*

# 设置开机自启
[root@DR nginx-1.20.1]# cd /usr/lib/systemd/system/
[root@DR system]# cp sshd.service nginxd.service
[root@DR system]# vi nginxd.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=/usr/local/nginx/sbin/nginx -s reload
KillMode=process

[Install]
WantedBy=multi-user.target

[root@DR system]# /usr/local/nginx/sbin/nginx -s stop
[root@DR system]# systemctl daemon-reload
[root@DR system]# systemctl start nginxd.service
[root@DR system]# 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                0.0.0.0:80                0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*
访问页面是否正常

在这里插入图片描述

Dynamic安装LNMP
# 安装***Nginx***
# 安装依赖包和需要的工具包
[root@Dynamic ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@Dynamic ~]# yum -y group mark install "Development Tools"

# 创建用户nginx
[root@Dynamic ~]# useradd -r -M -s /sbin/nologin nginx

# 创建nginx的日志存放目录
[root@Dynamic ~]# mkdir -p /var/log/nginx

# 下载nginx并解压
[root@Dynamic ~]# mkdir /usr/src/soft
[root@Dynamic ~]# cd /usr/src/soft/
[root@Dynamic ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@Dynamic soft]# ls
nginx-1.20.1.tar.gz
[root@Dynamic soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local/

# 开始编译安装nginx
[root@Dynamic soft]# cd /usr/local/nginx-1.20.1/
[root@Dynamic nginx-1.20.1]# ./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@Dynamic nginx-1.20.1]# make && make install
......

# 配置环境变量
[root@Dynamic nginx-1.20.1]# which nginx
/usr/bin/which: no nginx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@Dynamic nginx-1.20.1]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@Dynamic nginx-1.20.1]# bash
[root@Dynamic nginx-1.20.1]# which nginx
/usr/local/nginx/sbin/nginx

# 启动nginx
[root@Dynamic nginx-1.20.1]# 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                             [::]:22                           [::]:*                          
[root@Dynamic nginx-1.20.1]# nginx

[root@Dynamic nginx-1.20.1]# 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                          0.0.0.0:80(nginx默认端口)                        0.0.0.0:*                          
LISTEN        0              128                             [::]:22                           [::]:*

# 设置开机自启
[root@Dynamic nginx-1.20.1]# cd /usr/lib/systemd/system
[root@Dynamic system]# cp sshd.service nginxd.service
[root@Dynamic system]# vim nginxd.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@Dynamic system]# nginx -s stop
[root@Dynamic system]# 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                             [::]:22                           [::]:*
                         
[root@Dynamic system]# systemctl daemon-reload
[root@Dynamic system]# systemctl enable --now nginxd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginxd.service → /usr/lib/systemd/system/nginxd.service.

[root@Dynamic system]# systemctl status nginxd.service
● nginxd.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginxd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-10-31 22:16:00 EDT; 7s ago
  Process: 211149 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 211151 (nginx)
    Tasks: 2 (limit: 11201)
   Memory: 2.5M
   CGroup: /system.slice/nginxd.service
           ├─211151 nginx: master process /usr/local/nginx/sbin/nginx
           └─211152 nginx: worker process

Oct 31 22:16:00 Dynamic systemd[1]: Starting nginx server daemon...
Oct 31 22:16:00 Dynamic systemd[1]: Started nginx server daemon.

[root@Dynamic system]# 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                          0.0.0.0:80                        0.0.0.0:*                          
LISTEN        0              128                             [::]:22                           [::]:*


# 安装***Mysql*** 
# 下载MySQL和工具  
yum -y install wget ncurses-compat-libs

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

[root@Dynamic ~]# ls /usr/src/soft/
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

# 创建用户
[root@Dynamic]# useradd -r -M -s /sbin/nologin mysql

# 解压
[root@Dynamic ~]# cd /usr/src/soft/
[root@Dynamic soft]# ls
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  nginx-1.20.1.tar.gz  

[root@Dynamic soft]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

# 创建软连接
[root@Dynamic soft]# cd /usr/local/
[root@Dynamic local]# ls
bin  games    lib    libexec                              nginx         sbin   src
etc  include  lib64  mysql-5.7.34-linux-glibc2.12-x86_64  nginx-1.20.1  share

[root@Dynamic local]# ln -sv  mysql-5.7.34-linux-glibc2.12-x86_64 mysql
'mysql' -> 'mysql-5.7.34-linux-glibc2.12-x86_64'
[root@Dynamic local]# ll
total 0
......
lrwxrwxrwx.  1 root root  35 Oct 29 02:58 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x.  9 root root 129 Oct 29 02:57 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 151 Oct 29 02:56 nginx
drwxr-xr-x.  9 1001 1001 186 Oct 29 02:56 nginx-1.20.1
.......

# 修改属主
root@Dynamic local]# chown -R mysql.mysql /usr/local/mysql*
[root@Dynamic local]# ll
total 0
......
lrwxrwxrwx.  1 mysql mysql  35 Oct 29 02:58 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x.  9 mysql mysql 129 Oct 29 02:57 mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root  root  151 Oct 29 02:56 nginx
drwxr-xr-x.  9  1001  1001 186 Oct 29 02:56 nginx-1.20.1
.......

# 创建存放数据目录并修改属组
[root@Dynamic local]# mkdir /opt/data
[root@Dynamic local]# chown -R mysql.mysql /opt/data/
[root@Dynamic local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 29 03:02 data

# 添加环境变量
[root@Dynamic local]# which mysql
/usr/bin/which: no mysql in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@Dynamic local]# echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@Dynamic local]# bash
[root@Dynamic local]# which mysql
/usr/local/mysql/bin/mysql

# 初始化数据库不要密码
[root@Dynamic local]# cd
[root@Dynamic ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
2021-10-29T07:07:05.689651Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
......
2021-10-29T07:07:06.619196Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

# 创建配置文件
[root@Dynamic ~]# vi /etc/my.cnf
[mysqld]
datadir = /opt/data
basedir = /usr/local/mysql
port=3306
socket = /tmp/mysql.sock
pid-file = /opt/data/mysql.pid
skip-name-resolve

# 修改支持文件
[root@Dynamic ~]# vi /usr/local/mysql/support-files/mysql.server
......
46 basedir=/usr/local/mysql   # 安装MySQL的位置
47 datadir=/opt/data          # 数据存放目录
.......


# 尝试启动MySQL
[root@Dynamic ~]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/Dynamic.err'.
 SUCCESS! 
[root@Dynamic ~]# 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         80                       *:3306(MySQL默认端口)                    *:*                  
LISTEN    0         128                   [::]:22                   [::]:*


# 登录MySQL并设置密码
[root@Dynamic ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

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('123456');                 # 给MySQL密码设置
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye 
[root@Dynamic ~]# mysql -uroot -p123456                   # 使用新密码登录
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)

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> exit
Bye

# 设置MySQL开启自启
[root@Dynamic ~]# cd /usr/lib/systemd/system/
[root@Dynamic system]# cp nginxd.service mysqld.service
[root@Dynamic system]# vim mysqld.service
[Unit]
Description=MySQL server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP

[Install]
WantedBy=multi-user.target

[root@Dynamic system]# /usr/local/mysql/support-files/mysql.server stop
Shutting down MySQL.. SUCCESS! 
[root@Dynamic system]# systemctl daemon-reload
[root@Dynamic system]# systemctl start mysqld.service
[root@Dynamic system]# 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         80                       *:3306                    *:*                  
LISTEN    0         128                   [::]:22                   [::]:*


# 安装***PHP***
# 下载依托源
[root@Dynamic ~]# yum -y install epel-release

# 下载依赖包
[root@Dynamic ~]# 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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel

[root@Dynamic ~]# 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

# 下载PHP
[root@Dynamic ~]# cd /usr/src/soft/
[root@Dynamic soft]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
[root@Dynamic soft]# ls
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz  nginx-1.20.1.tar.gz  php-8.0.10.tar.gz

# 解压PHP
[root@Dynamic soft]# tar xf php-8.0.10.tar.gz -C /usr/local/

# 编译安装PHP
[root@Dynamic soft]# cd /usr/local/
[root@Dynamic local]# ls
bin  games    lib    libexec  mysql-5.7.34-linux-glibc2.12-x86_64  nginx-1.20.1  sbin   src
etc  include  lib64  mysql    nginx                                php-8.0.10    share

[root@Dynamic local]# cd php-8.0.10
[root@Dynamic 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
.......
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

[root@Dynamic php-8.0.10]# make
......
[root@Dynamic php-8.0.10]# make install
......

# 添加环境变量
[root@Dynamic php-8.0.10]# echo "PATH=/usr/local/php8/bin:$PATH" > /etc/profile.d/php.sh
[root@Dynamic php-8.0.10]# bash
[root@Dynamic php-8.0.10]# which php
/usr/local/php8/bin/php

# 配置php-fpm
[root@Dynamic php-8.0.10]# cp php.ini-production /etc/php.ini
[root@Dynamic php-8.0.10]# cd sapi/
[root@Dynamic sapi]# cd fpm/
[root@Dynamic fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@Dynamic fpm]# pwd
/usr/local/php-8.0.10/sapi/fpm
[root@Dynamic fpm]# chmod +x /etc/init.d/php-fpm

[root@Dynamic fpm]# cd /usr/local/php8/
[root@Dynamic php8]# ls
bin  etc  include  lib  php  sbin  var
[root@Dynamic php8]# cd etc/
[root@Dynamic etc]# cp php-fpm.conf.default php-fpm.conf
[root@Dynamic etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@Dynamic etc]# cd php-fpm.d
[root@Dynamic php-fpm.d]# ls
www.conf.default
[root@Dynamic php-fpm.d]# cp www.conf.default www.conf
[root@Dynamic php-fpm.d]# ls
www.conf  www.conf.default

# 尝试启动php
[root@Dynamic php-fpm.d]# service php-fpm start
Starting php-fpm  done
[root@Dynamic php-fpm.d]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128              127.0.0.1:9000(php默认端口)              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支持PHP
[root@Dynamic php-fpm.d]# vim  /usr/local/nginx/conf/nginx.conf
......
 43         location / {     
 44             root   html;
 45             index  index.php index.html index.htm;  # 添加index.php
 46         }
......
 65         location ~ \.php$ {                         # 把这几行取消注释
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;    # 修改
 70             include        fastcgi_params;
 71         }
......

# 添加index.php
[root@Dynamic php-fpm.d]# cd /usr/local/nginx/html/
[root@Dynamic html]# ls
50x.html  index.html
[root@Dynamic html]# vi index.php
<?php
        phpinfo();
?>

[root@Dynamic php-fpm.d]# /usr/local/nginx/sbin/nginx -s reload
访问页面是否正常

在这里插入图片描述

Static安装httpd

下载: apache官网.

# 安装***httpd***  
# 下载工具包   
[root@Static soft]# yum groups mark install 'Development Tools'

# 创建用户
[root@Static soft]# useradd -r -M -s /sbin/nologin apache

# 安装依赖包
[root@Static soft]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make wget

[root@Static ~]# mkdir /usr/src/soft
[root@Static ~]# cd /usr/src/soft/

# 下载官网,apache.org

[root@Static soft]# ls
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.48.tar.gz

# 解压
[root@Static soft]# tar xf apr-1.7.0.tar.gz -C /usr/local/
[root@Static soft]# tar xf apr-util-1.6.1.tar.gz  -C /usr/local/
[root@Static soft]# tar xf httpd-2.4.48.tar.gz  -C /usr/local/

# 编译安装apr
[root@Static soft]# cd /usr/local/apr-1.7.0/
[root@Static apr-1.7.0]# vi configure
......
31879     trap "$RM \"$cfgfile\"; exit 1" 1 2 15
31880    #  $RM "$cfgfile"  # 注释掉或删除
......
[root@Static apr-1.7.0]# ./configure --prefix=/usr/local/apr
......
[root@Static apr-1.7.0]# make && make install
......

# 编译安装apr-util
[root@Static apr-1.7.0]# cd ../apr-util-1.6.1/
[root@Static apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
......
[root@Static apr-util-1.6.1]# make && make install
.......

# 编译安装httpd
[root@Static apr-util-1.6.1]# cd ../httpd-2.4.48/
[root@Static httpd-2.4.48]# ./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
......
Server Version: 2.4.48
    Install prefix: /usr/local/apache
    C compiler:     gcc
    CFLAGS:          -g -O2 -pthread  
    CPPFLAGS:        -DLINUX -D_REENTRANT -D_GNU_SOURCE  
    LDFLAGS:           
    LIBS:             
    C preprocessor: gcc -E

[root@Static httpd-2.4.48]# make && make install
.......

# 配置环境变量
[root@Static httpd-2.4.48]# echo "export PATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/httpd.sh
[root@Static httpd-2.4.48]# bash
[root@Static httpd-2.4.48]# which httpd
/usr/local/apache/bin/httpd

# 取消配置文件中ServerName前面的注释
#ServerName提供服务器用于标识自身的名称和端口。
#这通常可以自动确定,但我们建议您指定
#这是为了防止启动过程中出现问题。
#如果您的主机没有注册的DNS名称,请在此处输入其IP地址。
[root@Static httpd-2.4.48]# sed -i 's/^#ServerName www.example.com:80/ServerName www.example.com:80/' /usr/local/apache/conf/httpd.conf

# 设置开机自启
[root@Static httpd-2.4.48]# cd /usr/lib/systemd/system/
[root@Static system]# cp sshd.service httpd.service
[root@Static system]# vi httpd.service
[Unit]
Description=httpd server daemon
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
KillMode=process

[Install]
WantedBy=multi-user.target

[root@Static system]# systemctl daemon-reload
[root@Static system]# systemctl start httpd
[root@Static system]# 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                   [::]:22                   [::]:*                  
LISTEN    0         128                      *:80(httpd默认端口)                      *:*
访问页面是否正常

在这里插入图片描述

配置动静分离

DR上

# 修改nginx配置文件
[root@DR ~]# vi /usr/local/nginx/conf/nginx.conf
......
35     upstream Dynamic {                    # 添加   需要注意的是upstream要添加在server段的上面
36         server 192.168.220.10;            # 添加
37     }
38 
39     upstream Static {                     # 添加
40        server 192.168.220.17;             # 添加
41     }                                     # 添加
42 
43 
44     server {                              # server段
45         listen       80;
46         server_name  localhost;
......
51        # location / {
52        #     root   html;
53        #     index  index.html index.htm;
54        # }
......
68         location ~ \.php$ {                # 添加
69             proxy_pass   http://Dynamic;   # 添加
70         }                                  # 添加
71 
72        location / {                        # 添加
73             proxy_pass http://Static;      # 添加 
74        }                                   # 添加
......
测试效果

当访问根目录的时候默认是跳到静态的httpd,192.168.220.17上

在这里插入图片描述

当访问动态资源时就自动跳到192.168.220.10上

在这里插入图片描述

配置负载均衡

DR上

# 修改nginx配置文件
[root@DR ~]# vi /usr/local/nginx/conf/nginx.conf
......
35     upstream LB {                       # 添加  需要注意的是upstream要添加在server段的上面
36         server 192.168.220.10;          # 添加
37         server 192.168.220.17;          # 添加
38     }
39 
40     server {                            # server段
41        listen       80;
42        server_name  localhost;
43
......
51        # location / {
52        #     root   html;
53        #     index  index.html index.htm;
54        # }
......
72        location / {                   # 添加
73             proxy_pass http://LB;     # 添加
74        }                              # 添加
......     
测试效果

访问

在这里插入图片描述

刷新,每刷新一次就在192.168.220.10和192.168.220.17上跳转

在这里插入图片描述

也可设置权重,让高性能主机的多处理任务

# 比如,修改nginx配置文件
[root@DR ~]# vi /usr/local/nginx/conf/nginx.conf
......
35     upstream LB {                          # 添加   需要注意的是upstream要添加在server段的上面
36         server 192.168.220.10 weight=3;    # 设置权重就会多处理任务
37         server 192.168.220.17;             # 添加
38     }
39 
40     server {                               # server段
41        listen       80;
42        server_name  localhost;
43
......
51        # location / {
52        #     root   html;
53        #     index  index.html index.htm;
54        # }
......
72        location / {                     # 添加
73             proxy_pass http://LB;       # 添加
74        }                                # 添加
......     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值