But when working on a WordPress plugin that imports data using admin-ajax, I kept getting a curious message in the console no matter how large or small the data was. Specifically, I was getting an error about “502 (Bad Gateway).”
The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.
And if you try to diagnose it based on that definition, you won’t get very far. It’s not that it’s wrong, but it’s that you need to modify your server configuration.
Luckily, it’s easy. Or it’s likely easy.
Valet, WordPress, Ajax, Bad Gateway: The Fix
Before sharing the fix, I’ll say that this sent me down a deep rabbit hole of all kinds of Nginx configurations, reverse proxy information, PHP-FPM, and on and on. If you opt to Google for that, be aware that you’ll likely come across them, too.
And the truth it, maybe that’s what your problem is (I’ll share the links at the bottom of the post), but my particular fix was much easier than that.
Assuming that you’re using macOS, navigate to the .valet directory in your terminal with the following command:
$ cd ~/.valet
Once there, look for the Nginx directory and then enter:
$ ls Nginx
Check to see if you see a file for your specific development server. Note that I use the link command over the park command so I’ll have something like wptrunk.dev. If you don’t have a file there, that’s likely the problem.
And if so, enter the following command:
$ touch Nginx/your-site.dev
Where your-site.dev is whatever the domain that’s causing the problem.
Next, open that particular file in your text editor of choice and add two lines:
fastcgi_buffers 16 16k; fastcgi_buffer_size 32k;
Save the file then restart Valet in the terminal with a simple:
$ valet restart
Then retry the problem that you were experiencing. For me, increasing (or defining) the buffer sizes to the above values worked. You may need larger values so, as they say, YMMV.
The point is, the fix may not be extraordinarily complicated so before you start writing an entire custom configuration file from scratch, try tinkering with these values first.