zoukankan      html  css  js  c++  java
  • nginx proxy_buffers 缓冲区配置

    对不经常变化的资源文件,如图片、样式和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文件的时候,清除缓存文件。

  • 相关阅读:
    ecshop /includes/lib_base.php、/includes/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php Backdoor Vul
    ecshop /pick_out.php SQL Injection Vul By Local Variable Overriding
    dedecms /include/filter.inc.php Local Variable Overriding
    dedecms plusguestbook.php SQL Injection Vul By plusguestbookedit.inc.php
    帝国备份王(Empirebak) classfunctions.php、classcombakfun.php GETSHELL vul
    dedecms /member/uploads_edit.php SQL Injection Vul
    PHP Lex Engine Sourcecode Analysis(undone)
    dedecms /member/resetpassword.php SQL Injection Vul
    dedecms /member/reg_new.php SQL Injection Vul
    dedecms /member/pm.php SQL Injection Vul
  • 原文地址:https://www.cnblogs.com/telwanggs/p/15113741.html
Copyright © 2011-2022 走看看