为了账号安全,请及时绑定邮箱和手机立即绑定

nginx Fastcgi缓存配置(19)

标签:
Nginx

获取全套nginx教程,请访问瓦力博客

nginx中的ngx_http_proxy_modulengx_http_fastcgi_module都可以实现反向代理。ngx_http_proxy_module是通用http协议反向代理,ngx_http_fastcgi_module是按
fastcgi接口协议的反向代理。

ngx_http_proxy_module ngx_http_fastcgi_module
proxy_pass fastcgi_pass
proxy_busy_buffers_size fastcgi_busy_buffers_size
proxy_buffer_size fastcgi_buffer_size

1.Fastcgi缓存图解

ssl

2.配置模块

fastcgi缓存配置和proxy_cache语法,配置都差不多。

fastcgi_cache_path

Syntax:	fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default: —
Context: http

fastcgi_cache_key

记录缓存的维度,维度越细,缓存越详细

Syntax:	fastcgi_cache_key string;
Default: —
Context: http, server, location

fastcgi_cache

Syntax:	fastcgi_cache zone | off;
Default: fastcgi_cache off;
Context: http, server, location

fastcgi_cache_valid

Syntax:	fastcgi_cache_valid [code ...] time;
Default: —
Context: http, server, location

3.fastcgi缓存配置

服务目录

/opt/app/code6
|--index.php
|--time.php

/etc/nginx/conf.d
|--fastcgi_cache.conf

index.php

<?php
	echo "瓦力博客";

time.php

<?php
	echo date('Y-m-d H:m:s');

fastcgi_cache.conf

fastcgi_cache_path /opt/app/fastcache levels=1:2 keys_zone=wali:100m max_size=1g inactive=60m;

server {
    listen       80; 
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    
    location / { 
        root /opt/app/code6;
        try_files $uri $uri/ /index.php?$args;
        index  index.php;
    }   

    location ~ \.php$ {
        root /opt/app/code6;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
        fastcgi_cache_key $scheme$request_method$host$request_uri$is_args$args;
        fastcgi_cache wali;
        fastcgi_cache_valid 200 60m;
    
        add_header X-Cache-Source $upstream_cache_status;
    }   
}

检测语法并重启

nginx -tc /etc/nginx/nginx.conf
nginx -s reload -c /etc/nginx/nginx.conf

访问你的域名http://walidream.com/time.php,按f5刷新,打开控制台如果看到X-Cache-Source:HIT就说明配置好了。

ssl

4.后端服务添加no-cache头对于Nginx代理缓存的影响

后端服务添加no-cache头对于Nginx代理缓存的影响

ssl

如果后台服务器在head头信息添加no-chche,那么nginx就不会缓存。如果这种情况下还需要nginx做缓存

fastcgi_ignore_headers Cache-Control Expires Set-Cookie; #忽略头信息

5.设置缓存维度会带来哪些影响

fastcgi_cache_key设置不同维度的key会产生哪些不同的状况

fastcgi_cache_key $scheme$request_method$host$request_uri$is_args$args;
fastcgi_cache_key  $scheme$host;

第一个key比第二个可以设置的详情,第二个key只设置协议+域名。假如访问http://walidream.com/tiem.php 页面被缓存了之后,然后在访问http://walidream.com/index.php的时候
就会发现页面没有刷新,原因就是第二个key设置的维度只有协议和域名导致上面两个链接被认为是同一个缓存,所以访问第二个链接时页面没有被刷新。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消