码迷,mamicode.com
首页 > 系统相关 > 详细

Nginx使用教程(八):使用Nginx缓存之Memcached缓存

时间:2017-01-13 21:58:38      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:cal   响应   har   htm   memcache   www   服务   llb   head   

使用Memcache

<br\>
Memcache是一个通用的内存缓存系统。 它通常用于加速缓慢的数据访问。 NGINXmemcached模块提供各种指令,可以配置为直接访问Memcache提供内容,从而避免对上游服务器的请求。
除了指令之外,模块还创建$ memcached_key变量,用于执行高速缓存查找。 在使用Memcache查找之前,必须在$memcached_key变量中设置一个值,该变量根据请求URL确定。

memcached_pass

<br\>
此指令用于指定memcached服务器的位置。 地址可以通过以下任意方式指定:
?域名或IP地址,以及可选端口
?使用带unix:前缀的的Unix域套接字
?使用NGINX upstream指令创建的一组服务器
该指令仅在NGINX配置的location和location if中使用。 如下例子:

  1. location /myloc/{
  2.    set $memached_key $uri;
  3.    memcached_pass localhost:11211;
  4.    }

memcached_connect_timeout / memcached_ send_timeout / memcached_read_timeout

<br\>
memcached connect_timeout指令设置在NGINX和memcached服务器之间建立连接的超时。
memcached_send_timeout指令设置将请求写入memcached服务器的超时。 memcached_read_timeout指令设置从memcached服务器读取响应的超时。
所有指令的默认值为60秒,可在NGINX配置的http,server和location区块下使用。 如下例子:

  1. http{
  2.    memcached_send_timeout 30s;
  3.    memcached_connect_timeout 30s;
  4.    memcached_read_timeout 30s;
  5.    }

memcached_bind

<br\>
此指令指定服务器的哪个IP与memcached连接,默认为关闭,即不指定,那么Nginx会自动选择服务器的一个IP用来连接。

完整示例

<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使用教程(八):使用Nginx缓存之Memcached缓存

标签:cal   响应   har   htm   memcache   www   服务   llb   head   

原文地址:http://www.cnblogs.com/felixzh/p/6283982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!