nginx的缓存设置
来源:互联网 发布:织梦dedecms5.7 编辑:程序博客网 时间:2024/06/06 06:45
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; proxy_temp_path /home/images_temp; #gzip on; proxy_cache_path /home/images_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; upstream chenyilei.com { server 172.18.84.162:8111 ; server 172.18.84.162:8222 ; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_cache cache_one; proxy_connect_timeout 2; proxy_cache_valid 200 1d; proxy_cache_key $arg_ckey; 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://chenyilei.com; proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";#不处理后端服务器返回的指定响应头 expires 1d; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
把方法写下来以示备忘或给后来者一个参考:
特别是这个:
(1)proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; #不处理后端服务器返回的指定响应头
(2)The cache and other modules which require shared memory support do not work on Windows Vista and later versions due to address space layout randomization being enabled in these Windows versions.
首先用的缓存是proxy_cache.,在http段里加入下列几句:
- 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 /home/temp_dir;
- proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30;
接下来在要缓存的service里加入:
- location /gou/detail-id-116 {
- ##缓存
- index index.html index.htm index.php;
- proxy_cache cache_one;
- proxy_cache_valid 200 302 30d;
proxy_cache_valid 404 10m;
proxy_cache_valid any 1h;
- proxy_cache_key $host$uri$is_args$args;
- proxy_pass http://contactpool;
- proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
- #不处理后端服务器返回的指定响应头
- expires 30d;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
当然可以用正则缓存更多的页面如:~.*\.(php|jsp|cgi)?$
上面的我只解释一下地方:- proxy_ignore_headers
现在:nginx -s reload 后缓存就有了,
接下来如何在需要的时候清理缓存呢,
网上一位大牛分析了nginx是如何存的缓存文件:
计算方法:
1) nginx先把请求地址/1.png用md5进行哈希,得到e0bd86606797639426a92306b1b98ad9
md5的参数就是上面的配置中:
- proxy_cache_key
2) level=1:2就是把最后一位数9拿出来建一个目录,然后再把9前面的2位建一个目录,最后把刚才得到的这个缓存文件放到9/ad目录中。
同样的方法推理,如果level=1:1,那么缓存文件的路径就是/usr/local/nginx/cache/9/d/e0bd86606797639426a92306b1b98ad9
那么我们就可以写一个脚本来清理特定的缓存了:
- #!/usr/bin/env php
- <?php
- $cache_dir = '/usr/local/nginx/cache/';
- $request_uri = $argv[1];
- $url_hash = md5($request_uri);
- $dir1 = substr($url_hash,-1,1) . '/';
- $dir2 = substr($url_hash,-3,2) . '/';
- $cached_file = $cache_dir . $dir1 . $dir2 . $url_hash;
- if (is_file($cached_file)) {
- if (unlink($cache_dir . $dir1 . $dir2 . $url_hash)) {
- echo $request_uri . " 缓存清除成功\n";
- } else {
- echo $request_uri . " 缓存清除失败\n";
- }
- } else {
- echo $request_uri . " 未被缓存\n";
- }
参考http://hermit-macx.iteye.com/blog/1697375
我的完整配置
0 0
- nginx的缓存设置
- nginx缓存功能的设置
- Nginx的浏览器本地缓存设置
- nginx静态资源设置缓存的方法
- Nginx:设置Nginx作为缓存
- nginx的缓存设置 expires缓存提升网站负载
- nginx缓存设置
- nginx缓存设置
- nginx缓存设置
- nginx缓存设置
- nginx 设置缓存
- Nginx HTTP缓存设置
- nginx缓存设置proxy_cache
- nginx---浏览器设置缓存
- Nginx浏览器缓存设置
- nginx学习之nginx的缓存设置 &提高网站性能
- squid 缓存 nginx,gzip设置
- Nginx设置本地浏览器缓存
- hdu 2647(逆序拓扑排序)
- 关于内存对齐的那些事
- 欧几里得
- 孙鑫MFC第十五讲 线程和互斥对象等
- Spring简单入门实例
- nginx的缓存设置
- Valid Parentheses
- 再看 Effective C++ 读书笔记
- 加班能不占吃饭时间吗? 想跳出现在的生活了
- vector的用法!
- 由HTML5绘制地图说开去
- 杭电ACM2003求绝对值&&&&杭电ACM2004成绩转换----20140723
- Light OJ 1266(二维树状数组)
- 开博申明