一、错误代码
nginx 做负载均衡的时候 如果出现一个节点down掉的时候,nginx会根据负载均衡的设置吗,将请求转移到其他的节点上,但是服务器连接没有down掉,而是返回错误异常码,如500 502 503 504 需要添加负载均衡设置proxy_next_upstream error timeout http_500 http_502 http_503 http_504; 让出现错误异常码时,可以继续分配到下一台服务器继续出来
[root@lb01 conf.d]# vim lb.conf
upstream webs {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name www.wp.com;
location / {
proxy_pass http://webs;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
二、负载均衡的调度算法
1.rr轮循 默认使用的调度算法
2.weight 加权轮询 后端服务器配置不同的时候
3.ip_hash 以IP地址的方式进行转发,第一次10.0.0.1请求的是WEB01 那么以后每次来访问都是web01来响应你
4.url_hash 以url来转发到不同的web服务器
5.least_conn 最少链接数 看哪台web服务器的TCP链接少,我就分给谁。
加权轮询配置方法
upstream webs {
server 172.16.1.7 weight=5;
server 172.16.1.8;
}
server {
listen 80;
server_name www.wp.com;
location / {
proxy_pass http://webs;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
三、server状态
down # 不参与调度 类似前面加#注释
backup # 备份 类似备胎
upstream webs {
server 172.16.1.7 down;
server 172.16.1.8;
}
#负载均衡请求一直通过web02
upstream webs {
server 172.16.1.7;
server 172.16.1.8 backup;
}
server {
#负载均衡请求一直通过web01,当web01服务器挂掉后 请求通过web02
四、编译安装nginx
作用:根据需求自定义nginx的安装配置以及所需要的模块
1.安装依赖
[root@lb ~]#yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下载nginx源码包(要和当前nginx版本号一致)解压
[root@lb ~]#wget http://nginx.org/download/nginx-1.26.1.tar.gz
[root@lb ~]#tar xf nginx-1.26.1.tar.gz
[root@lb ~]#ll
total 1216
drwxr-xr-x 8 502 games 158 May 29 2024 nginx-1.26.1
3.下载第三方模块 解压
[root@lb ~]#wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb ~]#unzip master.zip
4.将新的模块添加进默认的模版中
[root@lb ~]#cd nginx-1.26.1/
[root@lb01 nginx-1.26.1]# patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch
5.添加模块的位置到默认的配置中
--add-module=/root/nginx_upstream_check_module-master
#############################
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/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 --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --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'
完成后复制上面的模块信息,进入第6步
6.配置安装
[root@lb01 nginx-1.26.1]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/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 --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --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'
7.make编译
[root@lb01 nginx-1.26.1]# make
8.安装
[root@lb01 nginx-1.26.1]# make install
9.检查模块
[root@lb ~/nginx-1.26.1]#nginx -V
nginx version: nginx/1.26.1
built by gcc 7.3.0 (GCC)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/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 --add-module=/root/nginx_upstream_check_module-master --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --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'
10.修改nginx配置文件
[root@lb /code]#vim /etc/nginx/conf.d/lb.conf
upstream webs {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}
server {
listen 80;
server_name www.wp.com;
location / {
proxy_pass http://webs;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://webs;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
访问http://www.test.com/upstream_check
五、部署phpmyadmin项目
1.web01部署
[root@web01 /etc/nginx/conf.d]#vim admin.conf
server {
listen 80;
server_name www.admin.com;
root /code/admin;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 /code/admin]#systemctl restart nginx
2.创建代码目录并下载代码
[root@web01 conf.d]# mkdir /code/admin
[root@web01 conf.d]# cd /code/admin
[root@web01 /code/admin]# wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.zip
[root@web01 /code/admin]#unzip phpMyAdmin-5.2.1-all-languages.zip
[root@web01 /code/admin]#mv phpMyAdmin-5.2.1-all-languages/* .
3.手动配置代码指向数据库
[root@web01 /code/admin]#mv config.sample.inc.php config.inc.php
[root@web01 /code/admin]#grep 172.16.1.51 config.inc.php -n
30:$cfg['Servers'][$i]['host'] = '172.16.1.51';
[root@web01 /code/admin]#ll /var/lib/php/
total 0
drwxrwx--- 2 root apache 6 Feb 23 2024 opcache
drwxr-xr-x 2 root root 6 Feb 23 2024 peclxml
drwxrwx--- 2 www www 84 Dec 13 15:51 session
windows解析
10.0.0.7 www.admin.com
浏览器访问 需要输入数据库远程普通用户名称和密码 lzy lzy123.com
web02部署配合phpmyadmi
1.B编写nginx配置文件
[root@web02 /code/admin]#vim /etc/nginx/conf.d/admin.conf
server {
listen 80;
server_name www.admin.com;
root /code/admin;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_sc
ript_name;
include fastcgi_params;
}
}
[root@web02 /code/admin]#systemctl restart nginx
将web01的代码拷贝到web02
[root@web02 conf.d]# scp -r 10.0.0.7:/code/admin /code
修改存放会话的目录权限
[root@web02 conf.d]# chown www.www /var/lib/php/session
[root@web02 conf.d]# ll -d /var/lib/php/session
drwxrwx--- 2 www www 6 Dec 2 16:44 /var/lib/php/session
hosts解析到web02
10.0.0.8 www.admin.com
浏览器测试访问
加入负载均衡
[root@lb /etc/nginx/conf.d]#vim admin.conf
upstream admin {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name www.admin.com;
location / {
proxy_pass http://admin;
include proxy_params;
}
}
六、安装redis服务
在51服务器上安装redis数据库
[root@mysql ~]#yum -y install redis
修改redis监听端口
[root@db01 ~]# vim /etc/redis/redis.conf
[root@mysql ~]#grep -n 172.16.1.51 /etc/redis/redis.conf
75:bind 127.0.0.1 172.16.1.51
启动redis
[root@mysql ~]#systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
登录查看
[root@mysql ~]#redis-cli
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
七、修改两台web会话写入到redis
web01配置
安装编译的命令
[root@web01 ~]#yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
php安装redis的插件
1.下载redis的源码包并解压
[root@web01 ~]# wget http://pecl.php.net/get/redis-4.0.1.tgz
2.配置
[root@web01 ~]#cd redis-4.0.1/
[root@web01 ~/redis-4.0.1]#phpize #初始化
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
[root@web01 ~/redis-4.0.1]#./configure
3.编译安装
[root@web01 ~/redis-4.0.1]#make&&make install
4.开启redis的插件功能,配置文件添加
[root@web01 ~/redis-4.0.1]#grep redis.so /etc/php.ini -n
1356:extension=redis.so #添加
5.重启服务
[root@web01 ~/redis-4.0.1]#systemctl restart php-fpm
[root@web01 ~/redis-4.0.1]#awk 'NR==1222||NR==1255' /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
#注释files和 session的行
[root@web01 ~/redis-4.0.1]#tail -4 /etc/php-fpm.d/www.conf
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
;php_value[opcache.file_cache] = /var/lib/php/opcache
重启
#先进行编译安装
同步配置文件给web02
[root@web01 ~/redis-4.0.1]#rsync -avz /etc/php.ini 172.16.1.8:/etc/
[root@web01 ~/redis-4.0.1]#rsync -avz /etc/php-fpm.d/www.conf 172.16.1.8:/etc/php-fpm.d/
登录正常
八、获取客户端真实的IP地址
配置代理服务器携带客户端真实的IP
[root@lb01 conf.d]# cat ../proxy_params
proxy_set_header Host $http_host; #携带头部信息传递
proxy_http_version 1.1; #对于http协议应该指定为1.1
proxy_connect_timeout 30; # 代理连接web超时时间
proxy_send_timeout 60; # 代理等待web响应超时时间
proxy_read_timeout 60; # web回传数据至代理超时时间
proxy_buffering on; # 开启代理缓冲区,web回传数据至缓冲区,代理边收边传返回给客户端
proxy_buffer_size 32k; #设置单个请求的缓冲区大小
proxy_buffers 4 128k; #设置代理服务器用于存储后端服务器响应的缓冲区数量和大小。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 携带客户端信息
Comments NOTHING