0
0
Nginxdevops~5 mins

Proxy buffering in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes when Nginx acts as a middleman between a user and a server, it needs to handle data smoothly. Proxy buffering helps Nginx collect data from the server before sending it to the user, making the experience faster and more stable.
When your backend server sends data slowly or in small pieces, and you want Nginx to send it smoothly to users.
When you want to reduce the load on your backend by letting Nginx handle bursts of traffic.
When you want to avoid users seeing incomplete or choppy content while waiting for the full response.
When you want to control how much memory Nginx uses to store data temporarily from the backend.
When you want to improve performance by letting Nginx manage data flow between backend and clients.
Config File - nginx.conf
nginx.conf
http {
    server {
        listen 8080;

        location / {
            proxy_pass http://backend-server;
            proxy_buffering on;
            proxy_buffers 8 16k;
            proxy_buffer_size 16k;
        }
    }
}

This configuration sets up a simple Nginx server listening on port 8080.

proxy_pass sends requests to the backend server.

proxy_buffering on; enables buffering of responses from the backend.

proxy_buffers 8 16k; allocates eight buffers of 16 kilobytes each for storing response data.

proxy_buffer_size 16k; sets the size of the buffer used for reading the first part of the response header.

Commands
This command tests the Nginx configuration file for syntax errors before applying changes.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads Nginx to apply the new configuration without stopping the service.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command sends a request to the Nginx server to verify it proxies and buffers responses correctly.
Terminal
curl -i http://localhost:8080/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx/1.24.0 Date: Sat, 01 Jan 2024 12:00:00 GMT Content-Type: text/html Content-Length: 1234 Connection: keep-alive <html>...</html>
Key Concept

If you remember nothing else from this pattern, remember: proxy buffering lets Nginx collect backend data before sending it to users, improving speed and stability.

Common Mistakes
Disabling proxy_buffering when the backend sends slow or chunked responses.
This causes users to receive data in small pieces, leading to slow or choppy loading.
Keep proxy_buffering enabled to let Nginx smooth out the data flow.
Setting proxy_buffers or proxy_buffer_size too small.
Buffers too small can cause frequent disk writes or slow response handling.
Set buffer sizes appropriate to your response size, like 8 buffers of 16k and a buffer size of 16k.
Not testing Nginx configuration before reload.
Syntax errors can cause Nginx to fail to reload, leading to downtime.
Always run 'nginx -t' to check configuration syntax before reloading.
Summary
Enable proxy_buffering in Nginx to let it collect backend responses before sending to clients.
Configure proxy_buffers and proxy_buffer_size to control memory used for buffering.
Test configuration with 'nginx -t' and reload Nginx to apply changes safely.
Use curl or similar tools to verify that Nginx proxies and buffers responses correctly.