nginx and https: nginx: [emerg] host not found in upstream

First of all excuse me if this problem is simple.

I have a server with Ubuntu and nginx on it. Always works while http used, but after I added certificates and tried to enable https I received this error nginx: [emerg] host not found in upstream "api".
Here is my config:

upstream api {
    least_conn;
    server 176.9.91.243:7123;
}

upstream api2 {
    least_conn;        
    server 176.9.32.170:7123;       
}

server {
#   listen 80;
    listen 443;

    ssl on;

    ssl_certificate /etc/ssl/chain.crt;
    ssl_certificate_key /etc/ssl/ssl_key.key;

    add_header Strict-Transport-Security "max-age=31536000";
    add_header Content-Security-Policy "block-all-mixed-content";


    root /var/www/hosting;

    index index.html index.htm;
    client_max_body_size 15m;

    server_name serverName.com;
    gzip  on;
    gzip_min_length 1000;
    gzip_proxied    expired no-cache no-store private auth;
    gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

    set $const 1;



    location /signalr {
        limit_conn addr 1000;
        access_log off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass https://api2/signalr;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
            proxy_read_timeout 120s;
            proxy_buffering off;
            keepalive_timeout 120s;
    }


    location /api/signalr {
        limit_conn addr 1000;
            access_log off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass https://api2/signalr;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
            proxy_read_timeout 120s;
            proxy_buffering off;
            keepalive_timeout 120s;
    }



    location /api/notifications{
        limit_conn addr 1000;
            access_log off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass https://api2/notifications;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
            proxy_read_timeout 120s;
            proxy_buffering off;
            keepalive_timeout 120s;
    }


    location /api/notificationhistory{
        limit_conn addr 1000;
        access_log off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass https://api2/notificationhistory;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
            proxy_read_timeout 120s;
            proxy_buffering off;
            keepalive_timeout 120s;
    }


        location /api/swagger {    
        access_log off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass https://api/swagger;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 120s;
            proxy_buffering off;
            keepalive_timeout 120s;

        }


    location /api {
    limit_conn addr 3000;
      rewrite /api(.*) /$1  break;
      proxy_read_timeout 60s;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass https://api;
      proxy_ssl_session_reuse off;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      #access_log /var/log/nginx/api-access.log upstreamlog;
    }

}

The problem only happens with api upstream while api2 works fine. If I use the IP directly in proxy_pass it works as well. What have I missed? Thank you.


Solution 1:

I'm pretty sure you're missing the resolver directive: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver