七、Nginx负载均衡配置 lb01、lb02
#web01 & web02创建测试 [root@web01 conf.d]# cat 03_test.conf server { listen 80; server_name test.yunwei.com; charset utf-8; root /data/test; location / { index index.html; } } nginx -t systemctl reload nginx mkdir -p /data/test echo 'web01' > /data/test/index.html #注:web02服务器替换web02 chown www.www /data/test -R ll /data/test/index.html 测试 [root@web02 conf.d]# curl -H "host:test.yunwei.com" 172.16.1.7 web01 [root@web02 conf.d]# curl -H "host:test.yunwei.com" 172.16.1.8 web02 #在web01上配置phpmyadmin [root@web01 conf.d]# cat /etc/nginx/conf.d/04_phpmyadmin.conf server { listen 80; server_name phpmyadmin.yunwei.com; root /data/phpmyadmin; index index.php index.html; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } nginx -t systemctl reload nginx ### mkdir /data/phpmyadmin -p cd /server/tools wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip unzip phpMyAdmin-5.1.1-all-languages.zip -d /data/phpmyadmin/ cd /data/phpmyadmin/ mv phpMyAdmin-5.1.1-all-languages/* . rmdir phpMyAdmin-5.1.1-all-languages cp config.sample.inc.php config.inc.php vim +30 /data/phpmyadmin/config.inc.php 改为 $cfg['Servers'][$i]['host'] = 'db01.yunwei.com'; #复制程序到web02并添加配置文件 rsync -avz -e "ssh -p 22" /data/phpmyadmin/ root@172.16.1.8:/data/phpmyadmin/ #在web02上配置phpmyadmin [root@web01 conf.d]# cat /etc/nginx/conf.d/04_phpmyadmin.conf server { listen 80; server_name phpmyadmin.yunwei.com; root /data/phpmyadmin; index index.php index.html; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } nginx -t systemctl reload nginx
#部署lb01 phpmyadmin.yunwei.com虚拟主机反向代理lb01配置 [root@lb01 conf.d]# cat /etc/nginx/conf.d/04_phpmyadmin.conf upstream backend { server 172.16.1.7 weight=1; #默认80端口 server 172.16.1.8 weight=1; #默认80端口 } server { listen 80; server_name phpmyadmin.yunwei.com; location / { proxy_pass http://backend; proxy_set_header Host $http_host; } } #测试浏览器打开phpmyadmin.yunwei.com 登录到数据库,然后刷新感受请求到不同节点后,出现未登录状态。 可以通过谷歌f12查看请求报文和响应报文。 登录172.16.1.51部署redis缓存 采用Redis共享方式来实现会话保持: #配置lb01 & lb02 安装nginx,参考上面,安装好nginx,然后配置 vi /etc/nginx/conf.d/01_lb_test.conf server { listen 80; server_name test.yunwei.com; location / { proxy_pass http://172.16.1.7:80; proxy_set_header Host $http_host; } } nginx -t systemctl reload nginx tail -f /var/log/nginx/access.log
基于动静分离:参考day59笔记
基于设备区分:参考day59笔记
根据不同业务做负载均衡
#web01配置 [root@web01 ~]# cat /etc/nginx/conf.d/05_www.conf server { listen 80; server_name www.yunwei.com; root /data/www; location / { index index.html; } } [root@web01 ~]# mkdir /data/www [root@web01 ~]# echo "register_web01..." > /data/www/index.html [root@web01 ~]# nginx -t [root@web01 ~]# systemctl reload nginx
#web02配置 [root@web02 ~]# cat /etc/nginx/conf.d/05_www.conf server { listen 80; server_name www.yunwei.com; root /data/www; location / { index index.html; } } [root@web02 ~]# mkdir /data/www [root@web02 ~]# echo "login--web02.." >> /data/www/index.html [root@web01 ~]# nginx -t [root@web01 ~]# systemctl reload nginx
#负载均衡lb01配置 [root@lb01 conf.d]# cat /etc/nginx/conf.d/05_www.conf upstream register { server 172.16.1.7:80; } upstream login { server 172.16.1.8:80; } server { listen 80; server_name www.yunwei.com; location /register { proxy_pass http://register/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Real-IP $remote_addr; } location /login { proxy_pass http://login/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Real-IP $remote_addr; } } [root@lb01 ~]# nginx -t [root@lb01 ~]# systemctl reload nginx