Linux安裝nginx並實現負載均衡

2021-09-09 07:34:05 字數 1409 閱讀 4746

由於nginx的編譯要依賴pcre中的一些類庫,所以要先安裝pcre:

先解壓:   tar -zxvf pcre-8.35.tar.gz

切換到pcre-8.35資料夾: cd pcre-8.35

執行   ./configure

編譯及安裝 :  make && make install

安裝好後檢視版本 : pcre-config --version

能正確顯示版本代表安裝成功。

安裝成功後的路徑一般為:/usr/local/lib/*pcre*

2.安裝nginx

解壓:tar -zxvf nginx-1.12.2.tar.gz

cd nginx-1.12.2

./configure

make && make install

安裝成功後的路徑:/usr/local/nginx

使用nginx:

執行 cd /usr/local/nginx/sbin,切換到nginx/sbin路徑下

./nginx 啟動nginx

./nginx -s stop 停止nginx,此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

./nginx -s quit 停止nginx,此方式停止步驟是待nginx程序處理任務完畢進行停止。

./nginx -s reload 重啟nginx,一般是重新載入配置檔案時使用

./nginx -s reopen 重啟nginx,重新開啟日誌檔案

./nginx -v 檢視nginx版本

./nginx -t 檢視配置檔案正確性

注意:

nginx啟動如果報錯:   ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: no such file or directory

執行命令:   ln -s /usr/local/lib/libpcre.so.1 /lib64/ 

3.nginx負載均衡:

要測試負載均衡,首先需要部署至少兩個tomcat,linux中部署多個tomcat的操作請移步我的另一篇部落格:

接著說負載均衡的配置:

編輯nginx.conf檔案 #gzip  on; 底下大概第34行新增服務配置,並設定每台服務的訪問權重,我這裡都設為1代表每台伺服器被分發請求的權重一樣:

upstream ivss.com

然後在底下的server {}中配置:

location /ivss/

意思是 如果攔截到請求中有/ivss/ 則將請求分發到**服務上,即

看圖說話:

Linux下安裝nginx並啟動

wget 2020 06 23 15 42 43 正在解析主機 nginx.org nginx.org 95.211.80.227,62.210.92.35,2001 1af8 4060 a004 21 e3 正在連線 nginx.org nginx.org 95.211.80.227 443.已連...

Linux安裝nginx並配置HTTPS

centos7安裝nginx有兩種方式 編譯原始碼安裝 yum安裝。將原始碼解壓到 usr local src 目錄 tar zxvf nginx 1.19.6.tar.gz c usr local src 安裝依賴 yum y install gcc pcre devel zlib devel o...

docker下安裝nginx並實現https訪問

一 啟動容器 docker run detach name wx nginx p 443 443 p 80 80 v home nginx data usr share nginx html rw v home nginx config nginx.conf etc nginx nginx.conf...