Nginx Upstream 模塊

Nginx Upstream 模塊

官方文檔: ngx_http_upstream_module

Example Configuration
Directives 指令
? upstream
? server
? zone
? state
? hash
? ip_hash
? keepalive
? keepalive_requests
? keepalive_timeout
? ntlm
? least_conn
? least_time
? queue
? random
? sticky
? sticky_cookie_insert
Embedded Variables

The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass directives.

upstream模塊功能是定義服務(wù)器組,可以被 proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass 指令引用.

Example Configuration 配置樣例

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

Directives 指令

upstream

Syntax: upstream name { ... }
Default:
Context: http

Defines a group of servers. Servers can listen on different ports. In addition, servers listening on TCP and UNIX-domain sockets can be mixed.

定義的服務(wù)器組,服務(wù)器可以監(jiān)聽到不同端口,另外,可監(jiān)聽 tcp 和 Unix sockets.

Example:

upstream backend {
    #默認(rèn)使用輪詢負(fù)載,加 weight是權(quán)重輪詢,權(quán)重越大分配的請(qǐng)求越多
    server backend1.example.com weight=5; 
    #后端tcp 監(jiān)聽 8080端口,30秒內(nèi)最大失敗次數(shù)3
    server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3; #sockets連接
    #所有后端失敗或者繁忙時(shí),才考慮使用backup
    server backup1.example.com  backup; 
}

server

Syntax: server address [parameters];
Default:
Context: upstream
Directives
weight=number sets the weight of the server, by default, 1. 設(shè)置服務(wù)器權(quán)重,默認(rèn)為一,越大則分配請(qǐng)求越多
max_conns=number limits the maximum *number* of simultaneous active connections to the proxied server (1.11.5). Default value is zero, meaning there is no limit. If the server group does not reside in the shared memory, the limitation works per each worker process. 限制與代理服務(wù)器同時(shí)活動(dòng)的最大連接數(shù),默認(rèn)值為0,意味著沒(méi)有限制.
max_fails=number sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream,scgi_next_upstream, memcached_next_upstream, and grpc_next_upstream directives. 設(shè)置在fail_timeout參數(shù)設(shè)置的持續(xù)時(shí)間內(nèi)應(yīng)該發(fā)生的與服務(wù)器通信失敗的嘗試次數(shù),以考慮在同樣由fail_timeout參數(shù)設(shè)置的持續(xù)時(shí)間內(nèi)服務(wù)器不可用。默認(rèn)情況下,不成功嘗試的次數(shù)設(shè)置為1。0值禁止對(duì)嘗試進(jìn)行計(jì)算。不成功的嘗試由proxy_next_upstream、fastcgi_next_upstream、uwsgi_next_upstream、scgi_next_upstream、memcached_next_upstream和grpc_next_upstream指令定義。
fail_timeout=time the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable; and the period of time the server will be considered unavailable.By default, the parameter is set to 10 seconds. 在fail_timeout時(shí)間內(nèi),與服務(wù)器通信失敗 max_fails 次,認(rèn)為服務(wù)器不可用.服務(wù)不可用時(shí)間為 fail_timeout.默認(rèn)為10秒.
backup marks the server as a backup server. It will be passed requests when the primary servers are unavailable. 標(biāo)記此服務(wù)器為備份服務(wù)。在全部服務(wù)器不可用時(shí)才會(huì)將請(qǐng)求發(fā)送給它.
down marks the server as permanently unavailable 標(biāo)記此服務(wù)器長(zhǎng)期不可用

ip_hash

Syntax: ip_hash;
Default:
Context: upstream

Example:

upstream backend {
    ip_hash;  #每個(gè)請(qǐng)求按照訪問(wèn)ip(即Nginx的前置服務(wù)器或者客戶端IP)的hash結(jié)果分配,這樣每個(gè)訪客會(huì)固定訪問(wèn)一個(gè)后端服務(wù)器,可以解決session一致問(wèn)題。

    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
    server backend4.example.com;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。