为了实现mysql独立运行,可以进行拆分lnmp的操作
(1)重新开启一台虚拟机,安装mysql并修改mysql密码
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostname mysql
[root@localhost ~]#bash
[root@mysql ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
[root@mysql ~]# yum -y install mysql-community-server
[root@mysql ~]# systemctl start mysqld
[root@mysql ~]# systemctl enable mysqld
[root@mysql ~]# mysql_secure_installation
(2)在原来的lnmp服务器上的数据库文件导出
数据库备份
[root@nginx ~]# mysqldump -uroot -p --all-databases > `date +%F%H`-mysql-all.sql
Enter password:
[root@nginx ~]# ls
2020-06-2013-mysql.all.sql
(3)在新开启的mysql服务器上导入数据库文件
先把lnmp上产生的mysql文件传送到新开启的mysql服务器上
[root@nginx ~]# scp -r /root/2020-06-2013-mysql.all.sql [email protected]:/root
进行数据的导入
[root@mysql ~]# mysql -uroot -p < 2020-06-2013-mysql.all.sql
Enter password:
[root@mysql ~]# systemctl restart mysqld
(4)在新mysql服务器上创建同名管理用户和密码
[root@mysql ~]# mysql -uroot -pasd123 #登录
mysql> grant all on blog.* to zj@'192.168.229.%' identified by 'asd123';
mysql> grant all on zh.* to li@'192.168.229.%' identified by 'asd123';
[root@mysql ~]# systemctl restart mysqld
(5)在原服务器上修改blog、zh的配置文件,重新指定数据库服务器IP
先查看blog的配置位置
[root@nginx ~]# cd /wordpress/
[root@nginx wordpress]# grep -r asd123
wp-config.php:define('DB_PASSWORD', 'asd123');
[root@nginx wordpress]# vim wp-config.php
主要把MySQL主机IP改为新的服务器IP
/** WordPress数据库的名称 */
define('DB_NAME', 'blog');
/** MySQL数据库用户名 */
define('DB_USER', 'zj');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'asd123');
/** MySQL主机 */
define('DB_HOST', '192.168.229.140');
先查看zh的配置文件位置
[root@nginx wordpress]# cd /zh
[root@nginx zh]# grep -r asd123
system/config/database.php: 'password' => 'asd123',
[root@nginx zh]# vim system/config/database.php
'host' => '192.168.229.140', #改为新MySQL主机IP
'username' => 'li', #MySQL数据库用户名
'password' => 'asd123', #MySQL数据库密码
'dbname' => 'zh', #zh数据库的名称
然后可以进行访问测试,在原服务器上查看访问日志成功迁移数据库。
[root@nginx zh]# tail /var/log/nginx/access.log
(1)再重新启动一台虚拟机,安装php
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostname php
[root@localhost ~]# bash
[root@php ~]# yum -y install epel-release
[root@php ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
然后安装PHP
[root@php ~]# yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache
(2)修改nginx原服务器上的配置文件,重新指向新的php服务器(zh步骤相同)
[root@nginx ~]# vim /etc/nginx/conf.d/blog.conf
修改IP地址指向为新的PHP服务器IP地址
server {
listen 80;
server_name blog.benet.com;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass 192.168.229.141:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx ~]# vim /etc/nginx/conf.d/zh.conf
修改IP地址指向为新的PHP服务器IP地址
server {
listen 80;
server_name zh.benet.com;
root /zh;
index index.php index.html;
location ~ \.php$ {
root /zh;
fastcgi_pass 192.168.229.141:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx ~]# systemctl restart nginx
(3)修改新的php服务器的配置文件
[root@nginx ~]# vim /etc/php-fpm.d/www.conf
定位并修改为:
listen = 192.168.229.141:9000 #新的PHP服务器IP
listen.allowed_clients = 192.168.229.142,192.168.229.134 #允许访问的web服务器
[root@nginx ~]# systemctl restart php-fpm
(4)从nginx服务器复制wordpress和zh的安装目录到新的php服务器
[root@nginx ~]# scp -rp /wordpress [email protected]:/
[root@nginx ~]# scp -rp /zh [email protected]:/
(5)客户端验证访问
客户端需要修改/etc/hosts文件进行域名解析
http://blog.benet.com
http://zh.benet.com
搭建nfs共享服务器,为了把网站静态元素通过挂载方式放在nfs上。
(1)重新开启一台虚拟机,安装nfs-utils、rpcbind
[root@node01 ~]# hostname nfs
[root@node01 ~]# bash
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# setenforce 0
[root@nfs ~]# yum -y install nfs-utils rpcbind
(2)创建挂载点
[root@nfs ~]# mkdir -p /nfs/{blog,zh}
(3)发布共享目录
[root@nfs ~]# vim /etc/exports
添加:
/nfs/blog 192.168.229.0/24(rw,sync,no_root_squash)
/nfs/zh 192.168.229.0/24(rw,sync,no_root_squash)
(4)重启nfs服务
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs
(5)在nginx服务器上查看nfs共享目录
[root@nginx ~]# showmount -e 192.168.229.139
Export list for 192.168.229.139:
/nfs/zh 192.168.229.0/24
/nfs/blog 192.168.229.0/24
(6)在nginx服务器上下载nfs工具并把wordpress的内容目录挂载到nfs
[root@nginx ~]# yum -y install nfs-utils rpcbind
[root@nginx ~]# cd /wordpress/
[root@nginx wordpress]# cp -rp wp-content/ wp-content.bak
[root@nginx wordpress]# mount -t nfs 192.168.229.139:/nfs/blog wp-content
[root@nginx wordpress]# cp -rp wp-content.bak/* wp-content/
[root@nginx wordpress]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 470M 0 470M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 8.1M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17G 5.2G 12G 31% /
/dev/sda1 xfs 1014M 185M 830M 19% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
192.168.229.139:/nfs/blog nfs4 17G 1.7G 16G 10% /wordpress/wp-content
(7)设置永久挂载
[root@nginx ~]# vim /etc/fstab
添加:
192.168.229.139:/nfs/blog /wordpress/wp-content nfs defaults 0 0
(1)重新开启一台虚拟机,安装nginx
[root@localhost ~]# hostname web2
[root@localhost ~]# bash
[root@web2 ~]# systemctl stop firewalld
[root@web2 ~]# setenforce 0
[root@web2 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web2 ~]# yum -y install nginx
(2)把web1上的nginx的配置复制到web2
[root@nginx ~]# scp -rp /etc/nginx/* [email protected]:/etc/nginx
(3)把web1上网页源码复制到web2
[root@nginx ~]# scp -rp /wordpress [email protected]:/
[root@nginx ~]# scp -rp /zh [email protected]:/
(4)启动服务
[root@web2 ~]# systemctl start nginx
[root@web2 ~]# systemctl enable nginx
(1)重新开启一台虚拟机,安装nginx
代理和负载均衡的区别
代理负责把连接请求直接转发到后台某个web节点。
负载均衡负责把请求使用某种调度算法分散发布给后台所有web节点。
nginx代理
[root@localhost ~]# hostname lb
[root@localhost ~]# bash
[root@lb ~]# systemctl stop firewalld
[root@lb ~]# setenforce 0
[root@lb ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@lb ~]# yum -y install nginx
[root@lb ~]# systemctl start nginx
[root@lb ~]# systemctl enable nginx
(2)代理优化配置
1.第一种方法配置
[root@lb ~]# cd /etc/nginx/conf.d
[root@lb conf.d]# mv default.conf default.conf.bak
[root@lb ~]# vim /etc/nginx/conf.d/lb.conf
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://192.168.229.134;
proxy_set_header Host $http_host; #转发请求时,包含头部“HOST”信息
proxy_set_header X-Real-IP $remote_addr; #和下行一起,共同实现追踪客户端原ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30; #代理和后端服务器连接超时时间
proxy_send_timeout 60; #后端服务器传回代理的超时时间
proxy_read_timeout 60; #代理等待后端服务器的响应时间
proxy_buffering on; #启用缓存,后端返回内容先缓存,再给客户端,收到多少转多少
proxy_buffer_size 32k; #代理缓存用户头信息的缓存区大小
proxy_buffers 4 128k; #缓存区的设置
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://192.168.229.134;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
}
}
[root@lb conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb conf.d]# systemctl restart nginx
2.第二种配置方法(推荐使用,服务器数量较多时,配置方便)
[root@lb nginx]# pwd
/etc/nginx
[root@lb nginx]# vim nginx_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb nginx]# vim conf.d/lb.conf
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://192.168.229.134;
include nginx_params; #添加
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://192.168.229.134;
include nginx_params; #添加
}
[root@lb conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb ~]# systemctl restart nginx
(3)客户端修改hosts文件指向lb1,测试访问
[root@client ~]# vim /etc/hosts
nginx负载均衡(Load Balance),简写LB
面对高并发web请求,使用各种调度算法(rr:轮询,wrr:加权轮询,lc最小连接数,wlc:加权最小连接数,ip_hash),分散转发到后台web群集节点,提高数据吞吐量,高容灾。
常见的LB:
软件:lvs nginx haproxy
硬件:F5
云LB:阿里云SLB 腾讯云CLB 青云QLB ucloud ULB
四层负载:ip地址 tcp/udp 端口号
七层负载:HTTP https ftp SMTP
(1)修改lb1的配置文件,添加负载均衡功能
[root@lb ~]# vim /etc/nginx/conf.d/lb.conf
修改为:
upstream webcluster {
server 192.168.229.144:80;
server 192.168.229.143:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://webcluster;
include nginx_params;
}
}
server {
listen 80;
server_name zh.benet.com;
location / {
proxy_pass http://webcluster;
include nginx_params;
}
}
[root@lb ~]# systemctl restart nginx
(2)客户端访问验证,浏览器如果判断不出来,就看web节点上的日志。
(3)nginx负载均衡后端状态
例子:
[root@lb ~]# vim /etc/nginx/conf.d/lb1.conf
修改为:upstream属于http字段
upstream web_cluster {
server 192.168.229.143:80 max_fails=2 fails_timeout=10s max_conns=1;
server 192.168.229.144:80 down; #一般用于停机维护
}
注意:参数不写会有默认值;
参数解释
down 当前节点服务器不参与负载均衡
backup 备份服务器
max_fails 允许请求失败的次数
fails_timeout 经过max_fails失败后,服务的暂停时间
max_conns 同一ip最大连接数
利用跳板机进行其他服务器的管理,达到免密登录的效果
[root@lb ~]# ssh-keygen
[root@lb ~]# ssh-copy-id 需要管理的IP
[root@lb ~]# ssh 需要管理的IP
4层负载均衡:端口映射
[root@lb ~]# vim /etc/nginx/nginx.conf
不属于http字段,所以插入数据到http字段上方:
stream {
upstream sshweb1 {
server 192.168.229.139:22; #管理服务器的群集
}
upstream mysql {
server 192.168.229.140:3306; #管理数据库的群集
}
server {
listen 5555; #效果:远程登录只能通过5555端口连接
proxy_pass sshweb1;
proxy_connect_timeout 30;
proxy_timeout 60;
}
server {
listen 7777; #效果:远程登录只能通过7777端口连接
proxy_pass mysql;
proxy_connect_timeout 30;
proxy_timeout 60;
}
}
[root@lb ~]# systemctl restart nginx
然后访问跳板机IP去登录到web服务器节点
[root@lb ~]# ssh [email protected] -p 5555
connection established.
协议
VRRP(虚拟路由冗余协议) 公有协议 224.0.0.18
HSRP(热备份路由协议) 私有协议,Cisco公司
1.高可用软件
keepalived:使用vrrp实现多台主机高可用群集
2.高可用角色:
master 主服务器
backup 备服务器
实施步骤:
目的:实现两台负载均衡器的高可用
环境:两台负载均衡器
最小化安装需要安装psmisc
[root@localhost ~]# yum -y install psmisc
lb1:192.168.1.117
lb2:192.168.1.118
搭建另一台负载均衡器lb2
把第一台的yum源nginx文件传过来,配置文件/etc/nginx/*也传过来。两台配置一样。
(1)安装keepalived(两台都装)
[root@lb1 ~]# yum -y install keepalived
[root@lb2 ~]# yum -y install keepalived
(2)配置keepalived
主服务器:lb1
[root@lb1 ~]# vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
router_id lb1 #主服务器名
}
vrrp_instance VI_1 {
state MASTER #主服务器
interface ens33 #网卡名称
virtual_router_id 51
priority 100 #优先级0-255
advert_int 1 #心跳线秒数
authentication { #认证标准
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #虚拟IP地址,相当于一个飘移地址,必须是同网段。
192.168.1.254
}
}
[root@lb1 ~]# systemctl restart keepalived
备服务器:lb2
3倍心跳时间收不到master的通知包,backup就会变成master。
[root@lb2 ~]# vim /etc/keepalived/keepalived.conf
修改为:
global_defs {
router_id lb2 #路由id号,和主服务器必须不同
}
vrrp_instance VI_1 {
state BACKUP #状态:BACKUP备 MASTER主
interface ens33
virtual_router_id 51
priority 99 #优先级:备比主要小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254 #虚拟路由ip,公共ip,必须和自己的网段相同。
}
}
[root@lb2 ~]# systemctl restart keepalived
(3)查看虚拟ip(漂移ip地址)
[root@lb1 ~]# ip addr show dev ens33
(4)客户端修改hosts文件,访问验证(访问成功,关闭主服务器,再访问)
[root@client ~]# vim /etc/hosts
修改为:
192.168.1.254 blog.benet.com
192.168.1.254 zh.benet.com
3.高可用裂脑
高可用节点之间互相失去联系,自认为自己是主服务器,就会出现多主现象,即裂脑现象。
裂脑出现的原因:
(1)心跳线松动或网卡故障
(2)服务器硬件故障,崩溃
(3)节点服务器开启防火墙,却没有做vrrp例外
(4)nginx服务死掉,不会出现裂脑现象,但整个集群都无法正常运作
(1)检测裂脑脚本(在备用服务器运行)
[root@lb2 ~]# vim split_brain.sh
#!/bin/bash
while true
do
ping -c 2 -W 3 192.168.1.117(主服务器IP) &> /dev/null
if [ $? -eq 0 -a `ip add | grep 192.168.1.254(飘移地址)|wc -l` -eq 1 ]
then
echo "split brain....."
else
echo "HA is ok"
fi
sleep 5
done
[root@lb2 ~]# chmod +x split_brain.sh
[root@lb2 ~]# bash split_brain.sh
lb1和lb2开启防火墙验证:
[root@lb1 ~]# systemctl start firewalld
[root@lb2 ~]# systemctl start firewalld
出现裂脑现象,解决因为防火墙出现的裂脑现象:
[root@lb1 ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
[root@lb1 ~]# firewall-cmd --reload
[root@lb2 ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
[root@lb2 ~]# firewall-cmd --reload
脚本显示裂脑现象成功消除。
(2)解决nginx故障造成群集无法工作
编辑nginx监控脚本
[root@lb1 ~]# mkdir /sh
[root@lb1 ~]# vim /sh/check_nginx_proxy.sh
#!/bin/bash
killall -0 nginx
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
[root@lb1 ~]# chmod +x /sh/check_nginx_proxy.sh
添加脚本追踪模块到keepalived配置文件
[root@lb1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb1
}
vrrp_script check_nginx_proxy {
script "/sh/check_nginx_proxy.sh"
interval 2
weight 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
track_script {
check_nginx_proxy
}
}
[root@lb1 ~]# systemctl restart keepalived
SSL:安全套接字层,由Netscape公司于1994年创建,它旨在通过Web创建安全的Internet通信。
它是一种标准协议,用于加密浏览器和服务器之间的通信。它允许通过Internet安全轻松地传输账号密码、银行卡、手机号等私密信息。
SSL常见应用:
https:启用ssl加密的安全HTTP传输协议 443端口
ipsec
PKI:公钥基础设施,主要功能是绑定证书持有者的身份和相关的密钥对(通过为公钥及相关的用户身份信息签发数字证书),为用户提供方便的证书申请、证书作废、证书获取、证书状态查询的途径,并利用数字证书及相关的各种服务(证书发布,黑名单发布,时间戳服务等),实现通信中各实体的私钥(身份认证、完整性、抗抵赖性)和保密性(公钥)。
标准:x.509
CA:证书颁发机构
RA:证书注册机构
证书的内容:
申请者的公钥
申请者的身份标识
证书有效期
颁发者的标识
颁发者的签名
HTTPS证书的选择
专业版OV型 不显示企业名
高级版EV型 显示企业名
HTTPS证书购买选择
单域名:仅能绑定一个域名
多域名:能绑定五个域名
通配符域名:不限个数
HTTPS注意事项
https仅支持二级域名
https不支持续费,证书到期重新申请替换
https显示绿色,说明整个网站都是https的
https显示黄色,网站代码中包含https不安全链接
https显示红色,证书不认或过期
企业内部实现https案例:
生成key密钥
生成证书签名请求文件(csr文件)
生成证书签名文件(ca文件)
例子:在web1上操作
1.查看是否安装openssl和版本
[root@web1-152 ~]# rpm -q openssl
[root@web1-152 ~]# yum -y install openssl
2.查看nginx是否安装ssl模块
[root@web1-152 ~]# nginx -V
显示结果包含: --with-http_ssl_module
3.创建ssl密钥目录,并进入目录
[root@web1-152 ~]# mkdir -p /etc/nginx/ssh_key
[root@web1-152 ssh_key]# cd /etc/nginx/ssh_key
4.本机当CA:证书颁发机构,创建私钥
[root@web1-152 ssh_key]# openssl genrsa -idea -out server.key 2048
5.生成证书,去掉私钥的密码
[root@web1-152 ssh_key]# openssl req -days 3650 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
6.配置https网站
[root@web1-152 ssh_key]# vim /etc/nginx/conf.d/https.conf
server {
listen 443 ssl;
server_name https.benet.com;
ssl_certificate ssh_key/server.crt;
ssl_certificate_key ssh_key/server.key;
location / {
root /httpsweb;
index index.html;
}
}
[root@web1-152 ssh_key]# mkdir /httpsweb
[root@web1-152 ssh_key]# echo "https.benet.com
" > /httpsweb/index.html
[root@web1-152 ssh_key]# systemctl restart nginx
7.客户机修改hosts文件,使用https://https.benet.com访问测试。
[root@web1-152 ~]# vim /etc/hosts
192.168.229.152 https.benet.com
8.rewrite地址重写(http重定向到https)
[root@web1-152 ~]# vim /etc/nginx/conf.d/https.conf
server {
listen 443 ssl;
server_name https.benet.com;
ssl_certificate ssh_key/server.crt;
ssl_certificate_key ssh_key/server.key;
location / {
root /httpsweb;
index index.html;
}
}
server {
listen 80;
server_name https.benet.com;
rewrite .* https://https.benet.com;
rewrite .* https://$host$request_uri redirect;
rewrite .* https://$server_name$request_uri redirect;
rewrite .* https://$server_name$1 redirect;
}
9.配置负载均衡https
[root@lb1 ~]# vim /etc/nginx/conf.d/lb_https.conf
upstream webhttps {
server 192.168.229.152:443;
server 192.168.229.155:443;
}
server {
listen 443 ssl;
server_name https.benet.com;
ssl_certificate ssh_key/server.crt;
ssl_certificate_key ssh_key/server.key;
location / {
proxy_pass https://webhttps;
include nginx_params;
}
}
server {
listen 80;
server_name https.benet.com;
return 302 https://$server_name$1;
}
10.模拟案例:配置https的blog、zh(web2和web1配置相同)
(1)配置web1的blog
[root@web1-152 ~]# vim /etc/nginx/conf.d/blog.conf
server {
listen 443 ssl;
server_name blog.benet.com;
ssl_certificate ssh_key/server.crt;
ssl_certificate_key ssh_key/server.key;
root /wordpress;
index index.php index.html;
location ~ \.php$ {
root /wordpress;
fastcgi_pass 192.168.229.157:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name blog.benet.com;
rewrite .* https://blog.benet.com;
rewrite .* https://$host$request_uri redirect;
rewrite .* https://$server_name$request_uri redirect;
rewrite .* https://$server_name$1 redirect;
}
(2)配置web1的zh
[root@web1-152 ~]# vim /etc/nginx/conf.d/zh.conf
server {
listen 443 ssl;
server_name zh.benet.com;
ssl_certificate ssh_key/server.crt;
ssl_certificate_key ssh_key/server.key;
root /zh;
index index.php index.html;
location ~ \.php$ {
root /zh;
fastcgi_pass 192.168.229.157:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name zh.benet.com;
rewrite .* https://zh.benet.com;
rewrite .* https://$host$request_uri redirect;
rewrite .* https://$server_name$request_uri redirect;
rewrite .* https://$server_name$1 redirect;
}
[root@web1-152 ~]# nginx -t
[root@web1-152 ~]# systemctl restart nginx
直接把web1的配置传到web2上
[root@web1-152 ~]# scp -rp /etc/nginx/ssh_key [email protected]:/etc/nginx
[root@web1-152 ~]# scp -rp /etc/nginx/conf.d/blog.conf [email protected]:/etc/nginx/conf.d/
[root@web1-152 ~]# scp -rp /etc/nginx/conf.d/zh.conf [email protected]:/etc/nginx/conf.d/
(3)配置负载均衡lb1,lb2配置一样
首先把证书传到lb1上
[root@web1-152 ~]# scp -rp /etc/nginx/ssh_key/ [email protected]:/etc/nginx/
[root@lb1 ~]# vim /etc/nginx/conf.d/lb.conf
upstream web_cluster {
server 192.168.229.152:443;
server 192.168.229.155:443;
}
server {
listen 443 ssl;
server_name blog.benet.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass https://web_cluster;
include nginx_params;
}
}
server {
listen 443 ssl;
server_name zh.benet.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass https://web_cluster;
include nginx_params;
}
}
server {
listen 80;
server_name blog.benet.com;
return 302 https://$server_name$1;
}
server {
listen 80;
server_name zh.benet.com;
return 302 https://$server_name$1;
}
[root@lb1 ~]# nginx -t
[root@lb1 ~]# systemctl restart nginx
关于lb2的配置,直接把lb1的配置传过去即可。
[root@lb1 ~]# scp -rp /etc/nginx/ssh_key/ [email protected]:/etc/nginx/
[root@lb1 ~]# scp -rp /etc/nginx/conf.d/lb.conf [email protected]:/etc/nginx/conf.d/