Maison > Article > développement back-end > Nginx系列(十四 nginx缓存)
一.代理模块缓存
1.配置文件
<code><span>#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区</span> proxy_temp_path /web/tmp/nginx; <span>#设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为1GB。</span> proxy_cache_path /web/cache/nginx levels=<span>1</span>:<span>2</span> keys_z>100m inactive=<span>1</span>d max_size=<span>1</span>g; </code>
<code>location / { <span>#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。</span> proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; <span>#对不同的HTTP状态码设置不同的缓存时间</span> proxy_cache_valid <span>200</span><span>304</span><span>1</span>h; <span>#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内</span> proxy_cache_key <span>$host</span><span>$uri</span><span>$is_args</span><span>$args</span>; proxy_pass http://webserver; proxy_redirect off; proxy_<span>set</span>_header Host <span>$host</span>; proxy_<span>set</span>_header X-Real-IP <span>$remote_addr</span>; proxy_<span>set</span>_header X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>; }</code>
反向代理的缓存功能是一个重定向,不是根据url生成缓存静态文件的功能
二.fastcgi模块缓存
1.配置文件
<code>fastcgi_temp_path /web/tmp/fastcgi; <span>#设置fastcgi缓存路径 levels代表目录层级,1:2会生成16*256,2:2会生成256*256 keys_zone代表缓冲区名称 inactive代表过期时间 max_size代表最多用多少磁盘空间</span> fastcgi_cache_path /web/cache/fastcgi levels=<span>1</span>:<span>2</span> keys_z>100m inactive=<span>1</span>d max_size=<span>1</span>g; </code>
<code><span>location</span><span>~ [^/]\.php(/|$)</span> { <span>fastcgi_cache</span> cache_two; <span>fastcgi_cache_valid</span><span>200</span><span>10m</span>; <span>fastcgi_cache_methods</span> GET HEAD; <span>#忽视以下头信息</span><span>fastcgi_ignore_headers</span><span>"Cache-Control"</span><span>"Expires"</span><span>"Set-Cookie"</span>; <span>fastcgi_cache_key</span><span>"<span>$scheme</span><span>$request_method</span><span>$host</span><span>$request_uri</span>"</span>; <span>#添加状态头信息</span><span>add_header</span> X-Cache-CFC <span>"<span>$upstream_cache_status</span> - <span>$upstream_response_time</span>"</span>; <span>fastcgi_index</span> index.php; <span>fastcgi_pass</span><span>127.0.0.1:9000</span>; <span>include</span> fastcgi_params; <span>fastcgi_split_path_info</span><span> ^(.+?\.php)(/.*)$</span>; <span>fastcgi_param</span> SCRIPT_FILENAME <span>$document_root</span><span>$fastcgi_script_name</span>; <span>fastcgi_param</span> SCRIPT_NAME <span>$fastcgi_script_name</span>; <span>fastcgi_param</span> PATH_INFO <span>$fastcgi_path_info</span>; <span>fastcgi_param</span> PATH_TRANSLATED <span>$document_root</span><span>$fastcgi_path_info</span>; } </code>
注意:
1.如果fastcgi响应的头信息里有Expires Cache-Control Set-Cookie的时候,fastcgi_cache是不起作用的,所以需要把fastcgi_ignore_headers这个配置项加上。
2.添加头信息X-Cache-CFC 是为了测试缓存是否起作用,$upstream_cache_status包含以下几个状态:
MISS 未命中,请求被传送到后端
HIT 缓存命中
EXPIRED 缓存已经过期请求被传送到后端
UPDATING 正在更新缓存,将使用旧的应答
STALE 后端将得到过期的应答
BYPASS 缓存被绕过了
三.参考
http://www.qttc.net/201307355.html
https://serversforhackers.com/nginx-caching/
http://www.ha97.com/5194.html
http://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter/
以上就介绍了Nginx系列(十四 nginx缓存),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。