13

I'm developing an Express-driven site, that is going through an nginx proxy. Sometimes when loading a page in the browser, I get this:

GET http://myapp.local/css/bootstrap.css net::ERR_CONTENT_LENGTH_MISMATCH

enter image description here

If I refresh the page, it usually goes away. But if refresh over and over and over, it will come up again.

What is the problem here? What can I do to narrow down the issue here? Here is my nginx conf for this server:

server {
  listen 80;
  server_name www.myapp.local;
  rewrite ^(.*) http://myapp.local$1 permanent;
}

server {
  listen 80;
  server_name myapp.local;

  access_log /vagrant/nginx/logs/myapp.local/access.log;
  error_log /vagrant/nginx/logs/myapp.local/error.log;

  location / {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

This is definitely something to do with the nginx proxy. Because if I access the site using just the IP address and Node port: http://10.10.10.10:8080 then I never ever get the error. But if I access it using the proxied vhost: http://myapp.local then I will eventually get the error (maybe 1 out of 10 chance I see it).

1
  • For a start i'd grep the nginx logs (/var/log/nginx/*) for all requests involving bootstrap.css. Also, see this serverfault answer for a better rewrite style using the return keyword (that answer is about https but it will work as well for removing www. from a url_.
    – grochmal
    Jun 19, 2016 at 16:28

4 Answers 4

14

This is a problem with proxy buffering. When buffering is enabled, nginx receives a response from the proxied server as soon as possible, saving it into the buffers set by the proxy_buffer_size and proxy_buffers directives. If the whole response does not fit into memory, a part of it can be saved to a temporary file on the disk. Writing to temporary files is controlled by the proxy_max_temp_file_size and proxy_temp_file_write_size directives.

When buffering is disabled, the response is passed to a client synchronously, immediately as it is received. nginx will not try to read the whole response from the proxied server. The maximum size of the data that nginx can receive from the server at a time is set by the proxy_buffer_size directive.

So you may simply disable the proxy buffering to fix this issue:

proxy_buffering  off;

Also note that nginx is simply trying to write to a temporary file on the disk and if the disk is full you will get the same error. So before disabling proxy_buffering check your disk usage.

4
  • So in a production environment should proxy_buffering be on or off? If a local file changes in the environment how do you "clear the proxy buffering cache" or whatever it is called? Do I have to change the setting off, restart nginx, then change it back and restart it again? Oct 12, 2017 at 14:03
  • 2
    Adding proxy_buffering off; fixed it for me. Dec 23, 2017 at 23:54
  • Note that nginx is simply trying to write to a temporary file on the disk and if the disk is full you will get the same error. So before disabling proxy_buffering check your disk usage. Jan 5, 2018 at 13:30
  • 2
    Warning: Disabling proxy buffering is actually quite risky, not recommended unless you know what exactly you’re doing. Typically, the reverse-proxy and the backend app servers are colocated on a very fast LAN. But the client-side connection quality varies and sometimes stalls. If the proxy’s client-side connection causes back pressure on the proxy’s upstream connection (large assets, or HTTP/2), it can hold an app server hostage as it’s forced to drain the tail end of a response at the client’s slower speed. Source: maxcdn.com/blog/nginx-application-performance-optimization
    – Mouneer
    Jun 25, 2018 at 10:03
11

The net::ERR_CONTENT_LENGTH_MISMATCH is a caching issue. You're telling Nginx to bypass the cache if certain conditions are met (in your case $http_upgrade).

You should've specified the caching location for nginx in a configuration file somewhere. A quick fix will be to delete the contents of this folder, restart nginx, and then try accessing the site again. Another quick fix at the expense of caching is to remove the line proxy_cache_bypass $http_upgrade;

If you provide more details on your caching setup, perhaps this answer could be improved.

3
  • 8
    Interesting thank you. I also found that adding proxy_buffering off seems to work as well. Thanks, Jun 20, 2016 at 16:11
  • 3
    Warning: Disabling proxy buffering is actually quite risky, not recommended unless you know what exactly you’re doing. Typically, the reverse-proxy and the backend app servers are colocated on a very fast LAN. But the client-side connection quality varies and sometimes stalls. If the proxy’s client-side connection causes back pressure on the proxy’s upstream connection (large assets, or HTTP/2), it can hold an app server hostage as it’s forced to drain the tail end of a response at the client’s slower speed. Source: maxcdn.com/blog/nginx-application-performance-optimization
    – Mouneer
    Jun 25, 2018 at 10:02
  • @JakeWilson you saved my day. I faced the same issue and finally you answer worked. Thanks man. Oct 4, 2019 at 10:20
0

When I tried the aforementioned solution it didn't fix the issue. I also changed the permission to write on the location but it didn't work. Then I realized I did something wrong in there. In the location to store the file, I had something like

"/storage" + fileName + ".csv"

. I was testing on the Windows environment and it was working great. But later when we moved the application to the Linux environment it stopped working. So later I had to change it to

"./storage" + fileName + ".csv"

and it started working normally.

0

In case that anybody else is having this issue and the proxy_buffering solution does not work for them. I would first of all want to recommend that in the API call you add "X-Accel-Buffering: no" instead of changing the proxy_buffering:

Buffering can also be enabled or disabled by passing “yes” or “no” in the “X-Accel-Buffering” response header field.

Finally, the issue can also be related to permissions. If you have changed your Nginx user recently, it is possible that the new user does not have access to the proxy_temp folder that Nginx writes to. You might have this folder under "/usr/local/var/run/nginx". You have to change the permissions of this folder to match the user and group that Nginx has.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.