Score:0

502 bad gateway nginx on subdomain

gb flag

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.

djdomi avatar
za flag
i am unsure but both servers handle the same and nginx -t should not work due it looks like a missing server block. why you listen for http 80 on the ssl block but define below the same?
user avatar
gb flag
The second server block was generated by certbot.
djdomi avatar
za flag
due that i am on my mobile with limited ability, try this https://pastebin.ccb-net.it/?228f82a007d63498#BQEup56ZyzoJyku7qwYmQavMMn8dtTp7b3XAc7kQcUCH - it makes the config a bit clearer to read
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.