Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix Nginx 502 Bad Gateway on large headers?

Tags:

http

nginx

uwsgi

I have a Django REST Framework app running behind an Nginx proxy, we have a third party service that redirects to one of the urls in the app. I'm getting 502s from this endpoint when the redirect happens and have narrowed it down to the Referer header being too large. My logic is as follows:

  • Received 502 when the redirect happens
  • Hitting the link locally with all the query params returns the expected response
  • Adding the Referer header (which is quite large) triggers the 502
  • Removing half of the Referer header returns us to the expected result

I've tried increasing my uwsgi buffer-size and nginx proxy buffer.

like image 623
mattjegan Avatar asked Apr 19 '18 02:04

mattjegan


People also ask

What causes 502 Bad gateway nginx?

In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn't able to get a valid or any response from the upstream server. If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy.


Video Answer


1 Answers

After a number of hours trying out different things, the reason was in fact the uwsgi buffer-size just not being high enough even though I had quadrupled it. For those that don't know, you need to add:

buffer-size=32768

Where the number is some number of bytes that works for your use case. The default is 4096.

like image 71
mattjegan Avatar answered Sep 19 '22 00:09

mattjegan