對不經常變化的資源文件,如圖片、樣式和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文件的時候,清除緩存文件。