Nginx實現負載均衡功能

2021-10-08 03:06:32 字數 2459 閱讀 6824

nginx是一款輕量級的web 伺服器、反向**伺服器、電子郵件(imap/pop3)**伺服器。

高併發連線:官方測試nginx能夠支撐5萬併發連線,實際測試可達到3萬左右,每天可以處理億次訪問量;原因是:採用最新epoll(linux2.6核心)和kqueue(freebsd)網路i/o模型,而apache採用的是傳統的select模型

記憶體消耗小

nginx支援負載均衡

nginx支援反向**

成本低廉

構建編譯環境

yum install -y gcc gcc-c++
nginx安裝需要依賴以下三個包(此處安裝rpm包)

yum install -y zlib zlib-devel

yum install -y pcre pcre-devel

yum install -y openssl openssl-devel
此處提供三個依賴包原始碼包安裝方式(rpm包安裝和原始碼包安裝選擇一種即可):

openssl :

[root@localhost] tar zxvf openssl-fips-2.0.9.tar.gz

[root@localhost] cd openssl-fips-2.0.9[root@localhost] ./config && make && make install

pcre:

[root@localhost] tar zxvf pcre-8.36.tar.gz

[root@localhost] cd pcre-8.36[root@localhost] ./configure && make && make install

zlib:

[root@localhost] tar zxvf zlib-1.2.8.tar.gz

[root@localhost] cd zlib-1.2.8[root@localhost] ./configure && make && make install

檢視nginx安裝路徑

[root@localhost] whereis nginx
檢查是否安裝成功

[root@localhost] cd /usr/local/nginx/sbin

[root@localhost] ./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

nginx指令碼檔案配置(由於是原始碼包安裝,所以nginx相關命令只能在sbin目錄下執行,過於繁瑣,現在把nginx命令的指令碼檔案新增到系統服務,就可以直接用server命令service nginx ~來操作nginx)

nginx檔案顯示白色,表示許可權不夠,授權

[root@localhost] chmod 755 nginx
將nginx服務加入chkconfig管理列表

[root@localhost] chkconfig --add /etc/init.d/nginx
設定nginx開機自啟

vim /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

設定執行許可權:

至此,nginx安裝完畢

用xftp將apache-tomcat-8.5.24.tar.gz上傳至/root目錄

解壓apache-tomcat-8.5.24.tar.gz至/opt目錄,並更名為tomcat

tar -zvxf apache-tomcat-8.5.24.tar.gz    //

解壓檔案

mv apache-tomcat-8.5.24 /opt //

轉移檔案

mv apache-tomcat-8.5.24 tomcat //

檔案更名

關閉防火牆

service iptables stop

chkconfig iptables off

(真實伺服器ip1:192.168.182.131;真實伺服器ip2:192.168.182.132;nginx伺服器ip:192.168.182.130)

因為兩台tomcat伺服器測試跳轉的頁面都是tom貓,為了顯示負載均衡效果,需要更改tomcat伺服器的顯示頁面;

Nginx實現負載均衡

負載均衡 建立在現有網路結構之上,它提供了一種廉價有效透明的方法擴充套件網路裝置和伺服器的頻寬 增加吞吐量 加強網路資料處理能力 提高網路的靈活性和可用性。負載均衡,英文名稱為load balance,其意思就是分攤到多個操作單元上進行執行,例如web伺服器 ftp伺服器 企業關鍵應用伺服器和其它關...

nginx實現負載均衡

目錄結構 conf 配置目錄 html 靜態頁面目錄 logs 日誌目錄 sbin 主程式 nginx支援的負載均衡方式 輪詢 預設 每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。weight 指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的...

nginx實現負載均衡

初步設想 1個前端伺服器 n個應用伺服器,實現不宕機發布。前提 後端應用共享session,可以考慮redis實現。初步實驗 nginx 2個tomcat執行測試 先準備乙個nginx,nginx.conf檔案內容如下 然後準備2個tomcat 設定埠分別為8084 8086。在root資料夾下面各...