# 502 Bad Gateway when downloading file

**URL:** <https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859>\
**Category:** NGINX\
**Created:** [October 16, 2011, 1:52am UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859 "2011-10-16T01:52:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 16, 2011, 1:52am UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859/1 "2011-10-16T01:52:13Z")

</div>

Hello all.  
I have topology as follows:  
client-\>NGINX-\>haproxy-\> 4xapache servers.

Problem is, that on one page, we have option to download file - it is  
done through changing header. Resulting header is as follows:  
Content-type: application/txt

Content-Length: “xxxxx”

Content-Disposition: attachment; filename=“aaa.txt”

I’m getting 502 Bad Gateway, The server returned an invalid or  
incomplete response error everytime after clicking on link. In logs,  
there is nothing weird. When trying it directly through HAPROXY or  
directly through nginx (pointed to one of that apache servers) it is  
working correctly.

Do you have any idea about how to resolve it? Have anybody experienced  
something like this?  
Many thanks for any reply concerning this.

Posted at Nginx Forum:

> **[502 Bad Gateway when downloading file](https://forum.nginx.org/read.php?2,216759,216759#msg-216759)**
>
> Hello all. I have topology as follows: client-\>NGINX-\>haproxy-\> 4xapache servers. Problem is, that on one page, we have option to download file - it is done through changing header. Resulting header is as follows: Content-type:...

---

<div class="post-metadata">

**Author:** ![sogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@sogi](https://www.ruby-forum.com/u/sogi)\
**Post date:** [October 16, 2011, 9:35am UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859/2 "2011-10-16T09:35:43Z")

</div>

On Sat, Oct 15, 2011 at 7:51 PM, sogi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all.  
> I have topology as follows:  
> client-\>NGINX-\>haproxy-\> 4xapache servers.
> 
> Problem is, that on one page, we have option to download file - it is  
> done through changing header. Resulting header is as follows:  
> Content-type: application/txt

how do you change header, and can you please post complete headers  
from successful request[s] and from unsuccessful request?

> Content-Length: “xxxxx”
> 
> Content-Disposition: attachment; filename=“aaa.txt”
> 
> I’m getting 502 Bad Gateway, The server returned an invalid or  
> incomplete response error everytime after clicking on link. In logs,  
> there is nothing weird.

please post any relevant log entries

When trying it directly through HAPROXY or

> directly through nginx (pointed to one of that apache servers) it is  
> working correctly.

we need to see the headers

> Do you have any idea about how to resolve it? Have anybody experienced  
> something like this?  
> Many thanks for any reply concerning this.

Stefan C.

---

<div class="post-metadata">

**Author:** ![sogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@sogi](https://www.ruby-forum.com/u/sogi)\
**Post date:** [October 16, 2011, 1:24pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859/3 "2011-10-16T13:24:46Z")

</div>

Hello.  
Many thanks for your reply.  
We are changing headers through PHP header() function.

Header is dispatching correctly from HAPROXY. It is as follows:  
HTTP/1.1 200 OK

Date: Fri, 14 Oct 2011 01:18:08 GMT

Server: Apache/2.2.16 (Debian)

X-Powered-By: PHP/5.2.10

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control: no-store, no-cache, must-revalidate, post-check=0,  
pre-check=0

Pragma: no-cache

Content-Length: 77032;

Content-Disposition: attachment; filename=“aaaa.txt”

Connection: close

Content-Type: application/txt

Logs from nginx debug:  
[http://pastebin.com/UrMXgYyf](http://pastebin.com/UrMXgYyf)  
(I have more - all from logging into system to 502, but there are so  
large actually 🙂 )

Actually, I don’t have any successful header…  
Let me know if you want something, I can debug/prepare it if you need  
more informations.  
Many thanks for all the help.

Posted at Nginx Forum:

> **[Re: 502 Bad Gateway when downloading file](https://forum.nginx.org/read.php?2,216759,216774#msg-216774)**
>
> Hello all. I have topology as follows: client-\>NGINX-\>haproxy-\> 4xapache servers. Problem is, that on one page, we have option to download file - it is done through changing header. Resulting header is as follows: Content-type:...

---

<div class="post-metadata">

**Author:** ![sogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@sogi](https://www.ruby-forum.com/u/sogi)\
**Post date:** [October 16, 2011, 4:15pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859/4 "2011-10-16T16:15:42Z")

</div>

mmm you are the man 🙂  
Well, its confirmed now - it is working when I removed ; from header. I  
missed it before…  
Great - many thanks again - you made my day 🙂

Posted at Nginx Forum:

> **[Re: 502 Bad Gateway when downloading file](https://forum.nginx.org/read.php?2,216759,216779#msg-216779)**
>
> Hello all. I have topology as follows: client-\>NGINX-\>haproxy-\> 4xapache servers. Problem is, that on one page, we have option to download file - it is done through changing header. Resulting header is as follows: Content-type:...

---

<div class="post-metadata">

**Author:** ![sogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@sogi](https://www.ruby-forum.com/u/sogi)\
**Post date:** [October 16, 2011, 2:31pm UTC](https://www.ruby-forum.com/t/502-bad-gateway-when-downloading-file/212859/5 "2011-10-16T14:31:58Z")

</div>

Hello!

On Sun, Oct 16, 2011 at 07:24:34AM -0400, sogi wrote:

> pre-check=0
> 
> Pragma: no-cache
> 
> Content-Length: 77032;

The “;” is incorrect and shouldn’t be here. If it’s indeed  
present in headers returned by your backend - there is no surprise  
haproxy rejects such responses (see below).

> Logs from nginx debug:  
> [http://pastebin.com/UrMXgYyf](http://pastebin.com/UrMXgYyf)  
> (I have more - all from logging into system to 502, but there are so  
> large actually 🙂 )

Debug log shows response from backend was already 502:

2011/10/13 21:47:05 [debug] 15881#0: \*33 http proxy status 502 “502 Bad  
Gateway”

That is, most likely it’s haproxy who returned 502. See above for  
possible reason.

Maxim D.
