反向代理:
這種代理方式叫做,隧道代理。有性能瓶頸,因為所有的數(shù)據(jù)都經(jīng)過Nginx,所以Nginx服務器的性能至關(guān)重要
負載均衡:
把請求,按照一定算法規(guī)則,分配給多臺業(yè)務服務器(即使其中一個壞了/維護升級,還有其他服務器可以繼續(xù)提供服務)
反向代理+負載均衡:
nginx.conf配置文件
啟用proxy_pass,root和index字段就會失效
proxy_pass后的地址必須寫完整 http://xxx
,不支持https
當訪問localhost時(Nginx服務器),網(wǎng)頁打開的是http://xxx
(應用服務器),網(wǎng)頁地址欄寫的還是localhost
http{
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://xxx;
#root html/test;
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
**定義地址別名 **
使用upstream定義一組地址【在server字段下】
訪問localhost,訪問都會代理到192.168.174.133:80
和192.168.174.134:80
這兩個地址之一,每次訪問這兩個地址輪著切換(后面講到,因為默認權(quán)重相等)
http{
upstream httpds{
server 192.168.174.133:80; #如果是80端口,可以省略不寫
server 192.168.174.134:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://httpds;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
設置權(quán)重
訪問使用哪個地址的權(quán)重
upstream httpds{
server 192.168.174.133:80 weight=10;
server 192.168.174.134:80 weight=80;
}
關(guān)閉
upstream httpds{
server 192.168.174.133:80 weight=10 down;
server 192.168.174.134:80 weight=80;
}
備用機
如果192.168.174.133:80
出現(xiàn)故障,無法提供服務,就用使用backup的這個機器
upstream httpds{
server 192.168.174.133:80 weight=10;
server 192.168.174.134:80 weight=80 backup;
}
動靜分離:
當用戶請求時,動態(tài)請求分配到Tomcat業(yè)務服務器,靜態(tài)資源請求放在Nginx服務器中
例子:
- 如果請求的資源地址是
location/
,/
的優(yōu)先級比較低,如果下面的location沒匹配到,就會走http://xxx這個地址的機器 - 如果請求的資源地址是
location/css/*
,就會被匹配到nginx的html目錄下的css文件夾中(我們把css靜態(tài)資源放在這個位置)
server {
listen 80;
server_name localhost;
location / { # /的優(yōu)先級比較低,如果下面的location沒匹配到,就會走http://xxx這個地址的機器
proxy_pass http://xxx;
}
location /css { # root指的是html,location/css指的是root下的css,所以地址就是html/css
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
使用正則
location ~*/(js|css|img){
root html;
index index.html index.htm;
}
URL重寫:
rewrite是URL重寫的關(guān)鍵指令,根據(jù)regex(正則表達式)部分內(nèi)容,重定向到replacement,結(jié)尼是flag標記。
rewrite <regex> <replacement> [flag];
關(guān)鍵字 正則 替代內(nèi)容 flagt標記
正則:per1森容正則表達式語句進行規(guī)則匹配
替代內(nèi)容:將正則匹配的內(nèi)容替換成replacement
flag標記說明:
last #本條規(guī)則匹配完成后,繼續(xù)向下匹配新的1ocation URI規(guī)則
break #本條規(guī)則匹配完成即終止,不再匹配后面的任何規(guī)則
redirect #返回302臨重定向,游覽器地址會顯示跳轉(zhuǎn)后的URL地址
permanent #返回301永久重定向,測覽器地址欄會顯示跳轉(zhuǎn)后的URL地址
瀏覽器地址欄訪問 xxx/123.html
實際上是訪問xxx/index.jsp?pageNum=123
server {
listen 80;
server_name localhost;
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
proxy_pass http://xxx;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
網(wǎng)關(guān)服務器:
上圖中,應用服務器,不能直接被外網(wǎng)訪問到,只能通過Nginx服務器進行訪問(使用proxy_pass),這時候這臺Nginx服務器就成為了網(wǎng)關(guān)服務器(承擔入口的功能)
所以,我們啟動應用服務器的防火墻,設置其只能接受這臺Nginx服務器的請求
添加rich規(guī)則
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.174.135" port protocol="tcp" port="8080" accept" #這里的192.168.174.135是網(wǎng)關(guān) 服務器地址
移除rich規(guī)則
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.174.135" port port="8080" protocol="tcp" accept"
重啟
移除和添加規(guī)則都要重啟才能生效文章來源:http://www.zghlxwxcb.cn/news/detail-521883.html
firewall-cmd --reload
查看所有規(guī)則
firewall-cmd --list-all #所有開啟的規(guī)則
先贊后看,養(yǎng)成習慣?。?!^ _ ^ ?? ?? ??
碼字不易,大家的支持就是我的堅持下去的動力。點贊后不要忘了關(guān)注我哦!文章來源地址http://www.zghlxwxcb.cn/news/detail-521883.html
到了這里,關(guān)于06、Nginx反向代理與負載均衡的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!