Fixing Truncated jQuery Responses in Nginx Proxy_pass: Buffer Configuration Guide


4 views

When using Nginx as a reverse proxy for HTTPS applications, you might encounter response truncation issues, particularly with large JavaScript files like jQuery. Here's a deep dive into the configuration that causes this and how to fix it.

The key parameters affecting response size in Nginx proxy configurations are:

proxy_buffering
proxy_buffer_size
proxy_buffers
proxy_busy_buffers_size

In your current configuration, proxy_buffers 8 8k only allocates 64KB total buffer space (8 buffers × 8KB each), which is insufficient for minified jQuery files (typically 80KB+).

Here's an improved version of your location block that handles large responses:

location / {
  proxy_pass https://localhost:8443;
  proxy_http_version 1.1;
  proxy_set_header Connection "";
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  
  # Buffer configuration
  proxy_buffering on;
  proxy_buffer_size 16k;
  proxy_buffers 16 16k;
  proxy_busy_buffers_size 32k;
  
  # Timeout settings
  proxy_read_timeout 300s;
  proxy_connect_timeout 75s;
}

While increasing buffer sizes solves truncation issues, consider:

  • Memory usage (each connection consumes buffer memory)
  • Average response size of your application
  • Concurrent connection count

For streaming applications or when memory is constrained:

location / {
  proxy_pass https://localhost:8443;
  proxy_buffering off;
  proxy_request_buffering off;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
}

Check the Content-Length header in both origin and proxied responses:

curl -I https://localhost:8443/jquery.min.js
curl -I https://my_serv.com/jquery.min.js

Also examine Nginx error logs for buffer-related warnings:

tail -f /var/log/nginx/error.log | grep -i buffer

When using Nginx as a reverse proxy for applications serving large JavaScript files (like jQuery), you might encounter truncated responses. This typically occurs when the response exceeds Nginx's default buffer settings.

The main parameters affecting this behavior in Nginx are:

proxy_buffer_size
proxy_buffers
proxy_busy_buffers_size

Here's an optimized configuration that handles large file responses:

server {
    listen      443;
    server_name my_serv.com;
    ssl                 on;
    ssl_certificate     certificate.pem;
    ssl_certificate_key privatekey.pem;

    keepalive_timeout 70;
    
    location / {
        proxy_pass https://localhost:8443;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        
        # Buffer configuration
        proxy_buffering on;
        proxy_buffer_size 16k;
        proxy_buffers 16 16k;
        proxy_busy_buffers_size 32k;
        
        # Timeout settings
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
    }
}

The configuration increases buffer sizes and enables buffering (which was previously turned off). For jQuery files (~250KB minified), these settings provide adequate buffer space:

  • proxy_buffer_size: Sets the size of the buffer for reading the first part of response
  • proxy_buffers: Number and size of buffers for reading response
  • proxy_busy_buffers_size: Limits the size of buffers that can be busy sending response

For extremely large files (MBs in size), consider using X-Accel-Redirect:

location /large_files/ {
    internal;
    alias /path/to/your/files/;
}

# In your application
response.headers['X-Accel-Redirect'] = '/large_files/jquery.min.js'

After making changes, always test with:

nginx -t
systemctl reload nginx

If issues persist, enable debug logging:

error_log /var/log/nginx/error.log debug;

Check for buffer-related errors in the logs and adjust sizes accordingly.