Seemingly never written nginx caching function, are only to see not to share, this is bad habit ah.
1, one of the traditional cache (404)
This approach is to nginx 404 errors to the back end, and then use Proxy_store to save the back-end page.
Configuration:
location / {
root /home/html/;#主目录
expires 1d;#网页的过期时间
error_page 404 =200 /fetch$request_uri;#404定向到/fetch目录下
}
location /fetch/ {#404定向到这里
internal;#指明这个目录不能在外部直接访问到
expires 1d;#网页的过期时间
alias /home/html/;#虚拟目录文件系统地址要和locaion /一致,proxy_store会将文件保存到这目录下
proxy_pass http://www.sudone.com/;#后端upstream地址,/fetch同时是一个代理
proxy_set_header Accept-Encoding '';#让后端不要返回压缩(gzip或deflate)的内容,保存压缩后的内容会引发乱子。
proxy_store on;#指定nginx将代理返回的文件保存
proxy_temp_path /home/tmp;#临时目录,这个目录要和/home/html在同一个硬盘分区内
}
Use the time also have to note is nginx to have permissions to/home/tmp and/home/html under the permission to write files, under Linux Nginx will generally be configured to nobody users to run, so that these two directories will be Chown nobody, Set to nobody user-specific, of course, can also chmod 777, but all experienced system administrators will recommend not to use 777 casually.
2. Traditional cache bis (!-E)
Principle and 404 jump basically consistent, but more concise some:
location / {
root /home/html/;
proxy_store on;
proxy_set_header Accept-Encoding '';
proxy_temp_path /home/tmp;
if ( !-f $request_filename )
{
proxy_pass http://www.sudone.com/;
}
}
You can see that this configuration than 404 saved a lot of code, it is used to determine the requested file!-f in the file system does not exist, does not exist on the proxy_pass to the back end, return is also used Proxy_store save.