文章目录
- Nginx代理服概述
- Nginx代理服务常见模式
- 正向代理
- 反向代理
- 正向代理与反向代理的区别
- Nginx代理服务⽀持协议
- Nginx配置代理语法
- 配置nginx代理实践
- 环境准备
- 配置web01网站
- 编写网站
- 配置代理
- 配置hosts,域名指向代理服务器
- 访问页面查看
- Nginx代理常用参数
- 添加发往后端服务器的请求头信息
- 代理到后端的TCP连接、响应、返回等超时时间
- proxy_buffer代理缓冲区
- 常用的优化配置
- proxy复用
- Nginx负载均衡
- 常见的负载均衡软件
- 负载均衡类型
- Nginx负载均衡配置
- 负载均衡配置语法 ngx_http_upstream_module
- 环境
- web01准备站点
- web02准备站点
- 配置负载均衡配置文件
- 配置优化文件
- 访问页面测试
- 负载均衡常见问题
- 负载均衡状态码屏蔽
- nginx 调度算法
- 轮询配置方法
- 加权轮询配置方法
- ip_hash的配置方法
- nginx负载均衡状态
- down状态配置测试
- backup状态配置测试
- 访问错误状态
- max_conns限制最大连接数状态配置
- nginx健康检查模块(扩展)
- 负载均衡会话保持
- 1.seesion共享的方法
- 2.搭建phpmyadmin
- 3.使用redis实现session共享
Nginx代理服概述
什么是代理
代理理财、代理租房、代理收货等等,如下图所示
没有代理情景
在没有代理模式的情况下,客户端和Nginx服务端,都是客户端直接请求服务端,服务端直接响应客户
端。
企业场景
那么在互联⽹请求⾥⾯,客户端往往⽆法直接向服务端发起请求,那么就需要⽤到代理服务,来实现客户端和服务通信,如下图所示
Nginx代理服务常见模式
Nginx作为代理服务,按照应⽤场景模式进⾏总结,代理分为正向代理、反向代理
正向代理
正向代理,(内部上⽹)客户端<—>代理->服务端
反向代理
反向代理,⽤于公司集群架构中,客户端->代理<—>服务端
正向代理与反向代理的区别
1.区别在于形式上服务的”对象”不⼀样
2.正向代理代理的对象是客户端,为客户端服务
3.反向代理代理的对象是服务端,为服务端服务
Nginx代理服务⽀持协议
反向代理模式 | Nginx配置模块 |
---|---|
http、websocket、https、tomcat、Java程序 | ngx_http_proxy_module |
fastcgi(php程序) | ngx_http_fastcgi_module |
uwsgi(python程序) | ngx_http_uwsgi_module |
grpc(golang程序) | ngx_http_v2_module |
GRPC(远程过程调用) websocker(及时性) rtmp(直播协议) |
Nginx配置代理语法
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
配置nginx代理实践
环境准备
主机 | 内网 | 外网 |
---|---|---|
lb01 | 172.16.1.51 | 192.168.15.51 |
web01 | 172.16.1.7 | 192.168.15.7 |
配置web01网站
[root@web01 conf.d]# vim proxy.conf
server {
listen 80;
server_name proxy.linux.com;
location / {
root /code/proxy;
index index.html;
}
}
[root@web01 ~]# systemctl restart nginx
编写网站
[root@web01 ~]# echo "web01 web01 web01 ........" > /code/proxy/index.html
配置代理
#安装nginx
#配置nginx
#创建用户
[root@lb01 ~]# vim /etc/nginx/conf.d/daili.conf
server {
listen 80;
server_name proxy.linux.com;
location / {
proxy_pass http://10.0.0.7;
}
}
#启动nginx
配置hosts,域名指向代理服务器
10.0.0.4 proxy.linux.com
#10.0.0.7 www.php.com blog.linux.com zh.linux.com edu.linux.com www.linux.com proxy.linux.com
访问页面查看
访问页面不是域名对应的页面,是web01上面nginx第一个配置文件
使用wireshark抓包分析
可以看出,当我们只用proxy_pass代理的时候,会发现如下问题:
10.0.0.1请求10.0.0.4的时候使用的是域名
10.0.0.4请求10.0.0.7的时候使用的是IP:port
之前课程中讲到,当访问80端口的时候,没有域名的情况下,默认会去找排在最上面的那个配置文件。
所以我们需要解决这个问题,保留住最开始的请求头部信息。
proxy_set_header,这个模块可以帮我们解决这个问题
Nginx代理常用参数
添加发往后端服务器的请求头信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location
# 用户请求的时候HOST的值是www.oldboy.com, 那么代理服务会像后端传递请求的还是www.oldboy.com
proxy_set_header Host $http_host;
# 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的ip
proxy_set_header X-Real-IP $remote_addr;
# 客户端通过代理服务访问后端服务, 后端服务通过该变量会记录真实客户端地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
日志里面还可以配置 "$httpd_x_real_ip"
代理到后端的TCP连接、响应、返回等超时时间
#nginx代理与后端服务器连接超时时间(代理连接超时)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
#nginx代理等待后端服务器的响应时间
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
#后端服务器数据回传给nginx代理超时时间
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
proxy_buffer代理缓冲区
#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
#设置nginx代理保存用户头信息的缓冲区大小
Syntax: proxy_buffer_size size;
Default: proxy_buffer_size 4k|8k;
Context: http, server, location
#proxy_buffers 缓冲区
Syntax: proxy_buffers number size;
Default: proxy_buffers 8 4k|8k;
Context: http, server, location
常用的优化配置
location / {
proxy_pass http://127.0.0.1:8080;
include proxy_params;
}
[root@lb01 ~]# vim /etc/nginx/proxy_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;
proxy复用
[root@lb01 nginx]# cat proxy_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 2s;
proxy_read_timeout 2s;
proxy_send_timeout 2s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
# 把反向代理中的HTTP 1.0改成1.1
Nginx负载均衡
#注意:代理只能代理一台机器
nginx做代理,一个location可以做一个代理
# 为什么做负载均衡?
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾
往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB
# 常见的负载均衡
SLB 阿⾥云负载均衡
LB ⻘云负载均衡
CLB 腾讯云负载均衡
ULB ucloud负载均衡
#负载均衡叫法
负载均衡
负载
Load Balance
LB
# 负载均衡作用
1.流量分发
2.后端服务的高可用
常见的负载均衡软件
Nginx #在1.9版本之前只能做七层,1.9版本之后既能做七层,也能做四层负载均衡
Haproxy #既能做四层,也能做七层负载均衡
LVS #只能做四层负载均衡
#LVS是最快的负载均衡软件,其他两个软件需要将请求发送到服务再转发到后端,LVS不⽤到服务,它
相当于将服务器变成了负载均衡,直接转发请求
负载均衡类型
#四层负载均衡
所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能⽀持TCP/IP的控制,所
以只需要对客户端的请求进⾏TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能⾮常
快、只需要底层进⾏应⽤处理,⽽不需要进⾏⼀些复杂的逻辑。
#七层负载均衡
七层负载均衡它是在应⽤层,那么它可以完成很多应⽤⽅⾯的协议请求,⽐如我们说的http应⽤的负
载均衡,它可以实现http信息的改写、头信息的改写、安全应⽤规则控制、URL匹配规则控制、以及
转发、rewrite等等的规则,所以在应⽤层的服务⾥⾯,我们可以做的内容就更多,那么Nginx则是
⼀个典型的七层负载均衡SLB
#四层负载与七层负载区别
四层负载均衡数据包在底层就进⾏了分发,⽽七层负载均衡数据包则是在最顶层进⾏分发、由此可以看
出,七层负载均衡效率没有四负载均衡⾼。
但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以⽤Nginx可以作会话保持,URL
路径规则匹配、head头改写等等,这些是四层负载均衡⽆法实现的。
#注意:四层负载均衡不识别域名,七层负载均衡识别域名
Nginx负载均衡配置
Nginx要实现负载均衡需要用到proxy_pass代理模块配置.
Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池.
upstream web {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name www.linux.com;
proxy_pass http://web;
}
upstream dynamic {
server backend1.example.com weight=5 max_fails=3 fail_timeout=5s;
server backend2.example.com weight=5 max_fails=3 fail_timeout=5s;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://dynamic;
health_check;
}
}
========================================================================
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503
http_504 http_404 http_403;
}
}
负载均衡配置语法 ngx_http_upstream_module
Syntax: upstream name { ... }
Default: —
Context: http
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name www.linux.com
location / {
proxy_pass http://backend;
}
}
环境
主机 | 外网ip | 内网ip | 身份 |
---|---|---|---|
lb01 | 10.0.0.4 | 172.16.1.4 | 负载均衡 |
web01 | 10.0.0.7 | 172.16.1.7 | web |
web02 | 10.0.0.8 | 172.16.1.8 | web |
web01准备站点
[root@web01 conf.d]# vim node.conf
server {
listen 80;
server_name node.linux.com;
charset 'utf-8';
location / {
root /code/node;
index index.html;
}
}
[root@web01 conf.d]# mkdir /code/node
[root@web01 conf.d]# echo "我是web01......" > /code/node/index.html
[root@web01 conf.d]# systemctl restart nginx
web02准备站点
[root@web02 conf.d]# vim node.conf
server {
charset 'utf-8';
listen 80;
server_name node.linux.com;
location / {
root /code/node;
index index.html;
}
}
[root@web02 conf.d]# mkdir /code/node
[root@web02 conf.d]# echo "我是web02......" > /code/node/index.html
[root@web02 conf.d]# systemctl restart nginx
配置负载均衡配置文件
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.linux.com;
location / {
proxy_pass http://node;
include proxy_params;
}
}
#检查include文件是否存在
[root@lb01 conf.d]# ll /etc/nginx/proxy_params
-rw-r--r-- 1 root root 275 Feb 28 15:24 /etc/nginx/proxy_params
[root@lb01 conf.d]# systemctl restart nginx
配置优化文件
[root@Nginx ~]# vim /etc/nginx/proxy_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;
访问页面测试
负载均衡常见问题
如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。
负载均衡状态码屏蔽
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503
http_504 http_404 http_403;
}
}
nginx 调度算法
调度算法 | 概述 |
---|---|
轮询 | 按时间顺序逐一分配到不同的后端服务器(默认) |
weight | 加权轮询,weight值越大,分配到的访问几率越高 |
ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 |
url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
least_conn | 最少链接数,那个机器链接数少就分发 |
轮询配置方法
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
加权轮询配置方法
#访问根据配置的权重比例进行分配
upstream node {
server 172.16.1.7:80 weight=5;
server 172.16.1.8:80 weight=1;
}
# 加权,权值越小,访问概率越低
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=5;
server 172.16.1.9:80 weight=100;
ip_hash的配置方法
#根据访问的来源IP分配至同一台服务器
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
ip_hash;
}
#经常使用这种方式进行会话保持
nginx负载均衡状态
状态 | 概述 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails失败后, 服务暂停时间 |
max_conns | 限制最大的接收连接数 |
down状态配置测试
upstream node {
#不参与负载均衡任何调度,一般停机维护或者上线的时候使用
server 172.16.1.7:80 down;
server 172.16.1.8:80;
}
backup状态配置测试
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 backup;
}
访问错误状态
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout=10s;
}
max_conns限制最大连接数状态配置
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_conns=120;
}
nginx健康检查模块(扩展)
[root@lb01 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb01 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb01 ~]# unzip master.zip
[root@lb01 ~]# cd nginx-1.14.2/
[root@lb01 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/data/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/data/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb01 nginx-1.14.2]# make && make install
[root@lb01 nginx-1.14.2]# vim /etc/nginx/conf.d/proxy.conf
upstream web {
server 10.0.0.7;
server 10.0.0.8;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name proxy.linux.com;
location / {
proxy_pass http://web;
proxy_set_header host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
}
location /upstream_check {
check_status;
}
}
#另一种方法
yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 网站 https://ghproxy.com/
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
# p1:在nginx目录内 p0不在nginx目录内
[root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.16.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
./configure --prefix=/data/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/data/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
# make && make install
#vim /etc/nginx/conf.d/test.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
# vim /etc/nginx/proxy_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 2s;
proxy_send_timeout 2s;
proxy_read_timeout 2s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
interval:检测间隔
rise:重试测试
fall:错误次数
timeout:超时时间
# nginx -s reload 重启
#访问 192.168.15.5/upstream_check
负载均衡会话保持
当用户访问页面登录后,会在服务器上生成一个session文件,并且返回给浏览器一个session_id内容的cookie,cookie会存在浏览器中,下一次访问cookie会携带session_id来到服务器验证,没有变化则说明是登录状态,不需要重新登录
1.seesion共享的方法
1.把seesion文件保存在本地的nfs挂载目录
2.通过程序将seesion写入数据库
3.通过程序将seesion存入redis
2.搭建phpmyadmin
1)上传代码包
[root@web01 ~]# cd /code/
[root@web01 code]# rz phpMyAdmin-4.9.0.1-all-languages.zip
2)解压代码包
[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/
3)配置连接数据库代码
[root@web01 code]# 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.52';
4.配置nginx配置文件
[root@web01 code]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim phpmyadmin.conf
server {
listen 80;
server_name php.linux.com;
location / {
root /code/phpmyadmin;
index index.php;
}
location ~ \.php$ {
root /code/phpmyadmin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# systemctl restart nginx
5.配置hosts,访问页面
#页面报错授权
[root@web01 conf.d]# chown -R www.www /var/lib/php/session
6.如果登录有问题
[root@db02 ~]# mysql -u root -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1666
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
MariaDB [(none)]>
MariaDB [(none)]> select user,host from mysql.user;
+------+------------+
| user | host |
+------+------------+
| root | 127.0.0.1 |
| edu | 172.16.1.% |
| root | 172.16.1.% |
| wp | 172.16.1.% |
| zh | 172.16.1.% |
| root | ::1 |
| | db02 |
| root | db02 |
| | localhost |
| root | localhost |
+------+------------+
10 rows in set (0.00 sec)
MariaDB [(none)]>
7.把代码和站点目录推送至web02
[root@web01 conf.d]# scp -r /code/phpmyadmin 172.16.1.8:/code/
[root@web01 conf.d]# scp /etc/nginx/conf.d/phpmyadmin.conf 172.16.1.8:/etc/nginx/conf.d/
#web02重启服务和授权
[root@web02 ~]# systemctl restart nginx
[root@web02 ~]# chown -R www.www /code/phpmyadmin/
[root@web02 ~]# chown -R www.www /var/lib/php/session
8.配置hosts访问web02
9.配置负载均衡
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# vim phpmyadmin_proxy.conf
upstream phpmyadmin {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name php.linux.com;
location / {
proxy_pass http://phpmyadmin;
include proxy_params;
}
}
10.配置hosts访问
3.使用redis实现session共享
1)安装redis
[root@db01 ~]# yum install -y redis
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)配置php服务将session存到redis
[root@web01 conf.d]# vim /etc/php.ini
#原内容 session.save_handler = files
session.save_handler = redis
#原内容 ;session.save_path = "/tmp"
session.save_path = "tcp://172.16.1.51:6379"
[root@web01 conf.d]# vim /etc/php-fpm.d/www.conf
#注释原内容
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
5)同步配置至web02
[root@web01 conf.d]# scp /etc/php.ini 172.16.1.8:/etc/
root@172.16.1.8's password:
php.ini 100% 61KB 1.2MB/s 00:00
[root@web01 conf.d]# scp /etc/php-fpm.d/www.conf 172.16.1.8:/etc/php-fpm.d/
root@172.16.1.8's password:
www.conf 100% 18KB 1.1MB/s 00:00
[root@web01 conf.d]#
6)重启php
[root@web01 conf.d]# systemctl restart php-fpm
[root@web02 conf.d]# systemctl restart php-fpm
7)访问页面测试
8)查看redis里面的session
[root@db01 ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:1c222f693e1ab3ae2b9a688f00a40531"
127.0.0.1:6379>