子域名上的 502 错误网关 nginx

子域名上的 502 错误网关 nginx

我有一个新的 droplet,运行 ubuntu 20.04。我只使用此服务器来托管我的 api,因此我尝试将其转发到 api.example.com。问题是,当我访问https://api.example.com但是502 Bad Gateway当我访问时http://example.com,我得到了 nginx 欢迎页面。我已经重新启动了 nginx,并运行了此程序,sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/但我仍然收到错误。我还注册了 api.example.com,它重定向到 https,所以我对这个问题有点困惑。nginx 错误日志显示:

2021/11/21 03:31:57 [error] 8737#8737: *25 connect() failed (111: Unknown error) while connecting to upstream, client: my ip, server: api.example.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3001/favicon.ico", host: "api.example.com", referrer: "https://api.example.com/". 

我的服务器也在端口 3001 上运行。

我的 nginx 配置是:

    server {
      listen 80;
      listen [::]:80;
    
      server_name api.example.com;
      
      location / {
    proxy_pass http://localhost:3001;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.exmaple.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api.exmaple.com/privkey.pem; # managed by Certbot
    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; 
        }
    
        listen 80;
        listen [::]:80;
    
        server_name api.example.com;
            return 404;
        
    }

另外我没有使用 cloudflare,所以我不认为这是 DNS 问题。如能得到任何帮助我将不胜感激。

相关内容