nginx的缓存设置

港控/mmm° 2022-09-20 15:22 280阅读 0赞
  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. proxy_temp_path /home/images_temp;
  22. #gzip on;
  23. proxy_cache_path /home/images_cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  24. upstream chenyilei.com {
  25. server 172.18.84.162:8111 ;
  26. server 172.18.84.162:8222 ;
  27. }
  28. server {
  29. listen 80;
  30. server_name localhost;
  31. #charset koi8-r;
  32. #access_log logs/host.access.log main;
  33. location / {
  34. proxy_cache cache_one;
  35. proxy_connect_timeout 2;
  36. proxy_cache_valid 200 1d;
  37. proxy_cache_key $arg_ckey;
  38. proxy_set_header Host $host;
  39. proxy_set_header X-Real-IP $remote_addr;
  40. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  41. proxy_pass http://chenyilei.com;
  42. proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";#不处理后端服务器返回的指定响应头
  43. expires 1d;
  44. index index.html index.htm;
  45. }
  46. #error_page 404 /404.html;
  47. # redirect server error pages to the static page /50x.html
  48. #
  49. error_page 500 502 503 504 /50x.html;
  50. location = /50x.html {
  51. root html;
  52. }
  53. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  54. #
  55. #location ~ \.php$ {
  56. # proxy_pass http://127.0.0.1;
  57. #}
  58. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  59. #
  60. #location ~ \.php$ {
  61. # root html;
  62. # fastcgi_pass 127.0.0.1:9000;
  63. # fastcgi_index index.php;
  64. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  65. # include fastcgi_params;
  66. #}
  67. # deny access to .htaccess files, if Apache's document root
  68. # concurs with nginx's one
  69. #
  70. #location ~ /\.ht {
  71. # deny all;
  72. #}
  73. }
  74. # another virtual host using mix of IP-, name-, and port-based configuration
  75. #
  76. #server {
  77. # listen 8000;
  78. # listen somename:8080;
  79. # server_name somename alias another.alias;
  80. # location / {
  81. # root html;
  82. # index index.html index.htm;
  83. # }
  84. #}
  85. # HTTPS server
  86. #
  87. #server {
  88. # listen 443 ssl;
  89. # server_name localhost;
  90. # ssl_certificate cert.pem;
  91. # ssl_certificate_key cert.key;
  92. # ssl_session_cache shared:SSL:1m;
  93. # ssl_session_timeout 5m;
  94. # ssl_ciphers HIGH:!aNULL:!MD5;
  95. # ssl_prefer_server_ciphers on;
  96. # location / {
  97. # root html;
  98. # index index.html index.htm;
  99. # }
  100. #}
  101. }

把方法写下来以示备忘或给后来者一个参考:

特别是这个:

(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段里加入下列几句:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. proxy_connect_timeout 5;
  2. proxy_read_timeout 60;
  3. proxy_send_timeout 5;
  4. proxy_buffer_size 16k;
  5. proxy_buffers 4 64k;
  6. proxy_busy_buffers_size 128k;
  7. proxy_temp_file_write_size 128k;
  8. proxy_temp_path /home/temp_dir;
  9. proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30;

接下来在要缓存的service里加入:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. location /gou/detail-id-116 {
  2. ##缓存
  3. index index.html index.htm index.php;
  4. proxy_cache cache_one;
  5. proxy_cache_valid 200 302 30d;
  6. ` proxy_cache_valid 404 10m;`

    ` proxy_cache_valid any 1h;`

  7. proxy_cache_key $host$uri$is_args$args;
  8. proxy_pass http://contactpool;
  9. proxy_ignore_headers “Cache-Control” “Expires” “Set-Cookie”;
  10. #不处理后端服务器返回的指定响应头
  11. expires 30d;
  12. proxy_set_header Host $host;
  13. proxy_set_header X-Real-IP $remote_addr;
  14. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  15. }

当然可以用正则缓存更多的页面如:~.*\.(php|jsp|cgi)?$

上面的我只解释一下地方:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. proxy_ignore_headers

这个表示不处理后端服务器返回的指定响应头,作用就是能够缓存动态页面,比如.php的页面,如果不加这一行就只能缓存静态的页面内容了。

现在:nginx -s reload 后缓存就有了,

接下来如何在需要的时候清理缓存呢,

网上一位大牛分析了nginx是如何存的缓存文件:

计算方法:
1) nginx先把请求地址/1.png用md5进行哈希,得到e0bd86606797639426a92306b1b98ad9
md5的参数就是上面的配置中:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. proxy_cache_key

值,如md5(“www.xxx.com/gou/detail-id-116”);

2) level=1:2就是把最后一位数9拿出来建一个目录,然后再把9前面的2位建一个目录,最后把刚才得到的这个缓存文件放到9/ad目录中。
同样的方法推理,如果level=1:1,那么缓存文件的路径就是/usr/local/nginx/cache/9/d/e0bd86606797639426a92306b1b98ad9

那么我们就可以写一个脚本来清理特定的缓存了:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. #!/usr/bin/env php
  2. <?php
  3. $cache_dir = ‘/usr/local/nginx/cache/‘;
  4. $request_uri = $argv[1];
  5. $url_hash = md5($request_uri);
  6. $dir1 = substr($url_hash,-1,1) . ‘/‘;
  7. $dir2 = substr($url_hash,-3,2) . ‘/‘;
  8. $cached_file = $cache_dir . $dir1 . $dir2 . $url_hash;
  9. if (is_file($cached_file)) {
  10. if (unlink($cache_dir . $dir1 . $dir2 . $url_hash)) {
  11. echo $request_uri . “ 缓存清除成功\n”;
  12. } else {
  13. echo $request_uri . “ 缓存清除失败\n”;
  14. }
  15. } else {
  16. echo $request_uri . “ 未被缓存\n”;
  17. }

    参考http://hermit-macx.iteye.com/blog/1697375

我的完整配置

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx缓存配置

    前言: 什么是Nginx的缓存功能: 概念: Nginx通过配置,可以告知浏览器,返回数据的有效时间,浏览器根据数据的有效时间,确定是否再次到服务器请求。如果没

    相关 Nginx缓存设置

      在开发调试web的时候,经常会碰到因浏览器缓存(cache)而经常要去清空缓存或者强制刷新来测试的烦恼,提供下apache不缓存配置和nginx不缓存配置的设置。在常用的