您的当前位置:首页正文

【高可用架构】用Nginx实现负载均衡(三)

2023-08-18 来源:汇意旅游网
【⾼可⽤架构】⽤Nginx实现负载均衡(三)

前⾔

在上⼀篇,已经⽤Envoy⼯具统⼀发布了Deploy项⽬代码。本篇我们来看看如何⽤nginx实现负载均衡负载均衡器IP

192.168.10.11【⾼可⽤架构】系列链接:演⽰

配置应⽤服务器

⾸先,需要将上⼀篇部署的两台应⽤服务器,都能够单独访问配置192.168.10.12、192.168.10.18上nginx的config

# vi /etc/nginx/config.d/dev.deploy.goods.confserver {

listen 80;

server_name dev.deploy.goods; index index.html index.htm index.php; location / {

rewrite ^/(.*)$ /index.php/$1 last;

try_files $uri $uri/ /index.php?$query_string; }

location ~ (.+\\.php)(.*)$ {

root \"/var/www/Deploy/public\";

fastcgi_split_path_info ^(.+\\.php)(.+)$;

fastcgi_pass unix:/var/run/php-fpm/php7-fpm.sock; fastcgi_index index.php; include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k;

fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; }}

重启nginx配置win的hosts

192.168.10.18 dev.deploy.goods # 开发机也是这个域名,⼤家可以换⼀个,这⾥暂时去掉

访问成功

已同样的⽅式配置12机

配置负载均衡器

进⼊192.168.10.11虚拟机配置config

upstream deploy_proxy {

server 192.168.10.12; server 192.168.10.18; }

server { listen 80;

server_name dev.deploy.goods;

index index.html index.htm index.php; charset utf-8;

location = /favicon.ico {access_log off; log_not_found off;} location = /robots.txt {access_log off; log_not_found off;} sendfile off;

client_max_body_size 100m; location / {

proxy_set_header Host dev.deploy.goods; # 域名要和APP服务器设置的⼀样 proxy_pass http://deploy_proxy; # 上⾯设置的deploy_proxy }

#ssl_certificate /etc/nginx/ssl/homestead.app.crt; #ssl_certificate_key /etc/nginx/ssl/homestead.app.key;

}

重启nginx配置

配置win的hosts(记得去掉之前设置的域名对应)

192.168.10.11 dev.deploy.goods

⾄此,负载均衡器也部署成功了,下⼀篇就要实现redis主从结构了,在此之前,可以提前看下【linux系统】的redis安装

因篇幅问题不能全部显示,请点此查看更多更全内容