Nginx做缓存服务器
Nginx做缓存服务器
Nginx配置
1.主配置/etc/nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application /octet-stream ; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; access_log logs /access .log main; include /etc/nginx/conf .d/*.conf; #include proxy.conf; #include upstrem.conf; #include blog.biglittleant.cn.conf; server { listen 80; server_name localhost; error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } } } |
2.proxy配置/etc/nginx/conf.d/proxy.conf
1 2 3 4 5 6 7 8 9 10 | proxy_temp_path /data/cdn_cache/proxy_temp_dir ; proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; |
参数解释:
1 2 3 4 5 6 7 8 9 10 11 | proxy_cache_path: 缓存文件路径 levels: 设置缓存文件目录层次;levels=1:2 表示两级目录 keys_zone: 设置缓存名字和共享内存大小 inactive: 在指定时间内没人访问则被删除 max_size: 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。每一个proxy_cache_path对应一个ngx_http_file_cache_t结构体。 proxy_cache tmp- test : 使用名为tmp- test 的缓存配置 proxy_cache_key $uri :定义缓存唯一key,通过唯一key来进行 hash 存取 proxy_cache_methods :设置缓存哪些HTTP方法 proxy_cache_min_uses :指定请求至少被发送了多少次以上时才缓存,可以防止低频请求被缓存 proxy_cache_bypass :如果指定的任何一个变量值不为空,或者不等于0,nginx就不会查找缓存,直接进行代理转发 proxy_cache_lock /proxy_cache_lock_timeout : 当多个客户端同时请求同一份内容时,如果开启proxy_cache_lock(默认off)则只有一个请求被发送至后端;其他请求将等待该内容返回;当第一个请求返回时,其他请求将从缓存中获取内容返回;当第一个请求超过了proxy_cache_lock_timeout超时时间(默认5s),则其他请求将同时请求到后端来获取响应,且响应不会被缓存;启用proxy_cache_lock可以应对雪崩效应。 |
3.upstream配置/etc/nginx/conf.d/upstream.conf
1 2 3 4 | upstream blog. test .cn { server 47.75.246.12:80 weight=10 max_fails=3; } |
4.blog.test.cn配置/etc/nginx/conf.d/blog.test.cn.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | server { listen 80; server_name blog. test .cn; access_log logs /blog .biglittleant.cn-access.log main; location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf|txt)$ { #Proxy proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; 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_pass http: //blog . test .cn; #Use Proxy Cache proxy_cache cache_one; proxy_cache_key "$host$request_uri" ; add_header Cache "$upstream_cache_status" ; proxy_cache_valid 200 304 301 302 8h; proxy_cache_valid 404 1m; proxy_cache_valid any 2d; } location / { proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; 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_pass http: //blog . test .cn; client_max_body_size 40m; client_body_buffer_size 128k; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; } } |
新建存储目录:
1 | mkdir -p /data/cdn_cache |
注意:启动nginx会多出两个cache的进程。
第一次请求资源会先从源服务下载在nginx上,再返回给客户端。第二次请求相同资源时直接从nginx返回给客户端。
如对您有帮助,支持下呗!
微信

支付宝

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类