前言
Nginx做的代理后面SpringBoot的項目,1N3T的架構,Tomcat的配置也進行了相應的調優。
配置
這里主要來簡單的說下Nginx的端口負載均衡,具體的大家可以參考
Nginx文檔 | Nginx 烹調書(Nginx Cookbook 中文版) | Nginx文檔PLUS
upstream是nignx分配方式,默認是輪詢
1. 輪詢: 輪詢是upstream的默認分配方式,即每個請求按照時間順序輪流分配到不同的后端服務器。
2. weight: 按着權重的大小進行輪詢。
3. ip_hash: 每個請求按照訪問ip的hash結果分配,這樣每個訪客會固定訪問一個后端服務器,可以解決session一致問題。
4. fair: 可以根據頁面大小、加載時間長短智能的進行負載均衡,響應時間短的后端服務器優先分配請求。
5. url_hash: 與ip_hash類似,但是按照訪問url的hash結果來分配請求,使得每個url定向到同一個后端服務器,主要應用於后端服務器為緩存時的場景下。
6. least_conn: 把請求轉發給連接數較少的后端服務器
輪詢
# max_fails=2 fail_timeout=30s 代表在30秒內請求某一應用失敗2次,認為該應用宕機,等待30秒再次請求
upstream pool {
server 192.168.10.1:6001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:51001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6005 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6004 weight=10 max_fails=2 fail_timeout=30s;
}
ip_hash
upstream pool {
ip_hash;
server 192.168.10.1:6001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:51001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6005 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6004 weight=10 max_fails=2 fail_timeout=30s;
}
完整配置
我這邊是虛擬主機的,所以直接配置對應的虛擬主機的配置就好了,完整配置如下
# 負載的地址池
upstream pool {
ip_hash;
server 192.168.10.1:6001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:51001 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6005 weight=10 max_fails=2 fail_timeout=30s;
server 192.168.10.1:6004 weight=10 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
listen 443 ssl;
server_name app.xxxx.net;
index index.html index.htm index.php default.html default.htm default.php;
underscores_in_headers on;
root /usr/local/nginx/conf/html;
ssl_certificate /cert/app.xxxx.net.pem;
ssl_certificate_key /cert/app.xxxx.net.key;
ssl_session_timeout 5m;
ssl_ciphers xxxxxxxxxxxxxxxx;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header x-forwarded-for $remote_addr;
#地址池名稱pool
proxy_pass http://pool;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
#地址池名稱pool
proxy_pass http://pool;
expires 30d;
}
location ~ .*\.(js|css)?$ {
#地址池名稱pool
proxy_pass http://pool;
expires 12h;
}
access_log /home/wwwlogs/app.xxxx.net.log;
}
pool
是自己命名的一個變量,你可以命名為別的值
Nginx端口負載均衡就是這么的簡單,但是不要忘記reload一下哦~