memcached + Nginx

Source: Internet
Author: User
Tags memcached

I. Preface

Nginx Memcached_module module can read the content directly from the memcached server output, subsequent requests are no longer processed by the application, such as PHP-FPM, Django, greatly improve the speed of dynamic pages. Nginx is only responsible for reading data from the memcached server, to write data to memcached also need the background of the application to complete, the active cache of the page cache to memcached, can be redirected to the backend to deal with 404.
Ngx_http_memcached_module can operate any software that concurrently uses the Memcached protocol. such as Ttserver, Membase and so on.

The structure diagram is as follows:

memcached keys can be set by Memcached_key variables, such as $uri. If hit, then the direct output of the content, no hit means that Nginx needs to request from the application page. At the same time, we also want the application to write key-value pairs to memcached so that the next request can be obtained directly from memcached.
If the key value does not exist, Nginx will report a not found error. The best approach is to use Error_page to specify and location request processing. Also includes "Bad Gateway" error and "Gateway Timeout" error, such as: Error_page 404 502 504 = @app;.
Note: You need to set default_type, otherwise it may not appear normal.

2. Module instruction Description:

Memcached_bind
Syntax: Memcached_bind Address | Off
Default value: None
Configuration segment: HTTP, server, location
Specify from which IP to connect to the memcached server

Memcached_buffer_size
Syntax: Memcached_buffer_size size;
Default value: 4k|8k;
Configuration segment: HTTP, server, location
Reads the buffer size of the response received from the memcached server. Synchronize the response to the client as soon as possible.

Memcached_connect_timeout
Syntax: Memcached_connect_timeout time;
Default value: 60s;
Configuration segment: HTTP, server, location
The time-out period for establishing a connection to the memcached server. Usually not more than 75s.

Memcached_gzip_flag
Syntax: Memcached_gzip_flag flag;
Default value: None
Configuration segment: HTTP, server, location
Test the memcached server response flag. If set, Content-encoding:gzip is added to the response header.

Memcached_next_upstream
Syntax: Memcached_next_upstream Error | Timeout | Invalid_response | Not_found | Off ...;
Default value: Error timeout;
Configuration segment: HTTP, server, location
Specifies the states in which the request will be forwarded to another load Balancer server, only if Memcached_pass has two or more than two.

Memcached_pass
Syntax: Memcached_pass address:port or socket;
Default value: None
Configuration segment: location, if in location
Specifies the memcached server address. Use the variable $memcached_key to query the value for key and return error_page 404 if there is no corresponding value.

Memcached_read_timeout
Syntax: Memcached_read_timeout time;
Default value: 60s;
Configuration segment: HTTP, server, location
Defines the read response time-out from the memcached server.

Memcached_send_timeout
Syntax: memcached_send_timeout
Default value: 60s
Configuration segment: HTTP, server, location
Sets the time-out for sending requests to the memcached server.

$memcached _key Variables:
The value of the Memcached key.

3. The enhanced version of Nginx memcached Ngx_http_enhanced_memcached_module

Based on the Nginx memcached module, the new features include:
1. Custom HTTP headers, such as Content-type, last-modified.
2. Hash key can be more than 250 characters, memcached is limited.
3. Store data to memcached via HTTP requests.
4. Delete data from memcached via HTTP request.
5. Clears all memcached cache data through HTTP requests.
6. Get memcached state data via HTTP request.
7. Key name space management, to partially flush the cache.
8. The cache responds to 304Not Modified requests via If-modified-since header and content last-modified.

4. Application examples

Nginx Configuration Example:

Upstream memcacheds {server 10.1.240.166:22222;}        server {listen 8080;        server_name nm.ttlsa.com;        Index index.html index.htm index.php;        Root/data/wwwroot/test.ttlsa.com/webroot;                location/images/{set $memcached _key $request _uri;                Add_header X-mem-key $memcached _key;                Memcached_pass memcacheds;                Default_type text/html;        Error_page 404 502 504 = @app;        } location @app {rewrite ^/.*/nm_ttlsa.php?key= $request _uri;                } location ~. *\.php?$ {include fastcgi_params;                Fastcgi_pass 127.0.0.1:10081;                Fastcgi_index index.php;                Fastcgi_connect_timeout 60;                Fastcgi_send_timeout 180;                Fastcgi_read_timeout 180;                Fastcgi_buffer_size 128k;                Fastcgi_buffers 4 256k;        Fastcgi_busy_buffers_size 256k;        Fastcgi_temp_file_write_size 256k;                Fastcgi_intercept_errors on;        Fastcgi_param script_filename $document _root$fastcgi_script_name; }}

nm_ttlsa.php instances:

Addservers ($servers); $r = $m->set ($_get[' key '), $data); Header (' Content-length: '. FileSize ($FN). " \ r \ n "); header (' Content-type:image/gif '. \ r \ n "); header (' X-cache:miss '. \ r \ n ");p rint $data;} Else{header (' location:http://www.ttlsa.com '. \ r \ n ");}

5. Testing

First visit: (Requires PHP processing)

Visit again: (read directly from memcached)

Ha, this example is not good.
1. The earth people know that memcached is not persistent, if it is a permanent picture application, the choice can be durable storage solutions suitable, such as Riak, Membase, Ttserver, MongoDB Gridfs and so on.
2. If it is the application of the user's avatar, it is not appropriate to use memcached to do the cache. Because the user changes the Avatar and has to refresh the cache, in view of this, one step with ttserver or MongoDB Gridfs to do the user avatar storage is not better.
Ttserver+nginx Building High-concurrency high-availability applications See also: http://www.ttlsa.com/html/1429.html
This example can be used to migrate images online to key-value storage transition scenarios.

Nginx's Memc-nginx and Srcache-nginx modules can be actively added to the memcached cache. Follow up and publish it later. See memc_nginx+srcache_nginx+memcached Building transparent dynamic page caching http://www.ttlsa.com/html/2460.html

memcached + Nginx

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.