# "Random" 502 Errors

**URL:** https://meta.discourse.org/t/random-502-errors/121330
**Category:** Self-hosting
**Created:** [June 25, 2019, 9:55pm UTC](https://meta.discourse.org/t/random-502-errors/121330 "2019-06-25T21:55:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Hewitt](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hewitt/32/142122_2.png) [@Hewitt](https://meta.discourse.org/u/Hewitt)
#### Post date: [June 25, 2019, 9:55pm UTC](https://meta.discourse.org/t/random-502-errors/121330/1 "2019-06-25T21:55:53Z")

</div>

I have a Discourse install running on a GCE server. Users reported having problems with the system randomly returning 502 errors. I can replicate the situation by clicking through the Latest, New, Unread, Top, and Categories links. Sooner or later one of them is going to return a 502 error.

I have checked the logs from my proxy server and it is logging entries like this for the failed urls:  
“upstream prematurely closed connection while reading response header from upstream”. There are a very large quantity of these errors, for seemingly random URLs.

Here are steps I have taken to try to solve the problem based on posts I have seen:

- Upgraded the OS
- Upgraded Docker
- Upgraded Discourse
- Rebooted the server

The original install was done using the Docker Cloud Setup guide. I then followed a guide to switch backups and images to use S3.

My server is running:  
Ubuntu 14.04.6 LTS (GNU/Linux 4.4.0-148-generic x86\_64)

Per discourse-doctor:

```plaintext
     DOCKER VERSION: Docker version 18.06.3-ce, build d7080c1

==================== MEMORY INFORMATION ====================
RAM (MB): 4820

             total used free shared buffers cached
Mem: 4707 2206 2501 140 101 948
-/+ buffers/cache: 1156 3550
Swap: 2047 0 2047

==================== DISK SPACE CHECK ====================
---------- OS Disk Space ----------
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 33G 15G 70% /
/dev/sda1 50G 33G 15G 70% /var/lib/docker

==================== DISK INFORMATION ====================

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000

   Device Boot Start End Blocks Id System
/dev/sda1 * 16065 104856254 52420095 83 Linux
Partition 1 does not start on physical sector boundary.

==================== END DISK INFORMATION ====================

```

I have run top and watched the CPU and Memory numbers and I’m not seeing anything concerning. I’ve looked in the logs and am not seeing anything that points me to the problem.

Any other details I can provide to help troubleshoot the issue? What steps should I take to track this down?

Thank you,

Stephen

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 25, 2019, 9:59pm UTC](https://meta.discourse.org/t/random-502-errors/121330/2 "2019-06-25T21:59:20Z")

</div>

It could be that Postgres needs a bit more memory. You’ve got plenty, so you might bump `db_shared_buffers` to 1024MB. You might also bump `db_work_mem` to 80MB.

---

<div class="post-metadata">

### Author: ![Hewitt](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hewitt/32/142122_2.png) [@Hewitt](https://meta.discourse.org/u/Hewitt)
#### Post date: [June 25, 2019, 11:05pm UTC](https://meta.discourse.org/t/random-502-errors/121330/3 "2019-06-25T23:05:59Z")

</div>

Thank you for the suggestion. I made both of those changes in the yml file. Restarting the app didn’t seem to make a difference, so I ended up rebooting the server. Unfortunately I can still replicate the problem.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 25, 2019, 11:23pm UTC](https://meta.discourse.org/t/random-502-errors/121330/4 "2019-06-25T23:23:44Z")

</div>

You need to rebuild or

```plaintext
cd /var/discourse
./launcher destroy app
./launcher start app

```

for the changes to take effect.

And, this might not be a silver bullet, but I have seen it help.

---

<div class="post-metadata">

### Author: ![Hewitt](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hewitt/32/142122_2.png) [@Hewitt](https://meta.discourse.org/u/Hewitt)
#### Post date: [June 26, 2019, 3:44am UTC](https://meta.discourse.org/t/random-502-errors/121330/5 "2019-06-26T03:44:03Z")

</div>

So far so good, we’ll monitor and see how this helps. Thank you!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [December 15, 2022, 11:21am UTC](https://meta.discourse.org/t/random-502-errors/121330/6 "2022-12-15T11:21:32Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [December 15, 2022, 11:39am UTC](https://meta.discourse.org/t/random-502-errors/121330/7 "2022-12-15T11:39:25Z")

</div>


