nginx_http_upstream_conf
Overview
upstream holds the backend information, how to select backend and free the peer connection based on balance algorithm.
explicit upstream defined in this way.
1 | upstream up_1 { |
There is also another way to define an upstream called implicit upstream, for implicit upstream, there is no way to define balance algorithm(round robin is used) and set parameters for it.
1 | location /abc { |
Http upstream
location may refer to an upstream by proxy_pass
directive, that means at conf phase, we link the location to the upstream, later on when request matches that location, we select a server from the linked upstream, create a connection for the selected server, send request to it.
how server is selected
server selection depends on balance algorithm, nginx supports several algorithms
- round robin
- least connection
- ip hash
each algorithm must provide peer.get
and peer.free
- peer.get(): select a server, update counter, its return value:
- NGX_DONE: get a connected connection
- NGX_OK: get a server, need to create new connection
- NGX_BUSY: no server is available
- peer.free(): free resource(free counter etc) used by peer.get.
what port is used when connecting upstream server
it depends how upstream is defined and used.
explicit upstream
we use port defined by server in upstream itself, if server has no port configured, for proxy_pass http://
use 80, for proxy_pass https://
use 443, never use port from url itself
1 | http { |
implicit upstream
we always use port from url itself, if no port in url, use 80 for http, 443 for https when connection with backend.
1 | http { |
uri sent to upstream server
uri sent to upstream server depends on how proxy_pass is used.
case: proxy_pass without uri
1 | # the request URI is passed to the server in the same form as by a client when the original request is processed |
case: proxy_pass has uri which uses variable
1 | # we use uri from variable directly to replace original uri |
case: proxy_pass with uri which is not variable
1 | # the part of a normalized request URI matching the location is replaced by a URI specified in the directive |
details refer to ngx_http_proxy_create_request()
.