Mastering Nginx 笔记三----upstream模块解释

Upstream module directives


ip_hash

Ensures the distribution of connecting clients evenly over

all servers by hashing the IP address, keying on its class-C

network.

keepalive

The number of connections to upstream servers that

are cached per worker process. When used with HTTP

connections, proxy_http_version should be set to 1.1

and proxy_set_header to Connection "".

least_conn

Activates the load-balancing algorithm where the server

with the least number of active connections is chosen for

the next new connection.

server

Defines an address (domain name or IP address with an

optional TCP port, or path to a UNIX-domain socket)

and optional parameters for an upstream server. The

parameters are:

     weight: It sets the preference for one server over

    another

     max_fails: It is the maximum number of

    unsuccessful communication attempts to a server

    within fail_timeout before the server is marked

    as down

     fail_timeout: It is the length of time a server

    has to respond to a request and the length of time

    a server will be marked as down

     backup: It will only receive requests once the

    other servers are down

     down: It marks a server as not able to

    process requests


The  keepalive directive deserves special mention. NGINX will keep this number of

connections per worker open to an upstream server


Memcached upstream servers


    upstream memcaches {

    server 10.0.100.10:11211;

    server 10.0.100.20:11211;

    }

    server {

    location / {

    set  $memcached_key "$uri?$args";

    memcached_pass  memcaches;

    error_page 404 = @appserver;

    }

    location @appserver {

    proxy_pass http://127.0.0.1:8080;

    }

    }


The  memcached_pass directive uses the  $memcached_key variable to make the key

lookup. If there is no corresponding value ( error_page 404 ), we pass the request

on to  localhost , where there is presumably a server running that will handle this

request and insert a key/value pair into the  memcached instance


Determining the client's real IP address


proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;



你可能感兴趣的:(nginx)