Nginx做缓存服务器
Nginx配置
1.主配置/etc/nginx/nginx.conf
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
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;
参数解释:
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
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
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)${#Proxyproxy_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 Cacheproxy_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;}}
新建存储目录:
mkdir -p /data/cdn_cache
注意:启动nginx会多出两个cache的进程。
第一次请求资源会先从源服务下载在nginx上,再返回给客户端。第二次请求相同资源时直接从nginx返回给客户端。
本文链接:https://www.cnblogs.com/Dev0ps/p/11753234.html
