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

ngx_http_proxy_modulengx_http_fastcgi_module
proxy_passfastcgi_pass
proxy_busy_buffers_sizefastcgi_busy_buffers_size
proxy_buffer_sizefastcgi_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设置的维度只有协议和域名导致上面两个链接被认为是同一个缓存,所以访问第二个链接时页面没有被刷新。

nginx教程

nginx环境搭建(1) nginx基础知识(2) nginx的安装 卸载(3) nginx的基本参数使用(4) nginx分析默认配置(5) nginx 虚拟主机配置(6) nginx 日志(7) nginx 模块(8) nginx 访问控制(9) nginx 静态资源web服务(10) nginx 缓存(11) nginx 跨域访问(12) nginx 防盗链(13) nginx 正向,反向代理配置(14) nginx 代理缓存配置(15) nginx websocket(16) nginx fastcgi(17) nginx 搭建wordPress博客(18) nginx Fastcgi缓存配置(19) nginx uwsgi反向代理(20) nginx 负载均衡(21) [深] nginx 动静分离(22) [深] nginx rewrite规则(23) [深] nginx 平滑升级 添加模块 调试(24) [深] nginx secure_link_module模块(25) [深] nginx geoip_module模块(26) [深] nginx https(27) [深] nginx与lua的开发(28) [架] nginx常见问题(29) [架] nginx性能优化(30) [架] nginx 安全(31) [架] nginx 反向代理gRpc(32)