使用Memcache
<br\>
Memcache是一個通用的內存緩存系統。 它通常用於加速緩慢的數據訪問。 NGINXmemcached模塊提供各種指令,可以配置為直接訪問Memcache提供內容,從而避免對上游服務器的請求。
除了指令之外,模塊還創建$ memcached_key變量,用於執行高速緩存查找。 在使用Memcache查找之前,必須在$memcached_key變量中設置一個值,該變量根據請求URL確定。
memcached_pass
<br\>
此指令用於指定memcached服務器的位置。 地址可以通過以下任意方式指定:
•域名或IP地址,以及可選端口
•使用帶unix:前綴的的Unix域套接字
•使用NGINX upstream指令創建的一組服務器
該指令僅在NGINX配置的location和location if中使用。 如下例子:
- location /myloc/{
- set $memached_key $uri;
- memcached_pass localhost:11211;
- }
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區塊下使用。 如下例子:
- http{
- memcached_send_timeout 30s;
- memcached_connect_timeout 30s;
- memcached_read_timeout 30s;
- }
memcached_bind
<br\>
此指令指定服務器的哪個IP與memcached連接,默認為關閉,即不指定,那么Nginx會自動選擇服務器的一個IP用來連接。
完整示例
<br\>
- server{
- location /python/css/ {
- alias "/code/location/css/";
- }
- location /python/ {
- set $memcached_key "$request_method$request_uri";
- charset utf-8;
- memcached_pass 127.0.0.1:11211;
- error_page 404 502 504 = @pythonfallback;
- default_type text/html;
- }
- location @pythonfallback {
- rewrite ^/python/(.*) /$1 break;
- proxy_pass http://127.0.0.1:5000;
- proxy_set_header X-Cache-Key "$request_method$request_uri";
- }
- # Rest NGINX configuration omitted for brevity
- }