Linux运维课程5.1——LAMP架构1
文章目录
- Linux运维课程5.1——LAMP架构1
- Nginx服务管理
- 负载均衡
- 概念
- 作用
- 5 种调度算法
- 1. 轮询(默认)
- 2. weight 加权轮询
- 3. ip_hash
- 4. least_conn 最少连接
- 5. url_hash(第三方模块,默认不自带)
- 配置
- nginx模块编译
- 安全控制
- 通用概念
- 分类整理
- 1. 系统层面安全控制(CentOS)
- 2. Nginx Web 安全控制
- 限制并发连接
- 限制请求数
- 限制速率
- 目录自动索引
- nginx缓存配置
- 禁用日志
- 日志轮换
- nginx日志可视化
- 站点限制
- 中文乱码
- 虚拟主机
- 概念
- 三种实现方式
- 基于域名
- 基于端口
- 基于 IP
- 作用
- 匹配优先级
- 配置
- https配置
- 概念
- 四大核心作用
- 简单流程
- 配置
- 重定向
- 概念
- 核心作用
- 两种重定向区分
- 配置
- 防盗链
- 概念
- 作用
- 配置
- PHP
- 概念
- 作用
- 经典架构
- PHP部署
- 安装并编译
- 安装libzip
- openssl
- 安装成功后配置PHP
- 问题
- PHP联合nginx
- PHP动态扩展模块
- 添加PHP环境变量
- 模块下载
- memcache下载
- 问题
- nginx高速缓存
- 概念
- 作用
- 核心指令
- nginx配置高速缓存
- openresty部署
- session共享
- 概念
- 作用
- 常见 4 种实现方案
- 配置tomcat
- nginx联合tomcat
- tomcat联合memcached
Linux运维课程5.1——LAMP架构1
Nginx服务管理
负载均衡
概念
Nginx 通过ngx_http_upstream_module模块实现负载均衡。
将客户端请求分发到后端多台应用服务器,实现请求分流,提升整体并发、实现高可用。
结构:客户端 → Nginx (代理) → 后端一组服务器
作用
- 请求分发:分摊流量,避免单台服务器压力过大
- 故障转移:后端某台服务器宕机,自动不再转发请求
- 弹性扩容:后端可随时新增节点
- 隐藏后端真实服务器,提升安全
5 种调度算法
1. 轮询(默认)
请求依次轮流分配给每台后端服务器。
upstream web { server 192.168.234.135; server 192.168.234.136; }2. weight 加权轮询
权重越高,分到的请求越多,适合服务器性能不一样场景。
upstream web { server 192.168.234.135 weight=3; server 192.168.234.136 weight=1; }3. ip_hash
根据客户端 IP 哈希计算,同一个客户端始终访问同一台后端。
适用场景:会话保持(session 持久)
upstream web { ip_hash; server 192.168.234.135; server 192.168.234.136; }缺点:后端下线,哈希分配会重新打乱。
4. least_conn 最少连接
请求转发给当前活跃连接最少的服务器。
5. url_hash(第三方模块,默认不自带)
根据访问 URL 分配缓存服务器。
配置
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.confnginx -t nginx -s reload在server4上访问
浏览器访问
nginx模块编译
[root@server4 ~]# tar zxf nginx-1.28.3.tar.gz [root@server4 ~]# tar zxf nginx_sticky_module_ng-0.0.2.tar.gz [root@server4 ~]# cd nginx-1.28.3/静态编译,模块与nginx主程序编译到一起
[root@server4 nginx-1.28.3]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_sticky_module_ng-0.0.2 [root@server4 nginx-1.28.3]# make && make install动态编译,nginx主程序与模块分开编译
[root@server4 nginx-1.28.3]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-compat --add-dynamic-module=../nginx_sticky_module_ng-0.0.2 [root@server4 nginx-1.28.3]# make [root@server4 nginx-1.28.3]# cd objs/ [root@server4 objs]# cp -f nginx /opt/nginx/sbin/nginx [root@server4 objs]# mkdir /opt/nginx/modules [root@server4 objs]# cp ngx_http_sticky_module.so /opt/nginx/modules/在主配置文件中加载模块
[root@server4 ~]# vim /opt/nginx/conf/nginx.conf load_module modules/ngx_http_sticky_module.so;检测语法
[root@server4 objs]# /opt/nginx/sbin/nginx -t安全控制
通用概念
安全控制:通过各类策略、权限、配置、防护手段,减少服务器攻击面,防止非法访问、越权操作、数据泄露、恶意攻击。
分类整理
1. 系统层面安全控制(CentOS)
- 账号安全
- 禁用 root 远程登录;创建普通运维账号
- 清理无用账号、锁定闲置账户
passwd -l - 设置强密码策略、定期更换密码
- 权限控制
- 文件最小权限原则:755 目录,644 文件,重要配置 600
- sudo 权限精细化分配,不随便给 all 权限
防火墙
firewalld/iptables:只开放业务端口,关闭不必要端口
SELinux
生产按需配置,防止进程越权访问文件
关闭无用服务
停止、禁用不需要的自启动服务,缩小攻击面
日志审计
rsyslog 记录系统日志,便于入侵排查
2. Nginx Web 安全控制
- 隐藏版本号
server_tokens off; - 禁止目录浏览(autoindex 关闭)
- 访问控制:allow/deny IP 黑白名单
- 基础认证(账号密码访问页面)
- 防 CC、限制连接数
limit_conn、限制请求limit_req - HTTPS 加密访问,禁止 HTTP 明文传输
- 防止跨站、请求头过滤
先将一张照片拖入server1中并命名为test.png
限制并发连接
[root@server1 nginx]# cd html/ [root@server1 html]# mkdir download [root@server1 ~]# cp test.png /usr/local/nginx/html/download/ [root@server1 nginx]# vim conf/nginx.conf[root@server1 nginx]# nginx -t [root@server1 nginx]# nginx -s reload[root@server4 ~]# ab -c 10 -n 10 http://192.168.36.132/download/test.png查看日志
[root@server1 nginx]# cat logs/access.log限制请求数
[root@server1 nginx]# vim conf/nginx.conf http { ... limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { location /download/ { limit_conn addr 1; limit_req zone=one burst=5 nodelay; } } }[root@server1 nginx]# nginx -s reload测试
[root@server4 ~]# ab -c 1 -n 10 http://192.168.36.132/download/test.png限制速率
[root@server1 nginx]# vim conf/nginx.conf http { ... server { location /download/ { limit_conn addr 1; limit_rate 100k; } } }[root@server1 nginx]# nginx -s reload测试
[root@server4 ~]# ab -c 1 -n 5 http://192.168.36.132/download/test.png目录自动索引
浏览器直接访问192.168.234.135/download
[root@server1 nginx]# vim conf/nginx.conf location /dowmload/ { ... autoindex on; ... }nginx -t nginx -s reload在浏览器再次访问,如图
nginx缓存配置
vim /usr/local/nginx/conf/nginx.conf加入以下配置
location ~ .*\.(gif|jpg|png)$ { expires 365d; root html; }nginx -t nginx -s reload curl -I http://192.168.234.135/download/test.png禁用日志
日志轮换
[root@server1 ~]# vim /opt/nginx_log.sh #!/bin/bash cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`[root@server1 ~]# chmod +x /opt/nginx_log.sh [root@server1 ~]# /opt/nginx_log.sh [root@server1 ~]# cd /usr/local/nginx/logs/ [root@server1 logs]# ls结合crontab周期化调用
[root@server1 ~]# crontab -e 00 00 * * * /opt/nginx_log.shnginx日志可视化
安装依赖性
[root@server1 ~]# yum install -y ncurses-devel GeoIP-devel下载安装包
wget https://tar.goaccess.io/goaccess-1.8.tar.gz解压缩、编译并安装
[root@server1 ~]# tar xf goaccess-1.8.tar.gz [root@server1 ~]# cd goaccess-1.8/ [root@server1 goaccess-1.8]# ./configure --enable-utf8 --enable-geoip=mmdb [root@server1 goaccess-1.8]# make [root@server1 goaccess-1.8]# make install编译成功
启动
[root@server1 ~]# goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &访问:http://192.168.234.135/report.html
站点限制
vim /usr/local/nginx/conf/nginx.conflocation /status { stub_status on; #启用监控模块 access_log off; #禁用日志记录 allow 127.0.0.1; #只允许本机访问 deny all; #禁用所有主机访问 }nginx -t nginx -s reload curl localhost/status #只有主机能访问中文乱码
echo 欢迎!> /usr/local/nginx/html/index.html刷新页面发现为乱码
vim /usr/local/nginx/conf/nginx.conf charste uft-8;#写入配置文件,如图nginx -t nginx -s reload虚拟主机
概念
虚拟主机:一台 Nginx 服务器,利用不同标识,同时部署多个独立网站。
外界看起来是多台服务器,实际运行在同一台机器,共享服务器资源。
三种实现方式
基于域名
同一个 IP、同一个端口,依靠请求头Host域名区分站点。
访问不同域名,打开不同网站。
server { listen 80; server_name www.a.com; root /html/a; } server { listen 80; server_name www.b.com; root /html/b; }基于端口
同一个 IP、不同端口区分网站。通过IP:端口访问。
server { listen 80; server_name 192.168.234.135; root /html/a; } server { listen 8080; server_name 192.168.234.135; root /html/b; }基于 IP
服务器配置多个网卡 IP,不同 IP 对应不同网站。
企业很少使用,需要多个 IP 资源。
server { listen 192.168.234.135:80; root /html/a; } server { listen 192.168.234.136:80; root /html/b; }作用
- 一台服务器搭建多个网站,充分利用硬件资源,节约成本
- 各个站点相互独立,网站目录、配置互不干扰
匹配优先级
- 先匹配精确 IP + 端口
- 匹配域名(server_name)
- 找不到匹配项,使用第一个 server 区块作为默认站点
配置
[root@server1 nginx]# mkdir /www1/ [root@server1 nginx]# echo web1 > /www1/index.html [root@server1 nginx]# vim conf/nginx.conf http { ... server { listen 80; server_name www1.westos.org; location / { root /www1; index index.html; } } } [root@server1 nginx]# nginx -s reload测试
[root@server4 ~]# vim /etc/hosts[root@server4 ~]# curl www1.westos.orghttps配置
概念
HTTPS = HTTP + SSL/TLS
HTTP 明文传输,HTTPS 在应用层与 TCP 之间增加TLS 加密层,实现加密通信,默认端口 443(HTTP 默认 80)。
四大核心作用
数据加密
客户端和服务器之间传输的数据密文传输,防止抓包窃听,账号、密码不会被直接捕获。
身份认证
依靠 CA 数字证书验证服务器身份,防止访问钓鱼网站、中间人伪装服务器。
数据完整性校验
传输过程数据被篡改(劫持、修改内容),客户端能够检测出来,拒绝接收。
防止中间人攻击
简单流程
- 客户端发起连接请求
- 服务器下发 SSL 证书
- 客户端验证证书合法性
- 协商加密套件,生成会话密钥
- 后续 HTTP 数据使用对称密钥加密传输
配置
[root@server1 conf]# cd /etc/pki/tls/certs [root@server1 certs]# make cert.pem[root@server1 certs]# mv cert.pem /usr/local/nginx/conf/ [root@server1 conf]# vim nginx.conf[root@server1 conf]# nginx -s reload重定向
概念
重定向(Rewrite):服务器收到请求后,通知浏览器访问新的 URL 地址,实现页面跳转。
依靠 Nginxrewrite指令,基于正则匹配 URI 实现跳转。
核心作用
HTTP 跳转 HTTPS
强制所有明文访问升级加密访问,保障安全。
网站目录迁移、页面改版
页面地址变更,旧网址自动跳转到新地址,避免 404。
域名变更
旧域名跳转到新域名;不带 www 跳转到 www 域名。
防盗链、URL 美化
隐藏真实后台地址,实现伪静态。
两种重定向区分
302 临时重定向
临时跳转,浏览器缓存不记录跳转规则;下次依旧请求原地址。
301 永久重定向
永久跳转,浏览器缓存跳转关系;浏览器后续直接访问新地址。
配置
80重定向到443
[root@server1 conf]# vim nginx.conf ... server { listen 80; server_name www1.westos.org; rewrite ^/(.*)$ https://www1.westos.org/$1 permanent; location / { root /www1; index index.html; } } ... [root@server1 conf]# nginx -s reload[root@server1 conf]# curl -I www1.westos.org [root@server1 conf]# curl -I www1.westos.org/index.htmlwww1.westos.org/bbs 重定向bbs.westos.org
[root@server1 conf]# mkdir /bbs [root@server1 conf]# echo bbs.westos.org > /bbs/index.html [root@server1 conf]# vim nginx.conf[root@server1 conf]# nginx -t [root@server1 conf]# nginx -s reload防盗链
概念
盗链:其他网站直接引用你服务器上图片、视频、附件等资源,消耗你的服务器流量与带宽,却不给你带来访问流量。
防盗链:Nginx 通过Referer 请求头判断来源站点,阻止非授权网站引用资源。
Referer:浏览器发起资源请求时,携带的来源页面地址。
作用
- 防止外部网站盗用图片、视频、静态资源
- 节约服务器带宽、降低流量消耗
- 保护静态资源版权
配置
server2上配置server1的防盗链
[root@sever2 ~]# cd /var/www/html/ [root@sever2 html]# vim index.html <html> <body> <img src="http://www1.westos.org/vim.jpg"/> </body> </html>[root@server1 conf]# vim nginx.conf ... location ~ \.(jpg|png)$ { root /www1; valid_referers none blocked www1.westos.org; if ($invalid_referer) { #return 403; rewrite ^/ http://bbs.westos.org/daolian.jpg; } }PHP
概念
PHP(Hypertext Preprocessor,超文本预处理器)
是服务器端开源脚本语言,嵌入 HTML 中运行,主要用于开发动态网页。
运行模式:浏览器访问页面 → Web 服务器 (Nginx/Apache) 调用 PHP 解析器执行代码 → 生成静态 HTML 返回浏览器。
作用
开发动态网站
实现网页动态内容:登录、注册、查询数据、留言、后台管理系统。
和数据库交互
常搭配 MySQL,实现数据增删改查(LAMP/LEMP 架构)。
处理表单
接收用户提交账号、密码、搜索内容等表单数据。
文件处理、上传下载
实现图片上传、文件读写。
会话管理
Cookie、Session 保持用户登录状态。
经典架构
- LAMP:Linux + Apache + MySQL + PHP
- LEMP:Linux + Nginx + MySQL + PHP-FPM
PHP不能直接浏览器打开,必须依靠 Web 服务器 + php-fpm 解析执行。
PHP部署
安装并编译
tar zxf php-8.3.32 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-cli --enable-opcache --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --enable-xml --enable-gd --with-zip=/usr/local/libzip --with-curl --with-jpeg --with-freetype --with-openssl=/usr/local/openssl --enable-bcmath --enable-soap --enable-sockets --enable-exif --with-readline --with-zlib其中会有提示缺少的安装包,找到安装包所满足的版本安装即可
安装libzip
(php编译提示所需的安装包,需特殊配置)
tar -xf libzip-1.9.2.tar.gz cd libzip-1.9.2 mkdir build && cd build为了编译libzip升级cmake为cmake3并且创建cmake与cmake3的软链接
yum install -y cmake3 ln -s /usr/bin/cmake3 /usr/bin/cmake cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/libzip \ -DBUILD_SHARED_LIBS=ON \ make j2 && make install安装后注册动态链接库
echo "/usr/local/libzip/lib64" > /etc/ld.so.conf.d/libzip.conf ldconfig环境变量
export PKG_CONFIG_PATH=/usr/local/libzip/lib64/pkgconfig 验证是否识别成功 pkg-config --modversion libzipmake -j2openssl
php再次编译,到openssl开始报错,因为系统上有旧版openssl
删除旧版
yum remove -y openssl-devel安装适合版本
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz tar -xf openssl-1.1.1w.tar.gz cd openssl-1.1.1w ./config shared zlib --prefix=/usr/local/openssl make -j$(nproc) make install环境变量
export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig:$PKG_CONFIG_PATH动态链接库注册
echo "/usr/local/openssl/lib" > /etc/ld.so.conf.d/openssl.conf ldconfig验证pkg-config能否识别
pkg-config --modversion openssl再次PHP检查,没有问题后进行编译安装
make && make install安装成功后配置PHP
[root@server1 php-7.4.12]# cd /usr/local/php/etc [root@server1 etc]# cp php-fpm.conf.default php-fpm.conf [root@server1 etc]# vim php-fpm.conf去掉注释
pid = run/php-fpm.pid[root@server1 etc]# cd php-fpm.d/ [root@server1 php-fpm.d]# cp www.conf.default www.conf拷贝主配置文件
[root@server1 ~]# cd php-7.4.12/ [root@server1 php-7.4.12]# cp php.ini-production /usr/local/php/etc/php.ini [root@server1 php-7.4.12]# vim /usr/local/php/etc/php.ini [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = Asia/Shanghai #修改时区[root@server1 php-7.4.12]# cd sapi/fpm [root@server1 fpm]# cp php-fpm.service /usr/lib/systemd/system [root@server1 fpm]# vim /usr/lib/systemd/system/php-fpm.service注释此行
#ProtectSystem=full[root@server1 fpm]# systemctl daemon-reload [root@server1 fpm]# systemctl start php-fpm.service [root@server1 fpm]# netstat -antlp|grep :9000[root@server1 fpm]# systemctl enable php-fpm问题
系统默认用的是之前安装的旧版
删除旧版
yum remove php* -y rm -f /usr/bin/php /usr/bin/phpize /usr/bin/php-configPHP联合nginx
[root@server1 sapi]# cd /usr/local/nginx/conf/ [root@server1 conf]# vim nginx.conf[root@server1 conf]# vim /usr/local/nginx/html/index.php <?php phpinfo() ?>问题
访问http://192.168.234.135/index.php浏览页返回404
原因:访问错网页,vim.comf文件中index.php地址在www.westos.org/index.php中,而非192.168.234.135,具体情况需要根据location所在的服务器网址而定
PHP动态扩展模块
添加PHP环境变量
vim .bash_profile PATH=$PATH:$HOME/bin:/usr/local/php/bin#添加模块下载
memcache下载
[root@server3 ~]# tar xf memcache-8.2 [root@server3 ~]# cd memcache-8.2/ [root@server3 memcache-8.2]# yum install -y autoconf [root@server3 memcache-8.2]# phpize[root@server3 memcache-8.2]# ./configure [root@server3 memcache-8.2]# make [root@server3 memcache-8.2]# make install[root@server3 no-debug-non-zts-20230831]# cd /usr/local/php/etc [root@server3 etc]# vim php.ini extension=mencache #添加memcache模块[root@server3 etc]# systemctl reload php-fpm [root@server3 etc]# php -m |grep memcache[root@server3 etc]# cd [root@server3 ~]# cd memcache-8.2/ [root@server3 memcache-8.2]# cp example.php memcache.php /usr/local/nginx/html/ [root@server3 memcache-8.2]# cd /usr/local/nginx/html/ [root@server3 html]# yum install -y memcached[root@server3 html]# systemctl enable --now memcached [root@server3 html]# netstat -antlp|grep :11211[root@server3 html]# cd /usr/local/nginx/html/ [root@server3 html]# vim memcache.php使用www.westos.org/example.php测试
查看缓存状态www.westos.org/memcache.php
用刚在文件里设置的用户名密码进行登录
问题
改变路径后路径不对
vim .bash_profile export PATH=/usr/local/php/bin:$PATHnginx高速缓存
概念
Nginx 代理缓存:Nginx 将后端服务器响应的数据(页面、图片、接口数据)存放在本地磁盘。
当后续有相同请求到达时,Nginx 直接返回缓存数据,不用再次转发请求到后端服务器。
作用
- 减轻后端服务器压力,减少后端访问量
- 提升访问速度,用户响应更快
- 降低后端 CPU、内存、数据库负载
- 后端短暂故障时,可利用缓存临时提供数据
核心指令
proxy_cache_path:定义缓存存放目录、大小、层级、有效期、内存缓冲区proxy_cache:启用缓存,调用上面定义的缓存空间proxy_cache_valid:设置不同类型响应码的缓存时长proxy_cache_key:缓存的索引 key,默认$scheme$proxy_host$request_uri
nginx配置高速缓存
[root@server3 nginx-1.30.4]# nginx -s stop [root@server3 nginx-1.30.4]# make clean [root@server3 nginx-1.30.4]# nginx -V #查看原本编译参数解压缩三个新模块(需提前下载)
网络安装好后格式为zip,需要用到unzip解压
[root@server3 nginx-1.30.4]# ./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../echo-nginx-module-master --add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master --with-cc-opt="-I/usr/local/openssl/include" --with-ld-opt="-L/usr/local/openssl/lib -Wl,-rpath=/usr/local/openssl/lib"编译完成后
[root@server3 nginx-1.30.4]# cd objs/ [root@server3 objs]# du -h nginxcp nginx /usr/local/nginx/sbin/ nginx -V报错减少
openresty部署
安装源以及openresty
[root@server3 ~]# wget https://openresty.org/package/rhel/openresty.repo -o /etc/yum.repos.d/openresty.repo [root@server3 ~]# yum install -y openresty拷贝原先nginx里的文件到/usr/local/openresty/nginx下的目录
[root@server3 sbin]# nginx -s stop [root@server3 sbin]# cd /usr/local/nginx/ [root@server3 nginx]# cd conf [root@server3 conf]# cp nginx.conf /usr/local/openresty/nginx/conf/ [root@server3 conf]# cp cert.pem /usr/local/openresty/nginx/conf/ [root@server3 conf]# /usr/local/openresty/nginx/sbin/nginx -t [root@server3 conf]# /usr/local/openresty/nginx/sbin/nginx [root@server3 conf]# cd .. [root@server3 nginx]# cd html/ [root@server3 html]# cp example.php memcache.php index.php /usr/local/openresty/nginx/html/网页测试
session共享
概念
默认情况下,Session 保存在 Web 服务器本地。
Nginx 负载均衡调度时,用户请求可能分发到不同后端服务器。
第一次请求落在 ServerA,登录信息存在 A;第二次调度到 ServerB,B 没有该 Session,用户登录状态丢失、重复跳登录页。
Session 共享:让集群所有后端服务器共用一份 Session 数据,无论请求分到哪台服务器,都能读取登录会话信息。
作用
解决负载均衡场景下,多台后端服务器之间会话不同步,避免用户登录失效。
常见 4 种实现方案
ip_hash(Nginx 层,最简单)
Nginx 调度策略,同一客户端 IP 永远分配到同一台后端。
优点:配置简单
缺点:服务器宕机,会话丢失;负载分配不均衡
Session 存入数据库(MySQL)
所有服务器统一读写数据库保存 session。
性能较差,频繁查询数据库,很少高并发使用
Session 存入 Redis(生产主流方案)
所有后端 PHP/Tomcat 把 Session 统一写入 Redis;
任意服务器读取 Redis 获取会话。
性能高、支持集群,企业最常用。
Cookie 复制(不推荐)
把全部会话数据存在客户端 Cookie,增大请求流量,有安全限制。
配置tomcat
[root@serer2 ~]# yum install -y java-1.8.0-openjdk.x86_64 [root@sever2 ~]# tar xf apache-tomcat-7.0.37.tar.gz -C /usr/local/ [root@server2 ~]# cd /usr/local/ [root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat [root@server2 local]# cd tomcat/启动服务
[root@server2 tomcat]# bin/startup.sh [root@server2 tomcat]# netstat antlp|grep :8080server3相同,最后测试192.168.234.136:8080和192.168.234.137:8080,如图
nginx联合tomcat
[root@server1 ~]# cd /usr/local/nginx/conf/ [root@server1 conf]# vim nginx.conf upstream tomcat { sticky cookie srv_id expires=1h; server 192.168.234.136:8080; server 192.168.234.137:8080; }location ~ \.jsp$ { proxy_pass http://tomcat; }[root@server1 conf]# nginx -s reload在server2和server3上拷贝测试页面
[root@server2 ~]# cd /usr/local/tomcat/webapps/ROOT/ [root@server2 ROOT]# ll test.jsp测试: http://192.168.234.135/test.jsp
停掉192.168.234.168的tomcat服务,此时服务器变为137,但之前的数据已经清空。
为了解决此类情况,需要用到memcached
tomcat联合memcached
[root@server2 ~]# yum install -y memcached [root@server2 ~]# systemctl enable --now memcached [root@server2 ~]# netstat -antlp|grep :11211[root@server2 ~]# tar zxf msm.tgz [root@server2 ~]# cd msm/[root@server2 msm]# cp * /usr/local/tomcat/lib/ [root@server2 msm]# cd /usr/local/tomcat/conf [root@server2 conf]# vim context.xml <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.234.136:11211,n2:192.168.234.137:11211" failoverNodes="n1" #server3上修改为n2 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />重启tomcat
[root@server2 conf]# cd .. [root@server2 tomcat]# bin/shutdown.sh [root@server2 tomcat]# bin/startup.sh查看日志
[root@server2 tomcat]# cat logs/catalina.outserver3同理
测试
关闭192.168.234.137
发现变成192.168.234.136仍有之前的数据,配置成功
