http {

    #1,配置虚拟主机充当静态服务器角色,并不允许客户端缓存
    server {
        listen 8080;
        server_name localhost;
        location  /nginx/nocache/ {
            alias /nginx/data/www/nocache/;
            add_header Cache-Control no-store;
            add_header Pragma no-cache;
            add_header Expires 0;
            autoindex off;
        }
    }
		

    #2,配置缓存
    proxy_cache_path /nginx/cache levels=1:2 keys_zone=nginx_data_cache:500m inactive=1d max_size=30g use_temp_path=off;
    server{
    	 listen 80;
    	 server_name localhost;
    	 access_log logs/localhost_access.log;
    	 
    	 location /up/ {
    	 			#设置缓存名称(同上keys_zone配置)		 
    	 			proxy_cache nginx_data_cache;
    	 			#设置要缓存的HTTP方法(默认是GET HEAD)						
    	 			proxy_cache_methods GET HEAD;
    	 			#要被缓存的资源的最少使用次数(默认为1)
    	 			proxy_cache_min_uses 1;
    	 			#设置200等响应缓存1小时(优先级最低,低于后端服务响应头中的缓存参数,如:max-age)
    	 			proxy_cache_valid 200 302 301 1h;
    	 			#设置其它缓存10分钟
    	 			proxy_cache_valid any 10m;
								
    	 			#设置缓存的KEY值(默认值:$scheme$proxy_host$request_uri)
    	 			proxy_cache_key $scheme$proxy_host$uri$is_args$args;
    	 			#如果缓存过期则向后端服务发起revalidate请求(此时客户端收到的可能是304而非200)
    	 			proxy_cache_revalidate on;
						
    	 			#是否将客户端的请求头部转发给后端服务器(默认是 on)
    	 			proxy_pass_request_headers on;
    	 			#是否向后端服务器发送HTTP包体部分(默认是 on)
    	 			proxy_pass_request_body on;
    	 			#确定在使用代理服务器时是否可以重用SSL会话(默认是 on)
    	 			proxy_ssl_session_reuse on;

    	 			#开启容错能力,即使用缓存内容来响应客户端的请求
    	 			#updating:当有请求正在更新本次请求的缓存文件时,本次请求直接响应过期的缓存给客户端
    	 			proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            
    	 			#设置向后端服务传递的头信息
    	 			proxy_set_header Host $host;
    	 			proxy_set_header X-Real-IP $remote_addr;
    	 			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	 			#proxy_set_header If-None-Match $http_if_none_match;

    	 			#相同的请求只有第一个请求向后端服务器进行转发
    	 			proxy_cache_lock on;
    	 			proxy_cache_lock_timeout 10s;
    	 			#向客户端显示nginx缓存的命中状态
    	 			add_header X-Cache-Status $upstream_cache_status;
    	 			
    	 			proxy_pass_header  Set-Cookie;
    	 			proxy_pass http://localhost:8081/;         
        }
    }
}
说明:nginx参数

url格式:

<scheme>://<user>:<password>@<host>:<port>/<path>;<param>?<query>#<frag>

假设浏览器访问地址为:http://localhost/up/index.html?a=1 对于nginx的参数来说

参数|值|备注 -|- $request_uri | /up/index.html?a=1|浏览器也是使用该值进行缓存 $uri | /up/index.html|不含参数部分 $is_args|?|如果没有则为空字符串 $args|a=1|如果没有则为“-”

更多参数说明请参考:http://nginx.org/en/docs/http/ngx_http_core_module.html#variables 其它参考链接:https://www.nginx.com/blog/nginx-caching-guide/


关于nginx缓存请求头的透传

假设nginx配置如下

    location /up/ {
            proxy_cache nginx_data_cache;
            proxy_cache_methods GET HEAD;
            proxy_cache_key $scheme$proxy_host$uri$is_args$args;
            proxy_cache_revalidate on;
            proxy_cache_lock on;
            
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          
            add_header X-Cache-Status $upstream_cache_status;
            proxy_pass http://localhost:8080/;         
    }

浏览器端发起的请求头为

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost
If-None-Match: dsawewfdsdsdsddddddddds
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36

后端服务器打印出来的请求头为:

host=localhost
x-real-ip=127.0.0.1
x-forwarded-for=127.0.0.1
connection=close
cache-control=max-age=0
upgrade-insecure-requests=1
user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
sec-fetch-site=none
sec-fetch-mode=navigate
sec-fetch-user=?1
sec-fetch-dest=document
accept-encoding=gzip, deflate, br
accept-language=zh-CN,zh;q=0.9

其中的If-None-Match并未透传给后端服务器。