Module ngx_http_memcached_module

优质
小牛编辑
125浏览
2023-12-01

The ngx_http_memcached_module module allows to obtain responses from a memcached server. The key is set in the $memcached_key variable. A response should be put in memcached in advance via means that are external to nginx.

Example Configuration

server {
    location / {
        set            $memcached_key "$uri?$args";
        memcached_pass host:11211;
        error_page     404 502 504 = @fallback;
    }

    location @fallback {
        proxy_pass     http://backend;
    }
}

Directives

syntax:memcached_bind address | off;
default:
context:http, server, location

This directive appeared in version 0.8.22.

Forces outgoing connections to a memcached server to originate from the specified local IP address. Value of the parameter can contain variables (1.3.12). The special value off (1.3.12) cancels the effect of the memcached_bind directive inherited from the previous configuration level, allowing the system to auto-assign local address.

syntax:memcached_buffer_size size;
default:
memcached_buffer_size 4k|8k;
context:http, server, location

Sets size of the buffer used for reading a response received from the memcached server. A response is passed to a client synchronously, immediately as it is received.

syntax:memcached_connect_timeout time;
default:
memcached_connect_timeout 60s;
context:http, server, location

Defines a timeout for establishing a connection with the memcached server. It should be noted that this timeout cannot usually exceed 75 seconds.

syntax:memcached_gzip_flag flag;
default:
context:http, server, location

This directive appeared in version 1.3.6.

Enables the test for the flag presence in the memcached server response and sets the request header field “Content-Encoding” to “gzip” if the flag is set.

syntax:memcached_next_upstream error | timeout | invalid_response | not_found | off ...;
default:
memcached_next_upstream error timeout;
context:http, server, location

Specifies in which cases a request should be passed to the next server:

error
an error occurred while establishing a connection with the server, passing it a request, or reading the response header;
timeout
a timeout has occurred while establishing a connection with the server, passing it a request, or reading the response header;
invalid_response
a server returned empty or invalid response;
not_found
a response was not found on the server;
off
disables passing a request to the next server.

It should be understood that passing a request to the next server is only possible if a client was not sent anything yet. That is, if an error or a timeout occurs in the middle of transferring a response, fixing this is impossible.

syntax:memcached_pass address;
default:
context:location, if in location

Sets an address of the memcached server. An address can be specified as a domain name or an address, and a port, for example,

memcached_pass localhost:11211;

or as a UNIX-domain socket path:

memcached_pass unix:/tmp/memcached.socket;

If a domain name resolves to several addresses, all of them will be used in a round-robin fashion. In addition, an address can be specified as a server group.

syntax:memcached_read_timeout time;
default:
memcached_read_timeout 60s;
context:http, server, location

Defines a timeout for reading a response from the memcached server. A timeout is only set between two successive read operations, not for the transmission of the whole response. If a memcached server does not transmit anything within this time, a connection is closed.

syntax:memcached_send_timeout time;
default:
memcached_send_timeout 60s;
context:http, server, location

Sets a timeout for transmitting a request to the memcached server. A timeout is only set between two successive write operations, not for the transmission of the whole request. If a memcached server does not receive anything within this time, a connection is closed.