nginx實現動靜分離的負載均衡集群
- 2019 年 10 月 8 日
- 筆記
1、負載均衡介紹
LB負載均衡集群分兩類:LVS (四層)和 nginx或haproxy (七層)
客戶端通過訪問分發器的VIP來訪問網站,現在應用更複雜,比如現在網站頁面有:.php .html .png .jpeg .jsp 等, 有動態頁面有靜態頁面。靜態頁面一般是不變的,想訪問更快些。但是前面的LVS是四層的。基於IP的。現在需要在應用層基於不同的應用進行分發。Nginx / Haproxy都可以支持7層
工作中,希望這樣:
靜態文件處理:可以使用nginx 或apache
動文件處理: apache ,tomcat
圖片文件處理: squid
2、 Nginx 負載均衡基礎知識
Nginx 的 upstream 負載的5種方式,目前最常用 前3種方式
1)、輪詢(默認)
每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器 down 掉,能自動剔除。
2)、weight
指定輪詢幾率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
3)、ip_hash
每個請求按訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決 session 的問題。
4)、fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
5)、url_hash(第三方) url哈西
按訪問url的hash結果來分配請求,使同樣的url定向到同一個後端服務器,後端服務器為緩存時比較有效
3、使用nginx實現負載均衡和動靜分離
3.1安裝nginx時必須先安裝相應的編譯工具和相關依賴
[root@docker-02 ~]# yum -y install gcc gcc-c++ autoconf automake [root@docker-02 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib:nginx提供gzip模塊,需要zlib庫支持openssl:nginx提供ssl功能pcre:支持地址重寫rewrite功能
3.2安裝nginx
[root@docker-02 ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [root@docker-02 ~]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/ [root@docker-02 nginx-1.8.0]# cd /usr/local/src/nginx-1.8.0/ [root@docker-02 nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module [root@docker-02 nginx-1.8.0]# make -j 4 [root@docker-02 nginx-1.8.0]# make install ##查看參數: [root@docker-02 nginx-1.8.0]# ./configure --help | grep mp4
參數:
–with-http_dav_module 啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法)默認情況下為關閉,需編譯開啟
–with-http_stub_status_module 啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態)
–with-http_addition_module 啟用ngx_http_addition_module支持(作為一個輸出過濾器,支持不完全緩衝,分部分響應請求)
–with-http_sub_module 啟用ngx_http_sub_module支持(允許用一些其他文本替換nginx響應中的一些文本)
–with-http_flv_module 啟用ngx_http_flv_module支持(提供尋求內存使用基於時間的偏移量文件)
–with-http_mp4_module 啟用對mp4文件支持(提供尋求內存使用基於時間的偏移量文件)
編譯和安裝: (查看CPU邏輯數cat /proc/cpuinfo | grep processor | wc -l)
3.3生成運行nginx的用戶
[root@docker-02 nginx-1.8.0]# useradd -u 8000 -s /sbin/nologin nginx [root@docker-02 nginx-1.8.0]# id !$ id nginx uid=8000(nginx) gid=8000(nginx) 組=8000(nginx)
3.4介紹nginx主要目錄結構
[root@docker-02 nginx]# ls /usr/local/nginx/ conf html logs sbin conf #配置文件 html #網站根目錄 logs #日誌 sbin #nginx啟動腳本 ##主配置文件: [root@docker-02 nginx]# ls /usr/local/nginx/conf/nginx.conf
3.5開機啟動nginx
[root@docker-02 nginx]# echo '/usr/local/nginx/sbin/nginx &' >> /etc/rc.local ##檢查 [root@docker-02 sbin]# ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful ##啟動報錯 [root@docker-02 sbin]# ./nginx -s reload nginx: [error] invalid PID number ""in"/usr/local/nginx/logs/nginx.pid" ##解決辦法 [root@docker-02 sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ##測試 [root@docker-02 sbin]# curl 172.17.1.151
4、配置nginx成為分發器,實現動靜分離
[root@docker-02 sbin]# cd /usr/local/nginx/conf/ ##配置文件目錄 [root@docker-02 conf]# cp nginx.conf nginx.conf.bak ##備份一下配置文件 [root@docker-02 conf]# vim nginx.conf 改:# user nobody; ##指定啟動nginx用戶 為:user nginx nginx; 改: 41 #access_log logs/host.access.log main; 42 43 location / { 44 root html; 45 index index.html index.htm; 46 if($request_uri~* .html$){ 47 proxy_pass http://htmlservers; 48 } 49 if($request_uri~* .php$){ 50 proxy_pass http://phpservers; 51 } 52 proxy_pass http://picservers; 53 } ##把以下內容注釋掉,否則php文件直接在nginx服務器上解析了,不再解析給後端服務器: 70 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 71 # 72 #location ~ .php$ { 73 # root html; 74 # fastcgi_pass 127.0.0.1:9000; 75 # fastcgi_index index.php; 76 # fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_s cript_name; 77 # include fastcgi_params; 78 #} ##定義負載均衡設備的 Ip ##定義負載均衡設備的 Ip ###在配置文件nginx.conf的最後一行}前,添加以下內容: 123upstream htmlservers { #定義負載均衡服務器組名稱 124 server 172.17.1.150:80; 125 server 172.17.1.152:80; 126} 127upstream phpservers{ 128 server 172.17.1.150:80; 129 server 172.17.1.152:80; 130} 131upstream picservers { 132 server 172.17.1.150:80; 133 server 172.17.1.152:80; 134} #後期工作中,根據工作中的需要,配置成具體業務的IP地址
5、重新加載nginx服務器配置文件
[root@docker-02 conf]# ./nginx -t [root@docker-02 conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6、配置後端服務器: 172.17.1.150 172.17.1.151(兩台機器同時執行)
[root@docker-01 html]# yum install httpd php -y ##另一台寫maoxiaopu進配置文件 [root@docker-01 html]# echo yunweimao > /var/www/html/index.html [root@docker-01 html]# vim /var/www/html/test.php 172.17.1.150-php <?php phpinfo(); ?>
上傳一張圖片,到172.17.1.150網站/var/www/html/目錄下:
啟動apache服務器:
[root@docker-01 html]# service httpd restart
7、測試
7.1測試靜態界面http://172.17.1.151/


7.2測試轉發動態頁面


7.3測試轉發圖片

