Nginx 预配置缓存

<!-- wp:shortcode -->
#nginx设定获取url中请求包含nc参数的进行预先配置缓存,开发人员根据需求自定义缓存级别,缓存配置为nginx和CDN两层生效。
#接口缓存 
 location ~ /loadSeat.jsonp {
	# access_log /opt/logs/nginx/ticket.acc1.log;
	# error_log /opt/logs/nginx/ticket.err1.log ;

		add_header  Web-Tag "/loadSeat";		
		#添加nc参数,用于缓冲ajax数据
		add_header  arg_nc "$arg_nc";
		add_header  arg_type "$arg_type";
		if ( $arg_type = "ck" ){                              #不做缓存
			#access_log /opt/logs/nginx/ticket.acc2.log;
                        proxy_pass  http://ticket;
			break;
                }
		if ( $arg_type != "ck" ){					#默认15s,默认规则要写在最上面
			proxy_pass  http://127.0.0.1:90;
		}
		if ( $arg_nc = 30 ){					#30S
			proxy_pass  http://127.0.0.1:81;
		}
		if ( $arg_nc = 60 ){					#1m
			proxy_pass  http://127.0.0.1:82;
		}
		if ( $arg_nc = 180 ){					#3m
			proxy_pass  http://127.0.0.1:83;
		}
		if ( $arg_nc = 300 ){					#5m
			proxy_pass  http://127.0.0.1:84;
		}
		if ( $arg_nc = 600 ){					#10m
			proxy_pass  http://127.0.0.1:85;
		}
	#	proxy_pass http://ticket;
    }

[root@WEB conf.d]# cat nc.conf 
server  {										#30s
	listen       81;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 30s;
		add_header  Web-Tag "81";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 30s;
	}
}

server  {										#1m
	listen       82;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 60s;
		add_header  Web-Tag "82";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 1m;
	}
}

server  {										#3m
	listen       83;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 180s;
		add_header  Web-Tag "83";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 3m;
	}
}

server  {										#5m
	listen       84;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 300s;
		add_header  Web-Tag "84";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 5m;
	}
}

server  {										#10m
	listen       85;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 600s;
		add_header  Web-Tag "85";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 10m;
	}
}
server  {										#15s
	listen       90;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 15s;
		add_header  Web-Tag "90";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 15s;
	}
}
<!-- /wp:shortcode -->

<!-- wp:shortcode -->
#nginx设定获取url中请求包含nc参数的进行预先配置缓存,开发人员根据需求自定义缓存级别,缓存配置为nginx和CDN两层生效。
#接口缓存 
 location ~ /loadSeat.jsonp {
	# access_log /opt/logs/nginx/ticket.acc1.log;
	# error_log /opt/logs/nginx/ticket.err1.log ;

		add_header  Web-Tag "/loadSeat";		
		#添加nc参数,用于缓冲ajax数据
		add_header  arg_nc "$arg_nc";
		add_header  arg_type "$arg_type";
		if ( $arg_type = "ck" ){                              #不做缓存
			#access_log /opt/logs/nginx/ticket.acc2.log;
                        proxy_pass  http://ticket;
			break;
                }
		if ( $arg_type != "ck" ){					#默认15s,默认规则要写在最上面
			proxy_pass  http://127.0.0.1:90;
		}
		if ( $arg_nc = 30 ){					#30S
			proxy_pass  http://127.0.0.1:81;
		}
		if ( $arg_nc = 60 ){					#1m
			proxy_pass  http://127.0.0.1:82;
		}
		if ( $arg_nc = 180 ){					#3m
			proxy_pass  http://127.0.0.1:83;
		}
		if ( $arg_nc = 300 ){					#5m
			proxy_pass  http://127.0.0.1:84;
		}
		if ( $arg_nc = 600 ){					#10m
			proxy_pass  http://127.0.0.1:85;
		}
	#	proxy_pass http://ticket;
    }

[root@WEB conf.d]# cat nc.conf 
server  {										#30s
	listen       81;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 30s;
		add_header  Web-Tag "81";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 30s;
	}
}

server  {										#1m
	listen       82;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 60s;
		add_header  Web-Tag "82";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 1m;
	}
}

server  {										#3m
	listen       83;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 180s;
		add_header  Web-Tag "83";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 3m;
	}
}

server  {										#5m
	listen       84;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 300s;
		add_header  Web-Tag "84";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 5m;
	}
}

server  {										#10m
	listen       85;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 600s;
		add_header  Web-Tag "85";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 10m;
	}
}
server  {										#15s
	listen       90;
	server_name  127.0.0.1;
	location /  {
		proxy_pass  http://ticket;
		proxy_cache cache_one;
		proxy_cache_valid 200 302 15s;
		add_header  Web-Tag "90";	
		add_header  Web-Cache "$upstream_cache_status";
		expires 15s;
	}
}
<!-- /wp:shortcode -->

 

<think>我们正在处理关于如何清除Nginx缓存的问题。根据用户的问题,我们需要提供清除Nginx缓存的方法。 根据引用[1]的内容,我们可以使用一个shell脚本来清除Nginx缓存。该脚本首先删除Nginx缓存目录下的所有内容,然后重新加载Nginx配置。 另外,引用[3]提到,有时可能需要更细粒度的缓存控制,比如使用X-Accel-Expires头,但用户的问题是关于清除整个缓存,所以我们主要关注如何清除整个缓存目录。 步骤: 1. 确定Nginx缓存目录的位置(通常配置在nginx.conf中,例如/var/cache/nginx)。 2. 删除该目录下的所有文件(注意:需要sudo权限)。 3. 重新加载Nginx以使更改生效。 我们将提供一个示例脚本,并解释每一步的作用。 同时,我们也要注意,清除缓存可能需要根据不同的Nginx配置进行调整。 另外,引用[2]和[4]提到了其他配置(如HTTPS、HTTP头等),但用户的问题是关于清除缓存,所以我们不展开这些内容。 根据引用[5],我们还应该注意安全配置,但同样,这不是当前问题的重点。 因此,我们提供以下方法: 方法1:手动清除缓存 步骤: 1. 删除缓存目录:sudo rm -rf /var/cache/nginx/* 2. 重新加载Nginx:sudo nginx -s reload 或者使用系统服务命令(如sudo systemctl reload nginx) 方法2:使用脚本(如引用[1]所示) 我们将编写一个脚本,并说明如何设置定时任务(如果用户需要定时清除缓存的话)。 注意:在执行删除操作前,请确认缓存目录是否正确,以免误删其他文件。 下面我们提供详细步骤。</think>### 清除Nginx缓存的方法 清除Nginx缓存主要有两种方式,具体操作取决于您的缓存配置方式: #### 1. **手动清除文件缓存(推荐)** ```bash # 删除缓存目录所有内容(需sudo权限) sudo rm -rf /var/cache/nginx/* # 重新加载Nginx使配置生效 sudo systemctl reload nginx # 或 sudo nginx -s reload ``` **说明**: - 缓存路径 `/var/cache/nginx` 需与 `nginx.conf` 中 `proxy_cache_path` 配置一致 - 操作后立即生效,适用于所有缓存配置类型[^1] #### 2. **通过Purge模块清除(需安装ngx_cache_purge)** ```nginx location ~ /purge(/.*) { proxy_cache_purge YOUR_CACHE_NAME $1$is_args$args; } ``` **操作方式**: ```bash curl -X PURGE http://yourdomain.com/purge/url-to-purge ``` **要求**: - 需编译安装 `ngx_cache_purge` 模块 - 需在配置中预定义缓存区域名称[^3] --- ### 配置建议 在 `nginx.conf` 中添加基础缓存配置: ```nginx proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m; server { location / { proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; # 添加清除指令(可选) proxy_cache_purge PURGE from $arg_purge_token; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值