Several schemes of cache caching for Nginx

Source: Internet
Author: User
Tags file system

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.