Nginx Tutorial (Eight): Using the memcached cache of Nginx cache

Source: Internet
Author: User
Tags memcached

Using Memcache

<br\>
Memcache is a general-purpose memory caching system. It is typically used to accelerate slow data access. The Nginxmemcached module provides a variety of instructions that can be configured to provide content directly to the memcache, thereby avoiding requests to the upstream server.
In addition to the instructions, the module creates a $ memcached_key variable for performing a cache lookup. Before you can use Memcache lookup, you must set a value in the $memcached_key variable that is determined by the request URL.

Memcached_pass

<br\>
This directive is used to specify the location of the memcached server. Addresses can be specified in any of the following ways:
? domain or IP address, and optional port
? using UNIX domain sockets with UNIX: prefixes
? A set of servers created using Nginx upstream directives
This directive is used only in the location and location if of nginx configuration. Here's an example:

    1. location/myloc/{
    2. Set $memached _key $uri;
    3. Memcached_pass localhost:11211;
    4. }
Memcached_connect_timeout/memcached_ Send_timeout/memcached_read_timeout

<br\>
The memcached connect_timeout instruction sets the timeout for establishing a connection between Nginx and memcached servers.
The memcached_send_timeout instruction sets the time-out for writing requests to the memcached server. The memcached_read_timeout instruction sets the timeout for reading the response from the memcached server.
The default value for all instructions is 60 seconds, which can be used under the Http,server and location blocks of nginx configuration. Here's an example:

    1. http{
    2. Memcached_send_timeout 30s;
    3. Memcached_connect_timeout 30s;
    4. Memcached_read_timeout 30s;
    5. }
Memcached_bind

<br\>
This directive specifies which IP of the server is connected to the memcached, the default is off, that is not specified, then nginx automatically chooses one IP for the server to connect to.

Complete example

<br\>

      1. server{
      2.    location/python/css/{
      3.    alias "/code/location/css/";
      4.   }
      5.    location/python/{
      6.    set $memcached _key "$request _method$request_uri";
      7.    charset utf-8;
      8.    memcached_pass 127.0.0.1:11211;
      9.    error_page 404 502 504 = @pythonfallback;
      10.    default_type text/html;
      11.   }
      12.    location @pythonfallback {
      13.    rewrite ^/python/(. *)/$1 break;
      14.  
      15.    proxy_pass http://127.0.0.1:5000;
      16.    proxy_set_header x-cache-key "$request _method$request_uri";
      17.   }
      18.    # Rest NGINX configuration omitted for brevity
      19. }

Nginx Tutorial (eight): Using the memcached cache of Nginx cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.