nginx利用 proxy_cache 來緩存文件

proxy_cache是nginx自帶的內置緩存模組,配置一下就可以用了,看下面的配置


proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;

# levels設置目錄層次
# keys_zone設置緩存名字和共享記憶體大小
# inactive在指定時間內沒人訪問則被刪除在這裡是1天
# max_size最大緩存空間

範例
  1. user  zhangy users;  
  2. worker_processes 10;  
  3. error_log  //vlogs/nginx_error.log  crit;  
  4. pid        /var/vlogs/nginx.pid;  
  5. #Specifies the value for maximum file descriptors that can be opened by this process.  
  6. worker_rlimit_nofile 65535;  
  7. events  
  8. {  
  9.  use epoll;  
  10.  worker_connections 65535;  
  11. }  
  12. http  
  13. {  
  14.  include       mime.types;  
  15.  default_type  application/octet-stream;  
  16.  #charset  gb2312;  
  17.  server_names_hash_bucket_size 128;  
  18.  client_header_buffer_size 32k;  
  19.  large_client_header_buffers 4 32k;  
  20.  client_max_body_size 8m;  
  21.  sendfile on;  
  22.  tcp_nopush     on;  
  23.  keepalive_timeout 60;  
  24.   
  25.  tcp_nodelay on;  
  26.   
  27.  fastcgi_connect_timeout 300;  
  28.  fastcgi_send_timeout 300;  
  29.  fastcgi_read_timeout 300;  
  30.  fastcgi_buffer_size 64k;  
  31.  fastcgi_buffers 4 64k;  
  32.  fastcgi_busy_buffers_size 128k;  
  33.  fastcgi_temp_file_write_size 128k;  
  34. //============  
  35. client_body_buffer_size  512k;
  36. proxy_read_timeout       60;
  37. proxy_send_timeout       5; 
  38. proxy_buffer_size        16k;
  39. proxy_buffers             464k;
  40. proxy_busy_buffers_size 128k;
  41. proxy_temp_file_write_size 128k;
  42. proxy_temp_path   /usr/local/nginx/proxy_temp;
  43. /*levels設置目錄層次 
  44. keys_zone設置緩存名字和共享記憶體大小 
  45. inactive在指定時間內沒人訪問則被刪除在這裡是1天
  46. max_size最大緩存空間*/
  47. proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m; 
     
  48. //============等號中間要加的,關鍵只要加上proxy_cache_path
  49. gzip on;
  50. gzip_min_length  1k;
  51. gzip_buffers     416k;
  52. gzip_http_version 1.0;
  53. gzip_comp_level 2;
  54. gzip_types       text/plain application/x-javascript text/css application/xml;
  55. gzip_vary on;
  56. upstream myselfxtajmd {
  57. server 127.0.0.1:10002;
  58. server 127.0.0.1:10001 weight=5; 
  59. }  
  60. server
  61. {
  62. listen       10000;
  63. server_name  localhost;
  64. index index.html index.htm index.php; 
  65. log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" '  '"$http_user_agent" $http_x_forwarded_for';
  66. access_log  /var/log/test.log  access;
  67. location /
  68. {
  69. proxy_cache content; //根keys_zone後的內容對應
  70. proxy_cache_valid  200 304 301 302 10d;   //哪些狀態緩存多長時間
  71. proxy_cache_valid  any 1d;    //其他的緩存多長時間
  72. proxy_cache_key $host$uri$is_args $args;   //通過key來hash,定義KEY的值
  73. proxy_pass http://myselfxtajmd;
  74. proxy_redirect                      off;
  75. proxy_set_header   Host             $host;
  76. proxy_set_header   X-Real-IP        $remote_addr;
  77. proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
  78. }
  79. //動態的放過
  80. location ~ .*\.(php|jsp|cgi)?$
  81. {
  82. proxy_set_header Host  $host;
  83. proxy_set_header X-Forwarded-For  $remote_addr;
  84. proxy_pass http://myselfxtajmd;
  85. }
  86. }

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json