2.8 使用memcached

nginx可以读取直接来自memcached的整个页面。所以,如果你的W eb应用程序能够在memcached中存储整个页面,nginx就能从memcached读取该页面。示例配置应该如下:

[...]

location ~ \.php$ {

set $no_cache "";

if ($query_string ~ ".+") {

set $no_cache "1";

}

if ($request_method !~ ^(GET|HEAD)$ ) {

set $no_cache "1";

}

if ($request_uri ~ "nocache") {

set $no_cache "1";

}

if ($no_cache = "1") {

return 405;

}

set $memcached_key $host$request_uri;

memcached_pass 127.0.0.1:11211;

default_type text/html;

error_page 404 405 502 = @php;

expires epoch;

}

location @php {

try_files $uri =404;

include /etc/nginx/fastcgi_params;

fastcgi_pass unix:/var/lib/php5-fpm/web1.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_script_name;

fastcgi_intercept_errors on;

}

[...]

重要的是,你的Web应用程序在memcached中存储页面所使用的密钥与nginx从memcached中读取这些页面所使用的密码是同一把(本例中是$host$request_uri),不过这不管用。

如果你在memcached中存储了许多数据,就要确保你已为memcached分配了足够的内容,比如:

vi /etc/memcached.conf

[...]

# 开始内存限额为64MB。这很合理。

# 注意:守护程序会增加到这个大小,但是不会一开始就占用这么大的内存。

-m 512

[...]


相关内容