Nginx負載均衡
- 編譯安裝
- Nginx目錄結構
- Nginx配置檔案結構
- Nginx具體應用
- 部署靜态資源
- 反向代理
- 正向代理
- 負載均衡
編譯安裝
./configure --prefix=/usr/local/nginx
make && make install
如果出現警告或報錯
提示
checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
#安裝gcc
yum install
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
#安裝perl庫
yum install
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
#安裝zlib庫
yum install
接下來執行
make
make install
Nginx目錄結構
重點目錄/檔案:
- conf/nginx.conf nginx配置檔案
- html 存放靜态檔案(html、css、js等)
- logs 日志目錄、存放日志檔案
- sbin/nginx 二進制檔案,用于啟動、停止Nginx服務
指令
檢查檔案正确性
在啟動Nginx服務之前,可以檢查conf/nginx.conf檔案配置的是否有錯誤
./nginx -t
啟動和停止
#啟動
./nginx
#停止
./nginx -s stop
#啟動完成後可以檢視Nginx程序
ps -ef | grep
重新加載配置
./nginx -s reload #重新加載配置
Nginx配置檔案結構
整體結構介紹
Nginx配置檔案(conf/nginx.conf)整體分為三部分:
- 全局塊 和Nginx運作相關的全局配置
- events塊 和網絡連接配接相關的配置
- http塊 代理、緩存、日志記錄、虛拟主機配置
- http全局塊
- Server塊
- Server全局塊
- location塊
注意:http塊中可以配置多個Server塊,每個Server塊中可以配置多個location塊。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SM4cTN2YmYhRzNxEzNjFDZyYzXzETO1MDM2EzLcdDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
Nginx具體應用
部署靜态資源
Nginx可以作為靜态web伺服器來部署靜态資源。靜态資源旨在服務端真實存在并且存在并且能夠直接展示的一些檔案,比如常見的html頁面、css、js、images、video
相對于Tomcat,Nginx處理靜态資源的能力更加高效,是以在生産環境下,一般都會将靜态資源部署到Nginx中 。将靜态資源部署到Nginx非常簡單,隻需要将檔案複制到Nginx安裝目錄下的html目錄即可。
server{
listen 80; #監聽端口
server_name localhost; #伺服器名稱
location /{ #比對用戶端請求url
root html; #指定靜态資源根目錄
index index.html; #指定預設首頁
}
}
反向代理
反向代理伺服器位于使用者與目标伺服器之間,但是對于使用者而言,反向代理伺服器就相當于目标伺服器,及使用者直接通路代理伺服器就可以獲得目标伺服器之間的資源,反向代理伺服器負責将請求轉發給目标伺服器。
使用者不需要知道目标伺服器的位址,也無需在使用者端做任何設定。
配置檔案
server{
listen 82;
server_name localhost;
location /{
proxy_pass http://192.168.16.129:8080;
}
}
正向代理
是一個位于用戶端和原始伺服器(origin server)之間的伺服器,為了從原始伺服器取得内容,用戶端向代理發送一個請求并指定目标(原始伺服器),然後代理向原始伺服器轉交請求并将獲得的内容傳回給用戶端。
正向代理的典型用途為在防火牆内的區域網路用戶端提供通路Internet的途徑
正向代理一般是在用戶端設定代理伺服器,通過代理伺服器轉發請求,最終通路到目标伺服器
負載均衡
早期的網站流量和業務功能都比較單一,單台伺服器就可以滿足基本需求,但是随着網際網路的發展,業務流量越來越大并且業務也越來越複雜,單台伺服器的性能及單點故障問題就凹陷出來了,是以需要多台伺服器組成應用叢集,進行性能的水準擴充以及避免單點故障出現。
應用叢集:将同一應用部署到多台機器上,組織應用叢集,接收負載均衡器分發的請求,進行業務處理并傳回相應資料
負載均衡器:将使用者請求根據對應的負載均衡算法分發到應用叢集中的一台伺服器進行處理
負載均衡政策
名稱 | 說明 |
輪詢 | 預設方式 |
weight | 權重方式 |
ip_hash | 依據ip配置設定方式 |
least_conn | 依據最少連接配接方式 |
url_hash | 依據url配置設定方式 |
配置負載均衡:
upstream targetserver{
server 192.168.16.129:8080 weight=10;
server 192.168.16.129:8081 weight=5;
}
server{
listen 82;
server_name localhost;
location /{
proxy_pass http://targetserver;
}
}