Tri Nguyen Tri Nguyen - 2 months ago 35
Node.js Question

Passport.js express google oauth 502 Bad Gateway on nginx

I am trying to implement a simple Google OAuth for my Express.js app using passport.js following this guide (just replace


When I try it locally, things seem to be working well. When I deploy it to my Ubuntu production server, I get a
502 Bad Gateway
error during the redirect callback from Google to the

passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {

If I comment out the the line
passport.authenticate('google', {..})
, then the error goes away. Upon inspecting nginx error log, I see this error

upstream sent too big header while reading response header from upstream

Here's the server configuration block for nginx:

location /auth/ {

Which means that I would log in to google by going to
, being redirected to
, and then the 502 error happens.

I have tried setting up a similar nginx environment on my OS X development machine, but the problem does not occur there.

I have also tried to add the following to the nginx block configuration, but that doesn't seem to help either

proxy_buffers 8 16k;

I am at my wit's end as to how to debug/ solve this problem. Anyone's suggestion would be greatly appreciated. Here's the link to my project so far


So I was close. proxy_buffers 8 16k; was not sufficient. Adding both of the following lines to nginx fixed it:

proxy_buffers 8 16k;
proxy_buffer_size 32k;