In case this helps anyone, I found the solution to this issue on this stackoverflow question:
This is happening cause the access code in the callback url is too large for the proxy buffers to handle in their default setting, and you just have to increase the buffer size by adding the below lines to Nginx config:
proxy_buffers 8 16k;
proxy_buffer_size 32k;