502 Bad Gateway Error

Hi there,

Have been using Sage for a while but am new to Bedrock/Trellis.

Today I’ve started getting a 502 Bad Gateway Error from Nginx. Have seen a couple of other posts in the forums about this, but the answers don’t mean a lot to me unfortunately.

Have tried rebooting the Vagrant box.

This seems to happen predominantly while editing a contact form in CF7.

Would restarting Nginx be helpful? Any thoughts?

Mike

Restarting nginx couldn’t hurt, but if the 502 is only intermittent, I doubt a restart will solve the issue. Restarting php might be more promising, but vagrant reload should have restarted everything.

vagrant ssh
sudo service nginx reload
sudo service php7.1-fpm reload

The next time you see the bad gateway error you could check nginx logs for recent entries that might shed some light. Related nginx log files are in

  • srv/www/example.com/logs
  • /var/log/nginx

Given that Contact Form 7 may be using ajax, this post about ajax and 502 errors may have potential:

For example, you might override the defaults by adding the following to your group_vars/all/main.yml:

# increasing fastcgi buffers to avoid 502 Bad Gateway with CF7 plugin
nginx_fastcgi_buffers: 8 16k      # default: 8 8k
# nginx_fastcgi_buffer_size: 16k  # default: 8k

I don’t know all the details of tuning fastcgi buffers, but here are a few notes (and related questions).

I’m guessing you’d want to just step up the nginx_fastcgi_buffers each time you notice an apparently related 502 error, and only maybe uncomment the increase/override for nginx_fastcgi_buffer_size as a later stage experiment if 502s persist.

Any time your adjust those values, you’ll need to rerun the nginx role to apply the changes:

# update nginx settings -- DEV
SKIP_GALAXY=true ANSIBLE_TAGS=nginx vagrant provision

# update nginx settings -- STAGING or PRODUCTION
ansible-playbook server.yml -e env=<environment> --tags nginx
13 Likes

Wow, thanks Phil.

I stepped through all those instructions. Appreciate you walking me through those steps.

Looks like increasing the fastcgi buffers did the trick.

After a bit more testing, it appeared I was getting the error only with a large form that I was editing in Contact Form 7. So using this line got it working in the end:

nginx_fastcgi_buffer_size: 16k

You’re a champ! Thanks so much for your help… again! :smile:

Mike

1 Like

This was helpful thank you.