28

I have 2 RoR web applications hosted on 2 different servers. For one particular page, the request is served from the second application. For rest of the pages, the request is served from the main application. Nginx settings for the main application

location /customer/help/ {
            proxy_pass http://second-application:3020/help_and_support/;
}
location /assets/ {
            proxy_pass http://second-application:3020/assets/;
}

This worked fine until yesterday. Now, /customer/help/ page is not loading properly. In firefox it shows a blank page, in chrome, it loads partially and console shows an error

net::ERR_INCOMPLETE_CHUNKED_ENCODING

After debugging I found that issue might be with image data sent over API. My second app calls an API to get images and displays them on page

<% url_with_binary_data = "data:image/" + "jpeg" + ";base64," + u.photo_url.to_s %>
<%= image_tag(url_with_binary_data, :class => "userpic")  %>

API code to get the image

photo_url: Base64.encode64(u.photo.file.read).gsub("\n", '')
MervS
  • 5,724
  • 3
  • 23
  • 37
pramodtech
  • 6,300
  • 18
  • 72
  • 111

12 Answers12

50

You might want to check if the user that is running the Nginx worker owns the directory /var/lib/nginx (or /var/cache/nginx in some distros).

I've learned that when you give a response too big for Nginx, it uses this directory to write as a working directory for temporary files. If the worker process cannot access it, Nginx will terminate the transmission before it completes, thus the error INCOMPLETE_CHUNKED_ENCODING.

DfKimera
  • 2,076
  • 2
  • 21
  • 39
  • I am getting same error. If issue related to nginx then how we can fix it? – kd12 Dec 01 '15 at 06:10
  • If you navigate to `/var/lib/nginx` and run `ls -lah`, what's the output? – DfKimera Dec 02 '15 at 13:48
  • 2
    You never knew how this saved me! – Sri Harsha Kappala Jul 04 '16 at 07:27
  • 3
    Check which user nginx is running (generally in nginx.conf), then you'll want to give ownership of the nginx lib directory to that user `chown -R nginx:nginx /var/lib/nginx/`. You can reload nginx (`service nginx reload` on CentOS 6) just in case. – Don Wilson Sep 14 '16 at 19:51
  • This solved my problem too, while proxying Kibana 5.4 via nginx. – Ajay M Nov 04 '17 at 14:10
  • thanks man, exactly what I needed :) in my case the user was different, on dev server it was fine, but once tested on production... your solution helped – Tom St Feb 05 '19 at 00:39
  • Should I create this directory if it doesn't exist? – Dexter Adams Apr 23 '19 at 19:37
  • God bless you my friend. Thanks a lot. – deadpool Oct 23 '19 at 12:25
  • I was running into an issue with a `strapi` + nginx install. When accessing the admin page, I would get an ECONNRESET error. `chown`ing the directory for nginx was the fix. Thanks so much! – Jack Apr 19 '20 at 01:12
  • This is useful answer, however I think we should add the answer here for future reference 1. Make sure nginx user can write to `/var/lib/nginx` (this directory must exist) 2. Make sure nginx user can write to `/var/cache/nginx` (this directory must exist) – ytdm Jun 30 '20 at 17:09
  • After hitting this error, I noticed I was getting it only any page that had a lot of data -- either a list of about 1500 names, or even a drop-down box of that size. I tried bigger buffers in nginx, and tried no buffers. Neither made a difference. What finally '"fixed" it was to use the Pagy gem, in the case of the index lists, to show just 500 at a time. And on the drop-down boxes, have just 50 names, but include a search box, so the user doesn't have to scroll a large list of names. – Norm Apr 05 '22 at 07:49
  • This is the best solution I have ever seen after 2 day googling over web. Thanks @DfKimera. God bless you. – ganji Apr 20 '22 at 05:06
26

Bumped into this issue on AWS and found that adding a few proxy_buffer directives to the site config file fixed the issues:

server {
    ...

    location / {
        ...
        proxy_buffers 8 1024k;  
        proxy_buffer_size 1024k;
    }
}
Ethan
  • 341
  • 4
  • 6
6

For me, the solution was what DfKimer recommended, but instead of /var/lib/nginx it was /var/cache/nginx.

shloosh
  • 525
  • 7
  • 10
1

For me the solution was enable proxy_max_temp_file_size

bes
  • 71
  • 1
  • 8
1

proxy_set_header Connection keep-alive;


Full config

server {
        listen      0000; #//port give by your need
        server_name  aa.com;
        proxy_buffers 16 4k;
        proxy_buffer_size 2k;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location ~ ^/hello/{
            proxy_buffering off;
            proxy_pass http://127.0.0.1:1111; #//port give by your need
            proxy_redirect     off;
            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_set_header Connection keep-alive; 
        }
Sheridan
  • 615
  • 1
  • 8
  • 21
Chiranjeeb
  • 211
  • 2
  • 5
1

I got this error due to server memory full. hope this helps any one in future.

Jomy Joseph
  • 321
  • 1
  • 8
  • Same for me on my Kibana server, a 'df -h' shows that the server was full, nginx was unable to cache new files in /var/cache/nginx, preventing Kibana to load (Kibana did not load properly). Making some space resolved the issue. – Xavier FRANCOIS Oct 06 '20 at 06:55
1

100% Working Solution for php & nginx web server

Issue => net::ERR_INCOMPLETE_CHUNKED_ENCODING nginx

Step1 : Open /etc/php/7.2/fpm/pool.d [Select your php folder in my case i am using php 7.2]

Step2 : Edit www.conf file inside pool.d folder

In my case it's look like this =>

[inet]
user = www-data
group = www-data

listen = 127.0.0.1:9999
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 5

pm.status_path = /status
ping.path = /ping

request_terminate_timeout = 10s
request_slowlog_timeout = 10s

;
; Log files
;
access.log = /var/log/php-fpm/php-fpm.log
slowlog = /var/log/php-fpm/slow.log

Step3 : Change the value of request_terminate_timeout = 10s (Whatever time you want)

request_terminate_timeout = 300s

Step4 : Now Save and restart php-fpm (in my case i am using php7.2 so cmd will be)

sudo service php7.2-fpm restart

Now you can execute your script it will works and will terminate after 300s

Now one more thing add one more line of syntax fastcgi_read_timeout 300; inside nginx.conf file or your website .conf file [Here is code ]

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http { 
include /etc/nginx/conf-enabled/*.conf;
include /etc/nginx/sites-enabled/*.conf;    
}

After adding fastcgi_read_timeout 300; it will look like this

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http { 
include /etc/nginx/conf-enabled/*.conf;
include /etc/nginx/sites-enabled/*.conf;
fastcgi_read_timeout 300; 
}

Now reload nginx and restart php-fpm by following cmd

sudo service php7.2-fpm reload
sudo service nginx reload

Note : The code snippet is run and tested bye me please let me know if you are not able to fix your issue via my answer

Shailesh Dwivedi
  • 657
  • 7
  • 15
0

If proxied server cannot write to /var/lib/nginx, yo do not need to play with file permissions or ownership of that directory. You can change the cache directory of nginx for the context by;

proxy_temp_path /home/emre/projects/frontend/nginx_temp 1 2;

inside http, server or location contexts of nginx.conf file.

check http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_temp_path

Emre Tapcı
  • 1,743
  • 17
  • 16
0

For me the solution was to disable the cache. For context my setup is for local development and nginx is being used as reverse proxy to map domains basically

location / {
  proxy_pass http://localhost:3000;

  # don't cache it
  proxy_no_cache 1;
  # even if cached, don't try to use it
  proxy_cache_bypass 1;
}
0

This error is by nginx trying to use cache folder without right access:

  1. Make sure nginx user can write to /var/lib/nginx (or /var/cache/nginx in some distros).
  2. Make sure nginx user can write to the folder (find the nginx user form nginx configuration file is located usually in /etc/nginx/nginx.conf)
  3. Give the right access (chown -R nginx:nginx /var/lib/nginx/)
  4. Reload the service(-service nginx reload -in centos)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
ganji
  • 752
  • 7
  • 17
0

To add another solution to this thread. I was getting the error (failed)net::ERR_INCOMPLETE_CHUNKED_ENCODING from Nginx after using SSE but it was working fine if I called the server directly. The reason was that Nginx was having a read timeout for 60 seconds. It can be overridden by using the proxy_read_timeout directive like the following to make it for 24 hours as an example:

proxy_read_timeout 24h;
Mina Tadros
  • 514
  • 6
  • 18
0

The main cause of the issues was due to timedout issues. So the solution which works for me was to increase the value of the following nginx block as per your need in seconds.

http {
  ...
  send_timeout 200; # 200 seconds
  ...
}
Tashi Dendup
  • 342
  • 3
  • 13