Nginx负载均衡会话保持
什么是会话保持
将用户登录的状态记录下来,并且保持用户登录。
做会话保持的方式:
-
nginx
的ip_hash
(运维) -
session
共享(开发)
cookie
:前端记录会话保持
session
:后端记录会话保持(给cookie加密)
环境准备
主机名 | 软件 | 外网IP | 内网IP |
---|---|---|---|
web01 | phpmyadmin,nginx,php | 10.0.0.7 | 172.16.1.7 |
web02 | phpmyadmin,nginx,php | 10.0.0.8 | 172.16.1.8 |
lb01 | nginx | 10.0.0.5 | 172.16.1.5 |
部署web
[root@web01 /code]# vim /etc/nginx/conf.d/php.dsr.com.conf
server {
listen 80;
server_name php.dsr.com;
root /code/phpmyadmin;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/code/php71w.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# 获取代码
[root@web01 /code]# wget http://test.driverzeng.com/Nginx_Code/phpMyAdmin-4.9.0.1-all-languages.zip
# 解压
[root@web01 /code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
# 更改名字
[root@web01 /code]# mv phpMyAdmin-4.9.0.1-all-languages phpmyadmin
# 授权
[root@web01 /code]# chown -R www.www /code/
# 远程复制给web02
[root@web01 /code]#scp -r /etc/nginx/conf.d/php.dsr.com.conf 172.16.1.8:/etc/nginx/conf.d/
# 修改文件名
[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';
[root@web02 phpmyadmin]# chown www.www /var/lib/php/session/
# 上图是远程登录数据库
## 查看数据库用户
[root@db01 ~]# mysql
MariaDB [(none)]> select user,host from mysql.user;
+----------------+-----------+
| user | host |
+----------------+-----------+
| wordpress_user | % |
| zh_user | % |
| root | 127.0.0.1 |
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+----------------+-----------+
8 rows in set (0.01 sec)
## 给root授权(可以远程登录)
MariaDB [(none)]> grant all on *.* to root@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+----------------+-----------+
| user | host |
+----------------+-----------+
| root | % |
| wordpress_user | % |
| zh_user | % |
| root | 127.0.0.1 |
| root | ::1 |
| | db01 |
| root | db01 |
| | localhost |
| root | localhost |
+----------------+-----------+
9 rows in set (0.00 sec)
## 登录数据库
配置负载均衡
[root@lb01 conf.d]# vim php_upstream.conf
upstream php_drz_com {
server 172.16.1.7;
server 172.16.1.8;
}
server{
listen 80;
server_name php.drz.com;
location / {
proxy_pass http://php_drz_com;
include /etc/nginx/proxy_params;
}
}
配置session共享(redis缓存数据库)
环境准备
主机名 | 软件 | 外网IP | 内网IP |
---|---|---|---|
web01 | phpmyadmin,nginx,php | 10.0.0.7 | 172.16.1.7 |
web02 | phpmyadmin,nginx,php | 10.0.0.8 | 172.16.1.8 |
lb01 | nginx | 10.0.0.5 | 172.16.1.5 |
db01 | redis | 10.0.0.51 | 172.16.1.51 |
# 1.安装redis
[root@db01 ~]# yum install redis -y
# 2.修改redis配置文件
[root@db01 ~]# vim /etc/redis.conf
bind 127.0.0.1 172.16.1.51
# 3.启动redis
[root@db01 ~]# systemctl start redis
# 4.加入开机自启
[root@db01 ~]# systemctl enable redis
# 5.检查端口
[root@db01 ~]# netstat -lntup|grep 6379
tcp 0 0 172.16.1.51:6379 0.0.0.0:* LISTEN 7545/redis-server 1
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 7545/redis-server 1
# 6.检查进程
[root@db01 ~]# ps -ef|grep [r]edis
redis 7545 1 0 18:28 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379
# 7.连接redis
[root@db01 ~]# redis-cli
127.0.0.1:6379>
## 查看数据库内容
127.0.0.1:6379> Keys *
1) "PHPREDIS_SESSION:388e33e1e099805c828bd282162ef542"
2) "PHPREDIS_SESSION:629407d75a1d29c511f262e3f8c35f81"
## 查看数据的存活时间
127.0.0.1:6379> ttl "PHPREDIS_SESSION:388e33e1e099805c828bd282162ef542"
(integer) 868
## 查看文件里的内容
127.0.0.1:6379> get PHPREDIS_SESSION:388e33e1e099805c828bd282162ef542
## 修改数据的生存时间
127.0.0.1:6379> EXPIRE PHPREDIS_SESSION:c31cf9f74609d1655323243febaeb1b9 60
# 8.修改程序,将session存放到Redis中
[root@web01 phpmyadmin]# vim /etc/php.ini
1231 session.save_handler = redis
1265 session.save_path = "tcp://172.16.1.51:6379"
1295 session.auto_start = 1
[root@web01 phpmyadmin]# vim /etc/php-fpm.d/www.conf
396 ;php_value[session.save_handler] = files
397 ;php_value[session.save_path] = /var/lib/php/session
# 9.重启php
[root@web01 phpmyadmin]# systemctl restart php-fpm