Nginx 502 gateway error with AWS RDS Aurora, Django, and Daphne

I am trying to run an open source cloud project described here. I want to use an AWS RDS Aurora instance, instead of the project's default database, so I followed their instructions to do that here.

However, when I reload the server:

bash ~/cloud_station_deployment/reload_server.sh

and go to my website's URL, I get a 502 Bad Gateway error with Nginx:

502 gateway error

How should I solve this error?


This is what I tried to find the cause of the problem:

sudo systemctl status nginx

output:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-09-30 19:32:21 UTC; 2h 23min ago
     Docs: man:nginx(8)
 Main PID: 2532 (nginx)
    Tasks: 2 (limit: 1140)
   CGroup: /system.slice/nginx.service
           ├─2532 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─3174 nginx: worker process

Sep 30 19:32:20 ip-172-31-83-16 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Sep 30 19:32:20 ip-172-31-83-16 systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 30 19:32:21 ip-172-31-83-16 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Sep 30 19:32:21 ip-172-31-83-16 systemd[1]: Started A high performance web server and a reverse proxy server.
Sep 30 19:32:36 ip-172-31-83-16 systemd[1]: Reloading A high performance web server and a reverse proxy server.
Sep 30 19:32:36 ip-172-31-83-16 systemd[1]: Reloaded A high performance web server and a reverse proxy server.

sudo systemctl status daphne

output:

● daphne.service - CloudStation Daphne
   Loaded: loaded (/etc/systemd/system/daphne.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2022-09-30 19:55:27 UTC; 24s ago
  Process: 3712 ExecStart=/home/ubuntu/ENV/bin/daphne -b 0.0.0.0 -p 8001 webgms.asgi:application (code=exited, status=1/FAILURE)
 Main PID: 3712 (code=exited, status=1/FAILURE)

Sep 30 19:55:26 ip-172-31-83-16 systemd[1]: daphne.service: Main process exited, code=exited, status=1/FAILURE
Sep 30 19:55:26 ip-172-31-83-16 systemd[1]: daphne.service: Failed with result 'exit-code'.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: daphne.service: Service hold-off time over, scheduling restart.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: daphne.service: Scheduled restart job, restart counter is at 5.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: Stopped CloudStation Daphne.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: daphne.service: Start request repeated too quickly.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: daphne.service: Failed with result 'exit-code'.
Sep 30 19:55:27 ip-172-31-83-16 systemd[1]: Failed to start CloudStation Daphne.

Then I tried looking at the Nginx error log in ~/logs/nginx-error.log:

cat ~/logs/nginx-error.log

output:

2022/09/30 20:54:25 [error] 3174#3174: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 24.130.137.133, server: ec2-52-203-10-77.compute-1.amazonaws.com, request: "GET /flightmonitor/ HTTP/1.1", upstream: "http://0.0.0.0:8001/flightmonitor/", host: "ec2-52-203-10-77.compute-1.amazonaws.com"

This makes me think the problem is related to Daphne not starting?

I also tried restarting the services, but that didn't work:

sudo systemctl daemon-reload
sudo systemctl restart nginx.service
sudo systemctl restart daphne.service

The 502 error stays and Daphne still says failed.

Other info: the server is an AWS EC2 Ubuntu 18.04 instance, and the AWS RDS instance is db.t3.small with public access. The error only happens after I follow the steps to use AWS RDS.

Back to Top