网站的首页往往是该网站访问量最大的页面,如果首页的业务比较复杂,后台对些页面需要使用较多的系统资源,包括CPU和内存,这就要求我们能够采用一些手段来减轻服务器的资源压力了。
nginx下的conf目录里修改nginx.conf文件如下:
http {
...
proxy_cache_path /tmp/cache keys_zone=tmpcache:200m;
...
}
在应用的路由配置文件里修改如下:
server {
...
location = / {
limit_req zone=allips burst=4;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
proxy_cache tmpcache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 1m; #首页返回200或304时,缓存1分钟
add_header cached $upstream_cache_status;
proxy_pass http://127.0.0.1:3100;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
...
}
打开行之足首页,在控制台查看结果,如图:
HIT 表示命中缓存
EXPIRED 表示缓存过期
MISS 表示没有找到缓存
感谢您的阅读!
如果看完后有任何疑问,欢迎拍砖。
欢迎转载,转载请注明出处:http://www.yangrunwei.com/a/31.html
邮箱:glowrypauky@gmail.com
QQ: 892413924