# Nginx 502 bad gatway error

**URL:** <https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084>\
**Category:** Deployment\
**Created:** [August 22, 2024, 12:48pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084 "2024-08-22T12:48:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RickMA-IT](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rickma-it/32/18840_2.png) [@RickMA-IT](https://forum.djangoproject.com/u/RickMA-IT)\
**Post date:** [August 22, 2024, 12:48pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/1 "2024-08-22T12:48:41Z")

</div>

Hello,

I’m running a Django application on a Ubuntu server with Nginx and Uwsgi. The application has runned for years and since a couple of days I get a 502 bad gateway error on every request.  
Nginx and uwsgi are both active but the nginx/error.log keeps showing :  
`[error] 1269#1269: *72356 upstream prematurely closed connection while reading response header from upstream, client: 50.170.179.62, server: _, `

Can anyone help me out?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 22, 2024, 2:30pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/2 "2024-08-22T14:30:55Z")

</div>

What might have happened in the past couple of days?

- Was the server rebooted?
- Was any software updated?
- Were any configuration changes made?

You mention the nginx error log, what about the uwsgi logs? What about your application logs?

Check your system logs as well, look for errors thrown by whatever you’re using to run uwsgi.

Check the existance of the socket file. Does it exist? Does it have the correct permissions?

---

<div class="post-metadata">

**Author:** ![RickMA-IT](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rickma-it/32/18840_2.png) [@RickMA-IT](https://forum.djangoproject.com/u/RickMA-IT)\
**Post date:** [August 22, 2024, 2:43pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/3 "2024-08-22T14:43:57Z")

</div>

In the first case the server wasn’t rebooted and I got a 502 error 20% off the time, I tried a reboot to solve this problem and after that I’m always getting the 502 error on every request.  
There are no software or configuration changes for the last 8 months.

uwsgi logs: I can only find this file uwsgi-uwsgi.log which is empty.  
The syslogs gives the following: but I don’t know if this is a possible cause of a badgateway:

```auto

Aug 22 14:39:14 clms uwsgi:
Aug 22 14:39:14 clms uwsgi: raise InvalidKeyError()
Aug 22 14:39:14 clms uwsgi: pushbullet.errors
Aug 22 14:39:14 clms uwsgi: .
Aug 22 14:39:14 clms uwsgi: InvalidKeyError
Aug 22 14:39:14 clms uwsgi:

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 22, 2024, 2:47pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/4 "2024-08-22T14:47:34Z")

</div>

Yes, that error would be the cause.

(Python 2.7? Really?)

(Side note: I took the liberty of editing your post to surround the log messages with ``` to make them more readable.)

---

<div class="post-metadata">

**Author:** ![RickMA-IT](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rickma-it/32/18840_2.png) [@RickMA-IT](https://forum.djangoproject.com/u/RickMA-IT)\
**Post date:** [August 22, 2024, 3:03pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/5 "2024-08-22T15:03:03Z")

</div>

Yeah I know, this is the old server of the customer wich is running for a couple of years.  
I’m developing the new one which is up and running but this one is still used.  
So I’am the one to solve this problem all well.

However I tried to solve this error to receive new API key and commented some code of pushbullet as well and added a try catch but I keep getting this error.  
Do you know anything about this?  
This is the file where pushbullet is used

```auto
import django
django.setup()

from django.core import exceptions
from pushbullet import Pushbullet, InvalidKeyError

api_key = 'MyKey'
#pb = Pushbullet(api_key)

try:
    pb = Pushbullet(api_key)
    pb.refresh()
except InvalidKeyError:
    # Log the error or notify admins
    print("Invalid Pushbullet API key. Please check your configuration.")
    # You might want to raise an exception or handle it differently

def push_message(title, body):
 # pb.push_note(title, body)
    return

def push_to_channel(title, body):
    # Get all channels created by the current user
   # my_channel = pb.channels[0]
   # my_channel.push_note(title, body)
    return

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 22, 2024, 4:08pm UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/6 "2024-08-22T16:08:57Z")

</div>

I’m sorry, I don’t know anything at all about pushbullet.

But given the potential age of the system and library that you are likely using, there are any number of possible issues to be addressed.

---

<div class="post-metadata">

**Author:** ![RickMA-IT](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rickma-it/32/18840_2.png) [@RickMA-IT](https://forum.djangoproject.com/u/RickMA-IT)\
**Post date:** [August 23, 2024, 9:50am UTC](https://forum.djangoproject.com/t/nginx-502-bad-gatway-error/34084/7 "2024-08-23T09:50:39Z")

</div>

> [@KenWhitesell](#):
>
> readable

I solved the error of Pushbullet and now it works again, thank you for you help
