一,负载均衡会话保持
1,概念
--------当用户访问浏览器登陆后,会在服务器上保存一个session值,并返回给浏览器,浏览器会把返回的session值以cookie保存在浏览器,当用户再次访问时,会携带着cookie值去访问服务区,服务区会对比带来的cookie值和本地session值对比,如果一致则不需要登陆,不一致则重新登陆
2,会话保持实现方法
1)NFS挂载
2)以数据的形式保存在数据库
3)redis
3,搭建phpmyadmin
[root@web01 phpmyadmin]# cd /code/phpmyadmin/
[root@web01 phpmyadmin]# ll
drwxr-xr-x 12 root root 4096 2019-06-04 12:06 phpmyadmin
[root@web01 phpmyadmin]# chown -R www.www /code/
配置连接到数据库
[root@web01 phpmyadmin]# cd phpmyadmin/
[root@web01 phpmyadmin]# cp config.sample.inc.php config.inc.php
[root@web01 phpmyadmin]# vim config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';
配置nginx:
[root@web01 ~]# vim /etc/nginx/conf.d/phpmyadmin.conf
server {
listen 80;
server_name linux.phpmyadmin.com;
root /code/phpmyadmin;
location / {
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 ~]# systemctl restart nginx
配置hosts 访问
出现权限问题
Failed to read session data: files (path: /var/lib/php/session)
[root@web01 phpmyadmin]# chown -R www.www /var/lib/php/session
再次访问
4,同步代码及配置到web03服务器
[root@web01 ~]# scp /etc/nginx/conf.d/phpmyadmin.conf 10.0.0.9:/etc/nginx/conf.d/
[root@web01 ~]# scp /code/phpmyadmin/* 10.0.0.9:/code/phpmyadmin
[root@web03 ~]# chown -R www.www /code/
测试访问
二,配置负载均衡
配置:
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.phpmyadmin.com.conf
upstream phpmyadmin {
server 172.16.1.7;
server 172.16.1.9;
}
server {
listen 80;
server_name linux.phpmyadmin.com;
location / {
proxy_pass http://phpmyadmin;
include /etc/nginx/proxy_params;
}
}
配置hosts 访问
原理图:
三,使用redis实现session共享(会话保持)
###下载在任何一台服务器上都可以
[root@db01 ~]# yum install -y redis
[root@db01 ~]# vim /etc/redis.conf
bind 127.0.0.1 172.16.1.51
[root@db01 ~]# systemctl start redis
[root@db01 ~]# netstat -lntp
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 7800/redis-server 1
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 7800/redis-server 1
web01和web03设置PHP的session路径
[root@web01 ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
[root@web01 ~]# systemctl restart php-fpm.service
[root@web03 ~]# systemctl restart php-fpm.service
查看
[root@db01 ~]# redis-cli
127.0.0.1:6379> key*
1) "PHPREDIS_SESSION:5777d5784201de4ebd640db7683e4fe5"
2) "PHPREDIS_SESSION:a5a17b2ddfe711f749b7f0ecd5f70397"
3) "PHPREDIS_SESSION:9dfdfe91f7b4b195533eb3dffb92f12d"
四, 四层负载均衡
应用场景:
用于解决七层负载均衡65535个端口限制,
大并发情况下,四层+七层来做负载均衡,四层可以保证七层负均的高可用;
1,两台七层负载均衡配置站点都一样:
[root@lb02 ~]# scp /etc/nginx/nginx.conf [email protected]:/etc/nginx/
[root@lb02 ~]# scp /etc/nginx/proxy_params [email protected]:/etc/nginx/
2,配置hosts,确认两台访问正常
3,配置负载均衡
#下载nginx
[root@lb04 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#配置nginx.conf
四层负载均衡的stream模块和http 层同级别,所以不能写在http层里面
[root@lb04 ~]# vim /etc/nginx/nginx.conf
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
include /etc/nginx/conf.c/*.conf
#写四层配置文件
[root@lb04 ~]# mkdir /etc/nginx/conf.c -p
[root@lb04 ~]# vim /etc/nginx/conf.c/lb4_proxy.conf
stream {
upstream lb {
server 172.16.1.4:80;
server 172.16.1.5:80;
}
server {
listen 80;
proxy_pass lb;
}
}
#重启配置hosts访问
[root@lb04 ~]# systemctl restart nginx
注意⚠️ 要保证两台负载均衡下的站点相同
配置四层负载均衡的日志
[root@lb04 ~]# vim /etc/nginx/conf.c/lb4_proxy.conf
stream {
log_format main '$remote_addr $remote_port - [$time_local] $status $protocol ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
access_log /var/log/nginx/lb4_access.log main;
upstream lb {
server 172.16.1.4:80;
server 172.16.1.5:80;
}
server {
listen 80;
proxy_pass lb;
}
}
五,四层负载均衡做端口转发
[root@lb04 ~]# vim /etc/nginx/conf.c/lb4_proxy.conf
stream {
log_format main '$remote_addr $remote_port - [$time_local] $status $protocol ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
access_log /var/log/nginx/lb4_access.log main;
upstream lb {
server 172.16.1.4:80;
server 172.16.1.5:80;
}
upstream ssh_7 {
server 172.16.1.7:22;
}
server {
listen 5555;
proxy_pass ssh_7
server {
listen 80;
proxy_pass lb;
}
}
#验证
[root@lb04 ~]# ssh 10.0.0.3 -p 5555
数据库读写分离配置(不常用了)
[root@lb04 ~]# vim /etc/nginx/conf.c/lb4_proxy.conf
... ...
upstream mysql {
server 172.16..1.52:3306
server 172.16..1.53:3306
server 172.16..1.54:3306
}
server {
listen 6666;
proxy_pass mysql;
}
六,动静分离
--配置单台机器动静分离
[root@web03 ~]# vim /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name linux.wp.com;
root /code/wordpress;
location / {
index index.php;
}
location / {
index index.php;
}
location ~* \.jpg$ {
root /code/wordpress;
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
--配置多台机器动静分离
1,配置web01静态
[root@web01 ~]# vim /etc/nginx/conf.d/jt.conf
server {
listen 80;
server_name linux.jt.com;
location ~* \.(jpg|png)$ {
root /code/pic;
}
}
[root@web01 ~]# mkdir /code/pic
[root@web01 ~]# cd /code/pic/
[root@web01 /code/pic]# ll
-rw-r--r-- 1 root root 13467 2020-11-23 04:29 1.jpg
-rw-r--r-- 1 root root 21252 2020-11-23 04:29 2.jpg
[root@web01 /code/pic]# chown -R www.www /code
[root@web01 ~]# systemctl restart nginx
配置hosts,访问
2,配置web03动态配置
[root@web03 ~]# yum install -y tomcat
[root@web03 ~]# cd /usr/share/tomcat/webapps/
[root@web03 /usr/share/tomcat/webapps]# mkdir ROOT
[root@web03 /usr/share/tomcat/webapps]# vim ROOT/jave_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
TEST JSP Page
<%
Random rand = new Random();
out.println("
随机数:
");
out.println(rand.nextInt(99)+100);
%>
[root@web03]# systemctl start tomcat
访问
3,配置负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/dj.com.conf
upstream jt {
server 172.16.1.8;
}
upstream dt {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name linux.dj.com;
location / {
root /code/dj;
index index.html;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://jt;
include /etc/nginx/proxy_params;
}
location ~* \.(jsp|php)$ {
proxy_pass http://dt;
include /etc/nginx/proxy_params;
}
}
访问linux.dj.com/jave_test.jsp
linux.dj.com/1.jpg
4,整合静态和动态资源
[root@lb01 ~]# mkdir /code/dj -p
[root@lb01 ~]# vim /code/dj/index.html
测试ajax和跨域访问
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://linux.dj.com/java_test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
测试动静分离
访问
七、资源分离
[root@web01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
server {
listen 8081;
server_name linux.sj.com;
location / {
root /code/anzhuo;
index index.html;
}
}
server {
listen 8082;
server_name linux.sj.com;
location / {
root /code/iphone;
index index.html;
}
}
server {
listen 8083;
server_name linux.sj.com;
location / {
root /code/pc;
index index.html;
}
}
root@web01 ~]# mkdir /code/{anzhuo,iphone,pc} -p
[root@web01 ~]# echo "anzhuo" > /code/anzhuo/index.html
[root@web01 ~]# echo "iphone" > /code/iphone/index.html
[root@web01 ~]# echo "pc" > /code/pc/index.html
[root@web01 ~]# systemctl restart nginx
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
upstream anzhuo {
server 172.16.1.7:8081;
}
upstream iphone {
server 172.16.1.7:8082;
}
upstream pc {
server 172.16.1.7:8083;
}
server {
listen 80;
server_name linux.sj.com;
location / {
if ($http_user_agent ~* "Android") {
proxy_pass http://anzhuo;
}
if ($http_user_agent ~* "iphone") {
proxy_pass http://iphone;
}
proxy_pass http://pc;
include proxy_params;
}
}
重启nginx,配置hosts访问