简体   繁体   中英

connection refused django gunicorn nginx

I am getting following error when running search feature on my site (search feature makes api request to my api server):

2022/08/31 21:01:56 [error] 726#726: *23 connect() failed (111: Connection refused) while connecting to upstream, client: 11.111.111.111, server: api.mydomain.com, request: "GET /dividends/IBM/3/5 HTTP/1.1", upstream: "http://127.0.0.1:8001/dividends/IBM/3/5", host: "api.example.com", referrer: "example.com/"

The supervisord gunicorn is up, the mongodb is up, and nginx is up

I have the following /etc/nginx/sites-available/stocks_backend :

upstream stocks_backend {
    server 127.0.0.1:8001 fail_timeout=4s;
}

server {
    server_name api.example.com www.api.example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/stocks_backend;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/etc/systemd/system/gunicorn.socket;
        proxy_ssl_server_name on;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; # managed by C>
    ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; # managed by>
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = api.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;
    server_name api.example.com www.api.example.com;
    return 404; # managed by Certbot

    access_log /root/stocks_backend/log/nginx-access.log;
    error_log /root/stocks_backend/log/nginx-error.log;
}

I have tried things from Django gunicorn nginx (111: Connection refused) while connecting to upstream but when I remove http:// from the proxy_pass to the socket to proxy_pass unix:/etc/systemd/system/gunicorn.socket; I get

nano /etc/nginx/sites-available/stocks_backend
root@my-droplet:~/stocks_backend# sudo service nginx restart
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

gunicorn socket

root@-droplet:~/stocks_backend# cat /etc/systemd/system/gunicorn.socket 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Change this line

proxy_pass http://unix:/etc/systemd/system/gunicorn.socket;

to

proxy_pass unix:/etc/systemd/system/gunicorn.socket;

Essentially removing http:// because that's not how NGINX communicates with Gunicorn.

Then, for the other problem that OP is getting, as of now OP doesn't have enough details.

How can OP get more details about the problem? OP can setup NGINX error logs, as explained here . Then OP should be able to resolve the issue with the info from the logs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM