Nginx upstream(Load Balance)
Load Balance 手段
- Nginx可用來做正向(Forward)、反向(Reverse)代理
- 針對網址的流水號 or 特定參數,做切割分派
- 動靜態資料分流
- HTTP 1.1 Long polling(長握手),改用 301 將 Connection keep 到目標 server 上,別綁在 proxy server 身上
正向代理
server {
listen 8080;
resolver 168.95.1.1;
access_log off;
location / {
proxy_pass http://$host$request_uri;
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
}
}
反向代理
- round-robin(Default) 照順序輪,會跳過 Down 掉的 server
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
}
server {
listen 80;
location / {
# 傳遞真實IP到後端
#proxy_set_header Host $http_host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myapp1;
}
}
}
- Weight 權重
upstream myapp1 {
server srv1.example.com weight=10;
server srv2.example.com;
}
- IP-Hash 用IP雜湊分配,可解決 session 問題
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
}
- Least Connect 最少連線數的 server 優先使用
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
}
- fair(第三方)按後端服務器的回應時間來分配,反映快的優先分配。
upstream myapp1 {
server server1;
server server2;
fair;
}
- url_hash(第三方)
- 按訪問url的hash結果來分配,同 hash 指向同一個服務器,後端服務器為緩存時比較有效。
- 例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數
upstream myapp1 {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
- SSL 1 external port to N internal port (process)
- 將同一個服務,在內部用不同的 port 分開
upstream upstream_1{
server 0.0.0.0:1338;
server 0.0.0.0:1339;
server 0.0.0.0:1340;
server 0.0.0.0:1341;
}
server {
listen 1337 ssl default_server;
server_name api.mydomain.com;
ssl_certificate /var/www/abc/ssl/apache.crt;
ssl_certificate_key /var/www/abc/ssl/mydomain.key;
ssl_client_certificate /var/www/abc/ssl/mydomain.chain.crt;
ssl_verify_client off;
location ~ / {
proxy_pass https://upstream_1;
}
}
server 設定
- down 暫時不參與負載
- weight 數字越大,負載的權重就越大。
- max_fails 允許失敗的預設次數,超過時,回傳 proxy_next_upstream 模組定義錯誤
- fail_timeout 失敗N次失敗後,暫停的時間。
- backup 備援機,在其他 server 忙碌或 down 才會動用
切割網址
動靜態分流
server {
listen 80;
server_name www.domain.com;
#ex. /profile/alice
location ~* /profile/[a-m] {
proxy_pass http://server0.domain.com;
}
#ex. /profile/nick
location ~* /profile/[n-z] {
proxy_pass http://server1.domain.com;
}
#ex. /profile/3002
location ~* /profile/[0-9]*[02468]$ {
proxy_read_timeout 10s;
proxy_pass http://server0.domain.com;
}
}
動靜態分流
location / {
# css, js 靜態文件設置有效期12小時天
location ~ .*\.(js|css)$ {
access_log off; #不記入 log
expires 12h;
}
#圖片設置有效期3天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 3d;
}
}
location ~ \.php$ {
...
proxy_pass xxx;
}
location @proxy {
...
}
Long Pollingserver {
listen 80;
server_name www.my-domain.com;
location ~* /profile/[a-m] {
rewrite ^(.*) http://server0.domain.com$1 permanent;
}
location ~* /profile/[n-z] {
rewrite ^(.*) http://server1.domain.com$1 permanent;
}
}
https://my.oschina.net/foreverich/blog/1517128
http://blog.yslin.tw/2012/02/nginx.html
Nginx upstream(Load Balance)
Reviewed by Wild
on
11/24/2018 03:39:00 下午
Rating:
沒有留言:
沒有Google帳號也可發表意見唷!