Nginx负载均衡和动静分离
1.反向代理与负载均衡
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 kiwi123.com {
ip_hash;
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。
定义好upstream
后,需要在server
段内添加如下内容:
server {
location / {
proxy_pass http://kiwi123.com;
}
}
2. 开始部署
实战说明:一个Nginx静态集群,一个LNMP集群,一台Nginx负载均衡服务器,当用户访问负载均衡服务器的时候,负载均衡服务器会做转发,转发的同时做动静分离
环境说明
主机名 | ip | 角色 | 系统版本 |
---|---|---|---|
LB | 192.168.234.22 | 负载均衡 | CentOS8 |
LNMP | 192.168.234.123 | LNMP动态服务器 | CentOS8 |
nginx | 192.168.234.33 | nginx静态服务器 | CentOS8 |
2.1 部署静态nginx
//关闭防火墙及SELinux
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
//获取nginx yum源
[root@nginx ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
//开始安装nginx
[root@nginx ~]# yum install -y nginx
//启动nginx并设置开机自启
[root@nginx ~]# systemctl enable --now nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 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@nginx ~]#
//默认主配置文件在
[root@LNMP ~]# vim /etc/nginx/nginx.conf
2.2 部署动态nginx LNMP
//关闭防火墙
[root@LNMP ~]# systemctl disable --now firewalld
[root@LNMP ~]# setenforce 0
[root@LNMP ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
//获取nginx yum源
[root@LNMP ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
//安装并设置开机自启nginx
[root@LNMP ~]# yum install -y nginx
[root@LNMP ~]# systemctl enable --now nginx
[root@LNMP ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 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 [::]:*
//安装MySQL8.0.33
//获取MySQL包
[root@LNMP ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
//创建MySQL用户
[root@LNMP ~]# useradd -r -M -s /sbin/nologin mysql
//解压MySQL包
[root@LNMP ~]# tar xf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz -C /usr/local/
//重命名
[root@LNMP ~]# cd /usr/local/
[root@LNMP local]# mv mysql-8.0.33-linux-glibc2.28-x86_64/ mysql
[root@LNMP local]# ls
bin etc games include lib lib64 libexec mysql sbin share src
//修改权限
[root@LNMP ~]# chown -R mysql.mysql /usr/local/mysql
//添加环境变量
[root@LNMP ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@LNMP ~]# source /etc/profile.d/mysql.sh
//创建MySQL数据存放目录
[root@LNMP ~]# mkdir -p /data/mysql
//初始化数据库
[root@LNMP ~]# mysqld --initialize --user mysql --datadir /data/mysql
//添加MySQL主配置文件
[root@LNMP ~]# vim /etc/my.cnf
[root@LNMP ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
socket = /tmp/mysql.sock
port = 3306
pid-file = /data/mysql/mysql.pid
user = mysql
skip-name-resolve
//复制配置文件
[root@LNMP ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
//修改配置文件
[root@LNMP ~]# vim /etc/init.d/mysqld
····················略
basedir=/usr/local/mysql ## 修改这行
datadir=/data/mysql ## 修改这行
····················略
//启动数据库
[root@LNMP ~]# service mysqld start
//修改MySQL密码
[root@LNMP ~]# mysql -uroot -pbfCrl_9/yW.L
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '1';
[root@LNMP ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 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 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
//开始安装PHP8.2.11
//获取php包
[root@LNMP ~]# wget https://www.php.net/distributions/php-8.2.11.tar.gz
//安装php依赖环境
[root@LNMP ~]# yum install -y pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make libzip libzip-devel sqlite-devel oniguruma* libxml2-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 php-mysqlnd
[root@LNMP ~]# yum install -y http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//解压安装包并进入
[root@LNMP ~]# tar xf php-8.2.11.tar.gz && cd php-8.2.11
[root@LNMP php-8.2.11]#
//开始编译
[root@LNMP php-8.2.11]# ./configure --prefix=/usr/local/php \
--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 &&\
make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@LNMP php-8.2.11]# echo 'export PATH=/usr/local/php/bin:$PATH' > /etc/profile.d/php.sh
[root@LNMP php-8.2.11]# source /etc/profile.d/php.sh
//复制程序文件
[root@LNMP php-8.2.11]# cp php.ini-production /etc/php.ini
[root@LNMP php-8.2.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
//添加执行权限
[root@LNMP php-8.2.11]# chmod +x /etc/rc.d/init.d/php-fpm
//复制php-fpm.conf
[root@LNMP php-8.2.11]# cd /usr/local/php/etc
[root@LNMP etc]# cp php-fpm.conf.default php-fpm.conf
//复制php-fpm.d/www.conf
[root@LNMP etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
//添加优化配置
[root@LNMP etc]# vim /usr/local/php/etc/php-fpm.conf
## 在文件的末尾添加以下内容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
//创建php页面
[root@LNMP ~]# cd /usr/share/nginx/html/
[root@LNMP html]# vim index.php
[root@LNMP html]# cat index.php
<?php
phpinfo();
?>
//启动php服务
[root@LNMP ~]# /etc/rc.d/init.d/php-fpm start
//修改nginx配置文件
[root@LNMP ~]# vim /etc/nginx/conf.d/default.conf
···················略
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm; ## 修改这一行
}
···················略
##取消以下内容的注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; ##修改这一行
include fastcgi_params;
}
····················略
//重启服务
[root@LNMP ~]# systemctl restart nginx
2.3 部署动态nginx负载均衡
//关闭防火墙以及SELinux
[root@LB ~]# systemctl disable --now firewalld
[root@LB ~]# setenforce 0
[root@LB ~]# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
//配置ngin yum源
[root@LB ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
//安装nginx并设置开机自启
[root@LB ~]# yum install -y nginx
[root@LB ~]# systemctl enable --now nginx
//修改配置文件添加动静态
[root@LB ~]# vim /etc/nginx/conf.d/default.conf
## 添加以下内容
upstream dynamic {
server 192.168.234.123:80 weight=1;
}
upstream static {
server 192.168.234.33:80 weight=1;
}
location / { ## 修改
proxy_pass http://static; ## 把原来的root之类的东西删掉改为这个
} ## 修改
location ~ \.php$ { ##取消注释
proxy_pass http://dynamic; ##修改这一行
} ##取消注释
在web端访问
静态页面
动态页面
注意:负载均衡服务器本地没有图片,转发动态页面时要将动态资源服务器的图片同步到静态资源服务器,因为当负载均衡请求静态资源是去静态服务器请求的,而图片就是静态资源。