连接被拒绝django gunicorn nginx

连接被拒绝django gunicorn nginx

我在我的网站上运行搜索功能时出现以下错误(搜索功能向我的 API 服务器发出 API 请求):

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/"

Supervisord gunicorn 已启动,mongodb 已启动,nginx 已启动

我有以下内容/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;
}

我已经尝试过了https://stackoverflow.com/questions/44046611/django-gunicorn-nginx-111-connection-refused-while-connecting-to-upstream但是当我从 proxy_pass 中删除 http:// 到套接字时,proxy_pass unix:/etc/systemd/system/gunicorn.socket;我得到了

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 插座

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

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

答案1

您指定了不同的路径:

  • 您让 systemd 为 gunicorn 创建的套接字位于/run/gunicorn.sock

  • 你告诉 nginx 连接的套接字是/etc/systemd/system/gunicorn.socket

后者不合适。/etc 用于配置,即 systemd 配置文件所在的位置 - 而不是因该配置而创建的套接字的最终位置。更改它,然后 nginx 可以连接(或者在解决此问题后,将针对阻止程序输出不同的消息)。

相关内容