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二、効果
応答ヘッダには、キャッシュが有効になったことを示すフィールドが表示されます.nginx proxy_buffers 缓冲区配置_第1张图片 cacheディレクトリを表示します.キャッシュされたファイルも表示されます.nginx proxy_buffers 缓冲区配置_第2张图片
三、問題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.この値にはヘッダ情報が含まれており、あまり大きく設定する必要はありません.公式サイトのデフォルト値4 k|8 kを参照可能
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 4 k|8 kです.キャッシュの内容を見て、静的リソースであれば、静的リソースの平均サイズを見てみましょう.一般的に大きいのは基本的に30 kbで、この場合は4 32 k記入できます.具体的な数とサイズは、システムの総メモリに基づいて設定します.number x sizeの値はあまり大きくできません.これはリクエストのバッファサイズなので、設定が大きすぎて、同時リクエストが多い場合、メモリの上昇が速く、問題があります.だから公式サイトのデフォルトは8 4 k|8 kです.
自分でテストできる機会があれば、コンカレントリクエストの数が増えるにつれて、システムメモリが消費される場合があります.
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単位サイズの2倍.公式サイトのデフォルトは8 k|16 kです.
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ファイルを更新するときにキャッシュファイルをクリアします.