Nginx缓存@H_403_2@
Nginx有两种缓存机制:fastcgi_cache和proxy_cache@H_403_2@下面我们来说说这两种缓存机制的区别吧@H_403_2@proxy_cache
作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的@H_403_2@fastcgi_cache
作用是缓存fastcgi生成的内容,很多情况是PHP生成的动态内容@H_403_2@proxy_cache
缓存减少了Nginx与后端通信的次数,节省了传输时间和后端带宽@H_403_2@fastcgi_cache
缓存减少了Nginx与PHP的通信次数,更减轻了PHP和数据库的压力。@H_403_2@proxy_cache
缓存设置@H_403_2@ 代码如下:
403_2@#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区@H_403_2@proxy_temp_path /data0/proxy_temp_dir;@H_403_2@#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。@H_403_2@proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;@H_403_2@server@H_403_2@{@H_403_2@listen 80;@H_403_2@server_name www.yourdomain.com 192.168.8.42;@H_403_2@index index.html index.htm;@H_403_2@root /data0/htdocs/www;@H_403_2@location /@H_403_2@{@H_403_2@#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。@H_403_2@proxy_next_upstream http_502 http_504 error timeout invalid_header;@H_403_2@proxy_cache cache_one;@H_403_2@#对不同的HTTP状态码设置不同的缓存时间@H_403_2@proxy_cache_valid 200 304 12h;@H_403_2@#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内@H_403_2@proxy_cache_key $host$uri$is_args$args;@H_403_2@proxy_set_header Host $host;@H_403_2@proxy_set_header X-Forwarded-For $remote_addr;@H_403_2@proxy_pass http://backend_server;@H_403_2@expires 1d;@H_403_2@}@H_403_2@#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。@H_403_2@location ~ /purge(/.*)@H_403_2@{@H_403_2@#设置只允许指定的IP或IP段才可以清除URL缓存。@H_403_2@allow 127.0.0.1;@H_403_2@allow 192.168.0.0/16;@H_403_2@deny all;@H_403_2@proxy_cache_purge cache_one $host$1$is_args$args;@H_403_2@}@H_403_2@#扩展名以.PHP、.jsp、.cgi结尾的动态应用程序不缓存。@H_403_2@location ~ .*\.(PHP|jsp|cgi)?$@H_403_2@{@H_403_2@proxy_set_header Host $host;@H_403_2@proxy_set_header X-Forwarded-For $remote_addr;@H_403_2@proxy_pass http://backend_server;@H_403_2@}@H_403_2@access_log off;@H_403_2@}@H_403_2@}@H_403_2@
@H_403_2@fastcgi_cache缓存设置@H_403_2@ 代码如下:
403_2@#定义缓存存放的文件夹@H_403_2@fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;@H_403_2@#定义缓存不同的url请求@H_403_2@fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";@H_403_2@server {@H_403_2@listen 8080;@H_403_2@server_name www.example .com;@H_403_2@location / {@H_403_2@root /www;@H_403_2@index index.html index.htm index.PHP;@H_403_2@}@H_403_2@location ~ (|.PHP)$ {@H_403_2@root /www;@H_403_2@fastcgi_pass 127.0.0.1:9000;@H_403_2@fastcgi_cache NAME;@H_403_2@fastcgi_cache_valid 200 48h;@H_403_2@fastcgi_cache_min_uses 1;@H_403_2@fastcgi_cache_use_stale error timeout invalid_header http_500;@H_403_2@fastcgi_index index.PHP;@H_403_2@fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;@H_403_2@include fastcgi.conf;@H_403_2@#设置缓存的过程中发现无法获取cookie,经查需要定义这句话@H_403_2@fastcgi_pass_header Set-Cookie;@H_403_2@}@H_403_2@log_format access '$remote_addr - $remote_user [$time_local] "$request" '@H_403_2@'$status $body_bytes_sent "$http_referer" '@H_403_2@'"$http_user_agent" $http_x_forwarded_for';@H_403_2@access_log /httplogs/access.log access;@H_403_2@}@H_403_2@
@H_403_2@总的来说 Nginx的proxy_cache和fastcgi_cache的缓存配置差不多。@H_403_2@--------------------------------------------------------------------------------@H_403_2@403_2@
在讨论memcache缓存之前,我们先了解下MysqL的内存缓存吧@H_403_2@MysqL的内存缓存可以在my.cnf中指定大小:
内存表和临时表不同,临时表也是存放内存中,临时表最大的内存需要通过tmp_table_size=128M设定。当数据查过临时表的最大值设定时,自动转为磁盘表,此时因需要进行IO操作,性能会大大下降,而内存表不会,内存满了后,会提示数据满错误。@H_403_2@例:@H_403_2@ 代码如下:
403_2@create table test@H_403_2@(@H_403_2@id int unsigned not null auto_increment primary key@H_403_2@state char(10),@H_403_2@type char(20),@H_403_2@date char(30)@H_403_2@)engine=memory default charset=utf8@H_403_2@
@H_403_2@