proxy_cache是nginx自带的内置缓存模块,配置一下就可以用了,看下面的配置
vi /usr/local/nginx/conf/nginx.conf
- user zhangy users;
- worker_processes 10;
- error_log /var/vlogs/nginx_error.log crit;
- pid /var/vlogs/nginx.pid;
- #Specifies the value for maximum file descriptors that can be opened by this process.
- worker_rlimit_nofile 65535;
- events
- {
- use epoll;
- worker_connections 65535;
- }
- http
- {
- include mime.types;
- default_type application/octet-stream;
- #charset gb2312;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- //============
- client_body_buffer_size 512k;
- 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_temp_path /usr/local/nginx/proxy_temp;
- /*levels设置目录层次
- keys_zone设置缓存名字和共享内存大小
- inactive在指定时间内没人访问则被删除在这里是1天
- max_size最大缓存空间*/
- proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;
- //============等号中间要加的,关键只要加上proxy_cache_path
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- upstream myselfxtajmd {
- server 127.0.0.1:10002;
- server 127.0.0.1:10001 weight=5;
- }
- server
- {
- listen 10000;
- server_name localhost;
- index index.html index.htm index.php;
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /var/log/test.log access;
- location /
- {
- proxy_cache content; //根keys_zone后的内容对应
- proxy_cache_valid 200 304 301 302 10d; //哪些状态缓存多长时间
- proxy_cache_valid any 1d; //其他的缓存多长时间
- proxy_cache_key $host$uri$is_args$args; //通过key来hash,定义KEY的值
- proxy_pass http://myselfxtajmd;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- //动态的放过
- location ~ .*\.(php|jsp|cgi)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_pass http://myselfxtajmd;
- }
- }
- }
上面只是配置的部分内容,反向代理的那部分没有放进去了,参考linux下nginx反向代理,实现负载均衡,当我们浏览http://localhost:10000/222.jpg时,在代理端就把图片缓存了,不用到代理终端去缓存了,可以节省资源。缓存的内容放在你设置的proxy_cache_path路径下面,看下图
第一层目录只有一个字符,是由levels=1:2设置,总共二层目录,子目录名字由二个字符组成。突然发现,我在我的系统里面,配置过varnish,squid,apache和mod_cache,nginx和proxy_cache,他们都是可以对文件进行缓存,不知道他们哪一个对文件的缓存效果最好呢?有空比较一下,对了这4种缓存,这个博客里面都有的,有兴趣的朋友,可以看一下
转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/apachenginx/1018.html
学习了。
能定义那些key缓存多少事件么