深入Nginx + PHP 缓存详解

栏目: PHP · 发布时间: 7年前

内容简介:以下是对Nginx中的PHP缓存进行了详细的分析介绍,需要的朋友可以参考下Nginx缓存nginx有两种缓存机制:fastcgi_cache和proxy_cache

以下是对Nginx中的 PHP 缓存进行了详细的分析介绍,需要的朋友可以参考下

Nginx缓存

nginx有两种缓存机制:fastcgi_cache和proxy_cache

下面我们来说说这两种缓存机制的区别吧

proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的

fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容

proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽

fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。

proxy_cache缓存设置

复制代码 代码如下:

#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

proxy_temp_path /data0/proxy_temp_dir;

#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

server

{

listen 80;

server_name  www.yourdomain.com 192.168.8.42;

index index.html index.htm;

root /data0/htdocs/www;

location /

{

#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。

proxy_next_upstream http_502 http_504 error timeout invalid_header;

proxy_cache cache_one;

#对不同的HTTP状态码设置不同的缓存时间

proxy_cache_valid 200 304 12h;

#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内

proxy_cache_key  host host uri isargs is_args args;

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://backend_server;

expires 1d;

}

#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。

location ~ /purge(/.*)

{

#设置只允许指定的IP或IP段才可以清除URL缓存。

allow 127.0.0.1;

allow 192.168.0.0/16;

deny all;

proxy_cache_purge cache_one $host 1 1 is_args KaTeX parse error: Expected 'EOF', got '}' at position 7: args; }? #扩展名以.php、.jsp…

{

proxy_set_header Host $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://backend_server;

}

access_log off;

}

}

fastcgi_cache缓存设置

复制代码 代码如下:

#定义缓存存放的文件夹

fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;

#定义缓存不同的url请求

fastcgi_cache_key " scheme scheme request_method host host uri argfilename arg_filename arg_x KaTeX parse error: Expected '}', got 'EOF' at end of input: …ation ~ (|.php) {

root /www;

fastcgi_pass 127.0.0.1:9000;

fastcgi_cache NAME;

fastcgi_cache_valid 200 48h;

fastcgi_cache_min_uses 1;

http://www.iis7.com/a/lm/ftp/

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Understanding Machine Learning

Understanding Machine Learning

Shai Shalev-Shwartz、Shai Ben-David / Cambridge University Press / 2014 / USD 48.51

Machine learning is one of the fastest growing areas of computer science, with far-reaching applications. The aim of this textbook is to introduce machine learning, and the algorithmic paradigms it of......一起来看看 《Understanding Machine Learning》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具