
正文
nginxphp存数据 nginx缓存接口数据
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php怎么读取设置nginx缓存
nginx缓存
nginx有两种缓存机制:fastcgi_cache和proxy_cache
下面nginxphp存数据我们来说说这两种缓存机制nginxphp存数据的区别吧
proxy_cache作用是缓存后端服务器的内容nginxphp存数据,可能是任何内容,包括静态的和动态的
fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容
proxy_cache缓存减少nginxphp存数据了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 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$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass ;
expires 1d;
}
#用于清除缓存,假设一个URL为,通过访问就可以清除该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$is_args$args;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass ;
}
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$request_method$host$uri$arg_filename$arg_x$arg_y";
server {
listen 8080;
server_name .com;
location / {
root /www;
index index.html index.htm index.php;
}
location ~ (|.php)$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache NAME;
fastcgi_cache_valid 200 48h;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
#设置缓存的过程中发现无法获取cookie,经查需要定义这句话
fastcgi_pass_header Set-Cookie;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /httplogs/access.log access;
}
总的来说 nginx的proxy_cache和fastcgi_cache的缓存配置差不多。
memcache缓存
在讨论memcache缓存之前,我们先了解下mysql的内存缓存吧
mysql的内存缓存可以在my.cnf中指定大小:内存表和临时表不同,临时表也是存放内存中,临时表最大的内存需要通过tmp_table_size=128M设定。当数据查过临时表的最大值设定时,自动转为磁盘表,此时因需要进行IO操作,性能会大大下降,而内存表不会,内存满了后,会提示数据满错误。
例:
create table test
(
id int unsigned not null auto_increment primary key
state char(10),
type char(20),
date char(30)
)engine=memory default charset=utf8
内存表的特性:
1.内存表的表定义存放在磁盘上,扩展名为.frm,所以重启不会丢失
2.内存表的数据是存放在内存中,重启会丢失数据
3.内存表使用一个固定的长度格式
4.内存表不支持blob或text列,比如varchar与text字段就不会被支持
5.内存表支持auto_increment列和对可包含null值的列的索引
6.内存表不支持事物
7.内存表是表锁,当修改频繁时,性能可能会下降
转自:
下面我们来看看memcache,相对而言mysql的内存表限制较多。
memcache的用途
1.提高系统的并发能力
2.减轻数据库的负担
注:memcache linux系统32位只支持4G内存,同时memcache最长保存时间为30天。
相关问答
Q1: 深入Nginx + PHP 缓存详解
以下是对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 /data /proxy_temp_dir; #设置Web缓存区名称为cache_one 内存缓存空间大小为 MB 天没有被访问的内容自动清除 硬盘缓存空间大小为 GB proxy_cache_path /data /proxy_cache_dir levels= : keys_zone=cache_one: m inactive= d max_size= g; server { listen ; server_name yourdomain ; index index index ; root /data /htdocs/; location / { #如果后端的服务器返回 执行超时等错误 自动将请求转发到upstream负载均衡池中的另一台服务器 实现故障转移 proxy_next_upstream _ _ error timeout invalid_header; proxy_cache cache_one; #对不同的HTTP状态码设置不同的缓存时间 proxy_cache_valid h; #以域名 URI 参数组合成Web缓存的Key值 Nginx根据Key值哈希 存储缓存内容到二级缓存目录内 proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X Forwarded For $remote_addr; proxy_pass //backend_server; expires d; } #用于清除缓存 假设一个URL为 通过访问就可以清除该URL的缓存 location ~ /purge(/ *) { #设置只允许指定的IP或IP段才可以清除URL缓存 allow ; allow / ; deny all; proxy_cache_purge cache_one $host$ $is_args$args; } #扩展名以 php jsp cgi结尾的动态应用程序不缓存 location ~ * (php|jsp|cgi)?$ { proxy_set_header Host $host; proxy_set_header X Forwarded For $remote_addr; proxy_pass //backend_server; } access_log off; } }
fastcgi_cache缓存设置
复制代码 代码如下: #定义缓存存放的文件夹 fastcgi_cache_path /tt/cache levels= : keys_zone=NAME: m inactive= d max_size= G; #定义缓存不同的url请求 fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y"; server { listen ; server_name example ; location / { root /; index index index index php; } location ~ (| php)$ { root /; fastcgi_pass : ; fastcgi_cache NAME; fastcgi_cache_valid h; fastcgi_cache_min_uses ; fastcgi_cache_use_stale error timeout invalid_header _ ; fastcgi_index index php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi conf; #设置缓存的过程中发现无法获取cookie 经查需要定义这句话 fastcgi_pass_header Set Cookie; } log_format access $remote_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$_referer" "$_user_agent" $_x_forwarded_for ; access_log / }
总的来说 nginx的proxy_cache和fastcgi_cache的缓存配置差不多 memcache缓存 在讨论memcache缓存之前 我们先了解下mysql的内存缓存吧 mysql的内存缓存可以在my cnf中指定大小 内存表和临时表不同 临时表也是存放内存中 临时表最大的内存需要通过tmp_table_size= M设定 当数据查过临时表的最大值设定时 自动转为磁盘表 此时因需要进行IO操作 性能会大大下降 而内存表不会 内存满了后 会提示数据满错误 例
复制代码 代码如下: create table test ( id int unsigned not null auto_increment primary key state char( ) type char( ) date char( ) )engine=memory default charset=utf lishixinzhi/Article/program/PHP/201311/21248
Q2: php和nginx之间是如何工作的
Nginx+php-fpm实现原理 Nginx本身不会对PHP进行解析nginxphp存数据,终端对PHP页面的请求将会被Nginx交给FastCGI进程监听的IP地址及端口nginxphp存数据,由php-fpm作为动态解析服务器处理,最后将处理结果再返回给nginx。其实,Nginx就是一个反向代理服务器。Nginx通过反向代理功能将动态请求转向后端php-fpm,从而实现对PHP的解析支持,这就是Nginx实现PHP动态解析的原理。 Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。
当nginx接收到一个http请求时,通过配置文件找到对应的server。然后匹配server中的所有location,找到最匹配的。而在location中的命令会启动不同的模块去完成工作,比如rewrite模块、index模块。因此在nginx中模块可以看作真正的劳动工作者。nginx的模块是被编译到nginx中的,属于静态方式。启动nginx时,模块被自动加载。
Q3: 在Nginx和php-fpm的环境中有办法立即输出缓存区内容的吗
nginx缓存分两类,一类是自己缓存一些文件。包括图片和解析后的PHP等. 也可以控制客户端缓存的内容和时间. 其次就是做代理,缓存图片之类的东西。做前端. memcache缓存的是数据库信息。第一次读了数据库,直接显示。同时存储memcache.下次读。直接去memcache里面读就可以
Q4: 502 Bad Gateway
502badgateway要先找到nginx配置的路径。
然后找到nginx所在的error日志文件来查看具体原因。
如果是客户端浏览器配置的问题,以360浏览器为例,出现502BadGateway可能是设置了代代理导致的。
取消浏览器代理之后,刷新一下就可以访问了。
502BadGateway是一种报错提示,这一错误并不意味着上游服务器已关闭(无响应网关/代理),而是上游服务器和网关/代理不同意的协议交换数据。
鉴于互联网协议是相当清楚的,它往往意味着一个或两个机器已不正确或不完全编程。
Q5: php如何更新nginx静态缓存
这里说下Memcachednginxphp存数据的例子nginxphp存数据:
复制代码 代码如下:
?php
$memcache = new Memcache;
$memcache-connect('localhost', 11211) or die ("Could not connect");
$version = $memcache-getVersion();
echo "Server's version: ".$version."\n";
$tmp_object = new stdClass;
$tmp_object-str_attr = 'test';
$tmp_object-int_attr = 123;
$memcache-set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)\n";
$get_result = $memcache-get('key');
echo "Data from the cache:\n";
var_dump($get_result);
读库的例子:
复制代码 代码如下:
?php
$sql = 'SELECT * FROM users';
$key = md5($sql); //memcached 对象标识符
if ( !($datas = $mc-get($key)) ) {
// 在 memcached 中未获取到缓存数据nginxphp存数据,则使用数据库查询获取记录集。
echo "n".str_pad('Read datas from MySQL.', 60, '_')."n";
$conn = mysql_connect('localhost', 'test', 'test');
mysql_select_db('test');
$result = mysql_query($sql);
while ($row = mysql_fetch_object($result))
$datas[] = $row;
// 将数据库中获取到的结果集数据保存到 memcached 中nginxphp存数据,以供下次访问时使用。
$mc-add($key, $datas);
} else {
echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
}
var_dump($datas);
nginxphp存数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于nginx缓存接口数据、nginxphp存数据的信息别忘了在本站进行查找喔。







