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"; 定义缓存唯一key,通过唯一key来进行 hash 存取。 add_header Cache "$upstream_cache_status"; 观察客户端是否命中。 proxy_cache_valid 200 304 301 302 8h; 设置状态码缓存时间8小时。 proxy_cache_valid 404 1m; 设置状态码404缓存时间为1分钟。 proxy_cache_valid any 2d; 除了上行指定的状态码缓存指定了时间,其他保留2天。 } 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返回给客户端。
