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


使用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. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM