負載均衡
- 此處指的是選擇一種策略,盡量把請求均勻的分布到每一個上游服務器
upstream
- 配置塊 http
- 語法 upstream name{}
- 定義一個上游服務器的集群,便于反向代理的proxy_pass使用
upstream backend {
server 127.0.0.1:5566 weight=1 max_fails=2 fail_timeout=30s;
}
server {
location / {
proxy_pass http://backend; #backend指上游服務器
}
}
server
- 配置塊 upstream
- 語法 server names[parameters]
- 指定一臺上游服務器的名字 可以是域名,ip,UNIX句柄
parameters | 定義 |
---|---|
weight=number | 上游服務器轉發的權重 默認1 |
max_fails=number | 在fail_timeout時間內,如果向上游服務器轉發失敗次數超過number,就認為該服務器在此時間段里不可用,默認1 設為0就不檢查失敗次數 |
fail_timeout=time | 用于優化反向代理功能,與連接、讀取、響應超時無關,默認10秒 |
down | 永久下線服務器,只在使用ip_hash有效 |
backup | 在使用ip_hash時無效 表示上游服務器只是備份服務器,只有在所有非備份上游服務器失效后,才會指向備份 |
ip_hash
- 配置塊 upstream
- 語法 ip_hash;
- 將單個用戶的請求固定到某個上游服務器
先根據客戶端的ip地址計算一個key,將可以按照upstream集群的上游服務器數量進行取模,再根據取模的結果把請求地址轉發到相應的上游服務器,不能與weight同時使用,在標識上游服務器不可用時,要用down不能直接刪除,確保轉發策略一貫性
upstream backend {
ip_hash;
server 127.0.0.1:5566 weight=1 max_fails=2 fail_timeout=30s;
server backend down;
}
記錄日志時支持的變量
變量名 | 定義 |
---|---|
$upstream_addr | 處理請求的上游服務器地址 |
$upstream_cache_status | 表示是否命中緩存,取值范圍:MISS、EXPIRED、UPDATING、SATLE、HIT |
$upstream_status | 上游服務器返回的響應中HTTP響應碼 |
$upstream_response_time | 上游服務器響應時間,精度毫秒 |
$upstream_http_$HEADER | http頭部,如upstream_http_host |
log_format timing '$remote_addr - $remote_user [$time_local] $request '
'upstream_response_time $upstream_response_time '
'msec $msec request_time $request_time';
log_format up_head '$remote_addr - $remote_user [$time_local] $request '
'upstream_http_content_type $upstream_http_content_type'
反向代理配置
proxy_pass
- 語法:proxy_pass URL;
- 配置塊 location、if
將當前請求反向代理到URL參數指定的服務器上,URL可以是主機名或ip地址
proxy_pass http://localhost:3000/uri/;
可以加上負載均衡 使用upstream
upstream backend {...}
server {location / {
proxy_pass http://backend; #backend指上游服務器
}}
可以把httpz轉換成https
proxy_pass https://192.168.0.1/;
若需要轉發host頭部
proxy_set_header Host $host;
proxy_method
- 語法:proxy_method method;
- 配置塊 http、server、location
轉發時的協議方法名
proxy_method POST; 客戶端的GET請求也會被轉發成POST
proxy_hide_header
- 語法:proxy_hide_header the_header;
- 配置塊 http、server、location
Nginx會將上游服務器的響應轉發給客戶端,但默認不會轉發以下HTTP頭部字段:Date、Server、X-Pad和X-Accel-*.使用proxy_hide_header可以指定哪些不能轉發
proxy_hide_header Cache-Control;
proxy_pass_header
- 語法:proxy_pass_header the_header;
- 配置塊 http、server、location
將原來禁止轉發的header設為轉發
proxy_pass_request_body
- 語法:proxy_pass_request_body on|off;
- 默認 on;
- 配置塊 http、server、location
是否向上游轉發http body
proxy_pass_request_headers
- 語法:proxy_pass_request_headers on|off;
- 默認 on;
- 配置塊 http、server、location
是否向上游轉發http header
prxoy_redirect
- 語法:prxoy_redirect [default|off|redirect rep|replacement]];
- 默認 default;
- 配置塊 http、server、location
當上游返回響應是重定向或刷新請求(301,302),proxy_redirect 可以重設HTTP頭部的location或refresh字段。
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
對location字段的URI是http://localhost:8000/two/some/uri.實際轉發給客戶端的是http://frontend/one/;
可以使用ngx-http-core-module提供的變量來設置
proxy_redirect http://localhost:8000/ http://$host:$server_port/;
可以省略replacement參數的主機名部分,這時會用虛擬主機名稱填充
proxy_redirect http://localhost:8000/two/ /one/;
使用default參數時,會按照proxy_pass配置項和所屬的location配置項重組
location /one/ {
proxy_pass http://upstream:port/two/;
proxy_redirect default;
}
等于
location /one/ {
proxy_pass http://upstream:port/two/;
proxy_redirect http://upstream:port/two/ /one/;
}
proxy_next_upstream
- 語法:proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off];
- 默認 error timeout;
- 配置塊 http、server、location
當向一臺上游轉發請求出錯時,繼續換一臺處理
(invaild_header 上游響應不合法)