nginx,

Tweaking Nginx proxy_buffers parameters

May 06, 2022 · 2 mins read · Post a comment

Before setting up YOLO values for proxy_buffers and proxy_buffer_size parameters, you need to find out the average and maximum response sizes first. On a side note, the following steps (as shown below) will apply to fastcgi_buffers as well, although these two represent different scenarios.

Generally speaking proxy_buffers are used in proxy (reverse) mode, so Nginx sits in front of apps, web servers, Docker, etcetera. On the other hand, fastcgi_buffers comes into play mostly when serving PHP apps by proxying to php-fpm for instance. Sometimes you may end up using them both.

Prerequisites

  • Nginx
  • sudo privileges

Get average response size

echo $(( `awk '($9 ~ /200/)' /var/log/nginx/access.log | awk '{print $10}' | awk '{s+=$1} END {print s}'` / `awk '($9 ~ /200/)' /var/log/nginx/access.log  | wc -l` ))

Average response size in bytes for devcoops.com:

40429

Get maximum response size

cat /var/log/nginx/access.log | grep "200" | awk '{print $10}' | sort -nr | head -n 1

Maximum response size in bytes for devcoops.com:

1040722

Optimization example

The default values for proxy_buffers are 8 4k / 8k (depending on the platform = one memory page), and 4k / 8k for proxy_buffers_size.

Step 1. Get the current proxy_buffers and proxy_buffer_size parameter values determined by the default memory page size.

getconf PAGESIZE

Output:

4096

Step 2. Since the average response rate is 40kB, and the maximum response rate is around 1MB, 4k won’t do enough. So, the optimal proxy_buffer values would be:

proxy_buffers 16 64k;
proxy_buffer_size 64k;

Note(s):

  • 16 x 64k = 1MB, as we want to cover the maximum response rate at least. There is no one size fit all solution, since there are certain trade-offs between having a larger number with smaller buffer size and the other way round. Usually, it comes down to how much memory is available on the server and how much is allocated. What you could do is test with different combos of number and sizes.
  • 64k and 64kB are the same thing.

Step 3. Reload Nginx.

sudo systemctl reload nginx

Conclusion

Inspired by Tweaking fastcgi-buffers.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.