关于前端:nginx-proxybuffers-缓冲区配置

7次阅读

共计 3590 个字符,预计需要花费 9 分钟才能阅读完成。

对不常常变动的资源文件,如图片、款式和 js 等文件,退出缓存,是优化的一种伎俩。通过 nginx 的 proxy_buffers 可实现缓存性能。

一、测试过的配置

// 先在 http 模块中设置好
proxy_connect_timeout 10;
proxy_read_timeout 180;
proxy_send_timeout 5;
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /data/nginx/cachetemp;
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=480m max_size=1g;

// 而后再匹配动态资源来缓存
location ~ .(jpg|jepg|png|gif|css|js) {
    proxy_pass  http://localhost:8082;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cache cache_one;
    proxy_cache_valid 200 302 24h;
    proxy_cache_valid 301 30d;
    proxy_cache_valid any 5m;
    expires 90d;
    // 用于测试缓存否失效
    add_header Nginx-Cache 1;
}

当重启 nginx 后,发现没失效,那可先测试配置文件是否 ok。命令是:nginx -t -c /etc/nginx/nginx.conf

二、成果

可看到响应头有对应的字段,示意缓存失效了:

查看 cache 目录,也能看到缓存的文件:

三、问题

如果报 Cannot allocate memory,个别是内存设置过大,可把 keys_zone 的值改小点
其余报错,则可依据报错提醒来批改配置文件。比如说 proxy_busy_buffers_size 太小或太大之类。

四、配置详解

proxy_buffering

是否开启缓冲区配置,默认是开启的

proxy_buffer_size

Sets the size of the buffer used for reading the first part of the response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. It can be made smaller, however.
大略意思是这个值是包含头部信息的,不必设置太大。可参考官网默认值 4k|8k

proxy_buffers

Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.
这个可了解为就是缓冲区的大小。指的是一个申请的缓冲区数量和大小。默认是 8 4k|8k。看向缓存什么内容,如果是动态资源,那先看看动态资源的均匀大小。个别比拟大的根本是 30kb,此时可填 4 32k。具体数量和大小,是要依据零碎总内存来设置。number x size 的值不能太大,因为这个是一个申请的缓冲区大小,设置太大了,当并发申请很多的时候,内存回升很快,就会存在问题了。所以官网默认的是 8 4k|8k。

有机会可自行测试下,随着并发申请数量越来越多,零碎内存耗费的状况。

proxy_busy_buffers_size

When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read. In the meantime, the rest of the buffers can be used for reading the response and, if needed, buffering part of the response to a temporary file. By default, size is limited by the size of two buffers set by the proxy_buffer_size and proxy_buffers directives.
nginx 在收到服务器数据后,会调配一部分缓冲区来用于向客户端发送数据。这个缓冲区大小是由 proxy_busy_buffers_size 决定的。大小通常是 proxy_buffers 单位大小的两倍。官网默认是 8k|16k。

proxy_temp_file_write_size

Limits the size of data written to a temporary file at a time, when buffering of responses from the proxied server to temporary files is enabled. By default, size is limited by two buffers set by the proxy_buffer_size and proxy_buffers directives. The maximum size of a temporary file is set by the proxy_max_temp_file_size directive.
当 nginx 收到服务器响应后,把数据一次性写入临时文件的大小。
proxy_max_temp_file_size 则限度了每个申请写入的临时文件的最大值。

proxy_temp_path

Defines a directory for storing temporary files with data received from proxied servers. Up to three-level subdirectory hierarchy can be used underneath the specified directory. For example, in the following configuration.
临时文件的门路

proxy_cache_path

Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key.
缓存门路,其中 keys_zone=name:size 是必填,其余参数都选填。keys_zone 定义缓存的名字和大小。levels 参数限定了缓存的层级。inactive 则相当于定时器,规定工夫内没拜访缓存,则会删除该缓存。max_size 限定了最大缓存的大小,不指定的话,则可能缓存耗尽内存和硬盘。

proxy_cache

Defines a shared memory zone used for caching. The same zone can be used in several places.
下面定义了缓冲区的一系列参数配置,到底用哪个缓冲区,则是由该参数来指定。这个名字就是 keys_zone 定义的 name。

proxy_cache_valid

Sets caching time for different response codes.
设置不同的响应码的缓存工夫。

expires

设置 http 响应头的 Cache-Control 和 Expires。

五、总结

缓存的次要益处是放慢资源的响应,晋升用户体验。但不好的中央,次要是怎么实时更新?

比方批改了 wordpress 博客的 style.css,发现因为有缓存,所以款式基本不失效。
个别公布流程都会带上主动刷新缓存的性能。但自建的 nginx 则没这个性能,目前只能在改了 wordpress 文件后,人工到服务器革除 /data/nginx/cache 目录文件来刷新缓存了。

前面能够写个脚本,在更新 wordpress 文件的时候,革除缓存文件。

正文完
 0