Score:0

Nginx connect() failed (111: Connection refused) while connecting to upstream

py flag
Neo

I'm seeing the following error message in nginx logs

[0m[0;33;1mnginx.1     | [0;31;1m2023/05/24 10:16:17 [error] 144#144: *76 connect() failed (111: Connection refused) while connecting to upstream, client: 172.90.174.63, server: api-staging, request: "GET /ws/notifications/?userid=user1 HTTP/1.1", upstream: "https://127.0.0.1:8000/ws/notifications/?userid=user1", host: "api-staging"

nginx conf

server {
        server_name api-staging;
        listen 443 ssl http2 ;
        access_log /var/log/nginx/access.log vhost;
        ssl_session_timeout 5m;
        ssl_session_cache shared:SSL:50m;
        ssl_session_tickets off;
        ssl_certificate /etc/nginx/certs/api-staging.crt;
        ssl_certificate_key /etc/nginx/certs/api-staging.key;
        ssl_dhparam /etc/nginx/certs/api-staging.dhparam.pem;
        ssl_stapling on;
        ssl_stapling_verify on;
        ssl_trusted_certificate /etc/nginx/certs/api-staging.chain.pem;
        add_header Strict-Transport-Security "max-age=31536000" always;

    location /ws/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
    location /ws/notifications/ {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
    location / {
        proxy_pass http://api-staging;
    }
}

Its secure websocket service implemented via Daphne that is listening to 8000. The service is indeed running. But from Daphne logs, it looks like the request Nginx is trying to forward is not received at all.

wget https://api-staging/ws/notifications/?userid=user123455
--2023-05-25 05:18:27--  https://api-stagingi/ws/notifications/?userid=user123455
Resolving api-staging (api-staging)... 138.164.92.3, 188.114.97.3, 2a06:98c1:3121::3, ...
Connecting to api-staging (api-staging)|138.164.92.3|:443... connected.
HTTP request sent, awaiting response... 502 Bad Gateway
2023-05-25 05:18:27 ERROR 502: Bad Gateway.

I'am not sure what changes I need to do to nginx conf to fix this. Any help will be hugeee. thanks

Nikita Kipriyanov avatar
za flag
Where this Nginx config came from? Do you know which service this 127.0.0.1:8000 refers to? Did you checked its status, what is it? What is in its logs?
Neo avatar
py flag
Neo
@NikitaKipriyanov I have updated the description. Please refer the same.
Nikita Kipriyanov avatar
za flag
You didn't answered to any of my questions. The problem is with the service that is supposed to be listening on 127.0.0.1:8000 (on the same system where Nginx is running), you need to identify what it should be and why it doesn't work.
Neo avatar
py flag
Neo
Apologies.. The service in question is Daphne ASGI server for implementing secure websockets. I could verify that Daphne is indeed running and listening to port 8000. Daphne logs show no incoming requests. Nginx and Daphne are running on 2 different docker containers in an EC2 instance.
Nikita Kipriyanov avatar
za flag
Then you need to refer not to 127.0.0.1:8000 in Nginx configuration, but to whatever address your real service is listening at, probably <daphne_container_name>:8000
Neo avatar
py flag
Neo
Thank you. Will try that..
Neo avatar
py flag
Neo
@NikitaKipriyanov the problem is, since the containers are running in EC2, the container names keep changing every time there is a deployment. From ecs-staging-275-daphne-e295d1d1b2dad2e1da01 it became ecs-staging-272-daphne-acd98bf5feb1a2f0e901. So, nginx failed to start. Can you please suggest what I can do next?
Nikita Kipriyanov avatar
za flag
Next you need to rethink and reformulate the question, because this is information crucial to understand your problem and to suggest a solution, and therefore it needs to be in the question, not in comments. Don't forget to update tags so someone more fluent with EC2 will notice your question and probably help.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.