upstream
Nginx支持負載均衡,可以很方便的幫助我們進行水平擴容,upstream就是nginx中的負載均衡模塊
- 當客戶端發(fā)送請求時,會先到Nginx,然后Nginx會將請求分發(fā)到后臺不同的服務器上。
- 如果后臺的服務器群中有一個宕機了,那么Nginx會自動忽略這臺服務器,不會將請求再次分發(fā)到這臺服務器上。
- 如果有新加入的服務器,修改配置后,Nginx也會將請求分發(fā)到這臺服務器上。
用法
參照Nginx中文文檔,可以得到簡單的配置方案如下。
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
}
server {
location / {
proxy_pass http://backend;
}
}
實戰(zhàn)遇到的問題
當使用不同域名不同云廠商進行負載均衡策略時,Post等類型請求被轉發(fā)為了Get,導致服務異常。原因是域名使用了https策略,并自動重寫了http請求。文章來源:http://www.zghlxwxcb.cn/news/detail-790674.html
解決方案:在寫配置時添加Https及端口。文章來源地址http://www.zghlxwxcb.cn/news/detail-790674.html
upstream testUpstream{
ip_hash;
server www.xxx.com:443 weight=5;
server www.xxx.com:443 weight=3 max_fails=1 fail_timeout=30s;
}
server {
listen 80;
server_name www.xxx.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl http2;
server_name www.xxx.com;
#core
location /testUpstream/api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass https://testUpstream/;
}
}
到了這里,關于使用Nginx的upstream實現(xiàn)負載均衡,并配置https,避免Post請求類型轉發(fā)后變?yōu)镚et的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!