# 502 Bad Gateway Error

**URL:** https://discourse.roots.io/t/502-bad-gateway-error/9619
**Category:** trellis
**Created:** 2017-05-16T15:57:32Z
**Posts:** 4
**Showing post:** 2 of 4

## Post 2 by @fullyint — 2017-05-16T19:12:57Z

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:

> [@502-bad-gateway ajax response](https://discourse.roots.io/t/502-bad-gateway-ajax-response/7636/2):
>
> I ended up increasing the fast cgi buffers which resolved the issue.

For example, you might override [the defaults](https://github.com/roots/trellis/blob/560b523222c4b30a08b127d49713b88eaff7ba95/roles/nginx/defaults/main.yml#L6-L7) 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](http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffer_size), but here are [a few notes](https://gist.github.com/magnetikonline/11312172) (and [related questions](https://serverfault.com/questions/88118/any-guide-to-set-values-for-fastcgi-buffers-in-nginx-config)).

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 `502`s persist.

Any time your adjust those values, you’ll need to rerun the [`nginx` role](https://github.com/roots/trellis/blob/560b523222c4b30a08b127d49713b88eaff7ba95/dev.yml#L18) 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
```

---

_[View the full topic](https://discourse.roots.io/t/502-bad-gateway-error/9619)._
