Nginx 静态化动态网站

Nginx 有几种缓存动态网站,详情请看:nginx缓存cache的5种方案。我的需求是保存动态页面为静态文件即可。然后写个脚本定时删除超过给定时间的html文件。这样可以减轻动态网站的压力。

使用 nginx 的 proxy_store 模块,匹配:

  1. http {  
  2.     server {  
  3.         listen       80;  
  4.         server_name  test.com;  
  5.         access_log  logs/test.access.log  main;  
  6.         gzip on;  
  7.         location / {  
  8.             root   /home/www/cache;  
  9.             proxy_store on;  
  10.             proxy_set_header Accept-Encoding '';  
  11.             proxy_temp_path /home/www/tmp;  
  12.   
  13.             rewrite ^/$ /index.html last;  
  14.             if ( !-f $request_filename ) {  
  15.                 proxy_pass http://127.0.0.1:81$request_uri;  
  16.             }  
  17.         }  
  18.     }  
  19. }  

上面大概意思:判断请求 uri 是否存在 /home/www/cache,没有就是请求 127.0.0.1:81 的,然后保存到 root 下

据说 /home/www/cache 与 /home/www/tmp 目录要在同一目录(我没试)

使用 rewrite ^/$ /index.html last; 的目的是把 / 的请求保存为 index.html,否则警告说不能保存目录(自己还不熟悉 nginx 匹配,这个功能也是试探着使用,误打误撞。)。

网上还有关于 try_files 结合 perl_module 来实现静态化文件,与删除缓存的文件。但试了N次没有成功(总是报错 eval )。只好用 proxy_store,还要写脚本去删除过期文件。

原文地址:https://www.cnblogs.com/rmbteam/p/2208939.html