I have a fresh droplet running ubuntu 20.04. I'm only using this server to host my api so I'm trying to get it to forward to api.example.com. The problem is, when I visit https://api.example.com I get 502 Bad Gateway
however when I visit http://example.com, I get served the nginx welcome page. I have already restarted nginx, and ran this sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
however I'm still getting the error. I have also registered api.example.com and it redirects to https so I'm kinda confused on the problem. The nginx error logs shows:
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/".
I also have my server running on port 3001.
My nginx config is:
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;
}
Also I'm not using cloudflare so I don't think this is a dns problem. Any help would be appreciated.