Path: blob/main/xml/en/docs/http/ngx_http_memcached_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Igor Sysoev4Copyright (C) Nginx, Inc.5-->67<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">89<module name="Module ngx_http_memcached_module"10link="/en/docs/http/ngx_http_memcached_module.html"11lang="en"12rev="19">1314<section id="summary">1516<para>17The <literal>ngx_http_memcached_module</literal> module is used to obtain18responses from a memcached server.19The key is set in the <var>$memcached_key</var> variable.20A response should be put in memcached in advance by means21external to nginx.22</para>2324</section>252627<section id="example" name="Example Configuration">2829<para>30<example>31server {32location / {33set $memcached_key "$uri?$args";34memcached_pass host:11211;35error_page 404 502 504 = @fallback;36}3738location @fallback {39proxy_pass http://backend;40}41}42</example>43</para>4445</section>464748<section id="directives" name="Directives">4950<directive name="memcached_allow_upstream">51<syntax><value>string</value> ...</syntax>52<default/>53<context>http</context>54<context>server</context>55<context>location</context>56<appeared-in>1.29.3</appeared-in>5758<para>59Defines conditions under which access to a memcached server60is allowed or <link id="denied">denied</link>.61If all string parameters are not empty62and not equal to “0” then the access is allowed.63The conditions are evaluated each time64before a connection to a memcached server is established.65Parameter values can contain variables:66<example>67geo $upstream_last_addr $allow {68volatile;6910.10.0.0/24 1;70}7172server {73listen 127.0.0.1:8080;7475location / {76memcached_pass host:11211;77memcached_allow_upstream $allow;78...79}80}81</example>82</para>8384<para>85<note>86This directive is available as part of our87<commercial_version>commercial subscription</commercial_version>.88</note>89</para>9091</directive>929394<directive name="memcached_bind">95<syntax>96<value>address</value>97[<literal>transparent </literal>] |98<literal>off</literal></syntax>99<default/>100<context>http</context>101<context>server</context>102<context>location</context>103<appeared-in>0.8.22</appeared-in>104105<para>106Makes outgoing connections to a memcached server originate107from the specified local IP address with an optional port (1.11.2).108Parameter value can contain variables (1.3.12).109The special value <literal>off</literal> (1.3.12) cancels the effect110of the <literal>memcached_bind</literal> directive111inherited from the previous configuration level, which allows the112system to auto-assign the local IP address and port.113</para>114115<para id="memcached_bind_transparent">116The <literal>transparent</literal> parameter (1.11.0) allows117outgoing connections to a memcached server originate118from a non-local IP address,119for example, from a real IP address of a client:120<example>121memcached_bind $remote_addr transparent;122</example>123In order for this parameter to work,124it is usually necessary to run nginx worker processes with the125<link doc="../ngx_core_module.xml" id="user">superuser</link> privileges.126On Linux it is not required (1.13.8) as if127the <literal>transparent</literal> parameter is specified, worker processes128inherit the <literal>CAP_NET_RAW</literal> capability from the master process.129It is also necessary to configure kernel routing table130to intercept network traffic from the memcached server.131</para>132133</directive>134135136<directive name="memcached_bind_dynamic">137<syntax><literal>on</literal> | <literal>off</literal></syntax>138<default>off</default>139<context>http</context>140<context>server</context>141<context>location</context>142<appeared-in>1.29.3</appeared-in>143144<para>145When enabled, makes the <link id="memcached_bind">bind</link> operation146at each connection attempt.147</para>148149<para>150<note>151This directive is available as part of our152<commercial_version>commercial subscription</commercial_version>.153</note>154</para>155156</directive>157158159<directive name="memcached_buffer_size">160<syntax><value>size</value></syntax>161<default>4k|8k</default>162<context>http</context>163<context>server</context>164<context>location</context>165166<para>167Sets the <value>size</value> of the buffer used for reading the response168received from the memcached server.169The response is passed to the client synchronously, as soon as it is received.170</para>171172</directive>173174175<directive name="memcached_connect_timeout">176<syntax><value>time</value></syntax>177<default>60s</default>178<context>http</context>179<context>server</context>180<context>location</context>181182<para>183Defines a timeout for establishing a connection with a memcached server.184It should be noted that this timeout cannot usually exceed 75 seconds.185</para>186187</directive>188189190<directive name="memcached_gzip_flag">191<syntax><value>flag</value></syntax>192<default></default>193<context>http</context>194<context>server</context>195<context>location</context>196<appeared-in>1.3.6</appeared-in>197198<para>199Enables the test for the <value>flag</value> presence in the memcached200server response and sets the “<literal>Content-Encoding</literal>”201response header field to “<literal>gzip</literal>”202if the flag is set.203</para>204205</directive>206207208<directive name="memcached_next_upstream">209<syntax>210<literal>error</literal> |211<literal>timeout</literal> |212<literal>denied</literal> |213<literal>invalid_response</literal> |214<literal>not_found</literal> |215<literal>off</literal>216...</syntax>217<default>error timeout</default>218<context>http</context>219<context>server</context>220<context>location</context>221222<para>223Specifies in which cases a request should be passed to the next server:224<list type="tag">225226<tag-name><literal>error</literal></tag-name>227<tag-desc>an error occurred while establishing a connection with the228server, passing a request to it, or reading the response header;</tag-desc>229230<tag-name><literal>timeout</literal></tag-name>231<tag-desc>a timeout has occurred while establishing a connection with the232server, passing a request to it, or reading the response header;</tag-desc>233234<tag-name id="denied"><literal>denied</literal></tag-name>235<tag-desc>the server <link id="memcached_allow_upstream">denied</link>236the connection (1.29.3);237<para>238<note>239This parameter is available as part of our240<commercial_version>commercial subscription</commercial_version>.241</note>242</para>243</tag-desc>244245<tag-name><literal>invalid_response</literal></tag-name>246<tag-desc>a server returned an empty or invalid response;</tag-desc>247248<tag-name><literal>not_found</literal></tag-name>249<tag-desc>a response was not found on the server;</tag-desc>250251<tag-name><literal>off</literal></tag-name>252<tag-desc>disables passing a request to the next server.</tag-desc>253254</list>255</para>256257<para>258One should bear in mind that passing a request to the next server is259only possible if nothing has been sent to a client yet.260That is, if an error or timeout occurs in the middle of the261transferring of a response, fixing this is impossible.262</para>263264<para>265The directive also defines what is considered an266<link doc="ngx_http_upstream_module.xml" id="max_fails">unsuccessful267attempt</link> of communication with a server.268The cases of <literal>error</literal>, <literal>timeout</literal>,269<literal>denied</literal> and270<literal>invalid_response</literal> are always considered unsuccessful attempts,271even if they are not specified in the directive.272The case of <literal>not_found</literal>273is never considered an unsuccessful attempt.274</para>275276<para>277Passing a request to the next server can be limited by278<link id="memcached_next_upstream_tries">the number of tries</link>279and by <link id="memcached_next_upstream_timeout">time</link>.280</para>281282</directive>283284285<directive name="memcached_next_upstream_timeout">286<syntax><value>time</value></syntax>287<default>0</default>288<context>http</context>289<context>server</context>290<context>location</context>291<appeared-in>1.7.5</appeared-in>292293<para>294Limits the time during which a request can be passed to the295<link id="memcached_next_upstream">next server</link>.296The <literal>0</literal> value turns off this limitation.297</para>298299</directive>300301302<directive name="memcached_next_upstream_tries">303<syntax><value>number</value></syntax>304<default>0</default>305<context>http</context>306<context>server</context>307<context>location</context>308<appeared-in>1.7.5</appeared-in>309310<para>311Limits the number of possible tries for passing a request to the312<link id="memcached_next_upstream">next server</link>.313The <literal>0</literal> value turns off this limitation.314</para>315316</directive>317318319<directive name="memcached_pass">320<syntax><value>address</value></syntax>321<default/>322<context>location</context>323<context>if in location</context>324325<para>326Sets the memcached server address.327The address can be specified as a domain name or IP address,328and a port:329<example>330memcached_pass localhost:11211;331</example>332or as a UNIX-domain socket path:333<example>334memcached_pass unix:/tmp/memcached.socket;335</example>336</para>337338<para>339If a domain name resolves to several addresses, all of them will be340used in a round-robin fashion.341In addition, an address can be specified as a342<link doc="ngx_http_upstream_module.xml">server group</link>.343</para>344345</directive>346347348<directive name="memcached_read_timeout">349<syntax><value>time</value></syntax>350<default>60s</default>351<context>http</context>352<context>server</context>353<context>location</context>354355<para>356Defines a timeout for reading a response from the memcached server.357The timeout is set only between two successive read operations,358not for the transmission of the whole response.359If the memcached server does not transmit anything within this time,360the connection is closed.361</para>362363</directive>364365366<directive name="memcached_send_timeout">367<syntax><value>time</value></syntax>368<default>60s</default>369<context>http</context>370<context>server</context>371<context>location</context>372373<para>374Sets a timeout for transmitting a request to the memcached server.375The timeout is set only between two successive write operations,376not for the transmission of the whole request.377If the memcached server does not receive anything within this time,378the connection is closed.379</para>380381</directive>382383384<directive name="memcached_socket_keepalive">385<syntax><literal>on</literal> | <literal>off</literal></syntax>386<default>off</default>387<context>http</context>388<context>server</context>389<context>location</context>390<appeared-in>1.15.6</appeared-in>391392<para>393Configures the “TCP keepalive” behavior394for outgoing connections to a memcached server.395By default, the operating system’s settings are in effect for the socket.396If the directive is set to the value “<literal>on</literal>”, the397<c-def>SO_KEEPALIVE</c-def> socket option is turned on for the socket.398</para>399400</directive>401402</section>403404405<section id="variables" name="Embedded Variables">406407<para>408<list type="tag">409410<tag-name id="var_memcached_key"><var>$memcached_key</var></tag-name>411<tag-desc>412Defines a key for obtaining response from a memcached server.413</tag-desc>414415</list>416</para>417418</section>419420</module>421422423