Home > Back-end >  Can't figure out nginx config: *2 no resolver defined to resolve backend
Can't figure out nginx config: *2 no resolver defined to resolve backend

Time:02-22

I can't figure out the NGINX config to launch the site.

I can’t figure it out myself, all the same, there is not enough theoretical basis. Maybe you can advise.

env BACKEND_API;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_processes 4;
events {
   worker_connections  1024;
}
   http {
      include mime.types;
      default_type application/octet-stream;
      upstream backend {
         server backend;
      }
   server {
       set_by_lua $backend_url 'return os.getenv("BACKEND_API")';
       listen 80;
       server_name _;
       server_tokens off;
       client_max_body_size 100M; 

       location / {
           root   /www/html;
           index  index.html index.htm;
           try_files $uri $uri/ /index.html;
       }

       location /api {
           try_files $uri @proxy_api;
       }

       location @proxy_api {
           proxy_set_header X-Forwarded-Proto https;
           proxy_set_header X-Url-Scheme $scheme;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect off;
           proxy_pass $backend_url;   
       } 
   }
  }

I get an error:

frontend        | 2022/02/21 08:29:58 [error] 28#28: *2 no resolver defined to resolve backend, client: 31.176.83.92, server: _, request: "POST /api/auth HTTP/1.1", host: "194.86.154.183", referrer: "http://194.86.154.183/"

Can you please tell me where to look? Any advice and help is welcome.

CodePudding user response:

In your http block the below config:

upstream backend {
     server backend;
  }

is throwing the error as it is not able to resolve the server value as backend.

here is a basic uses of upstream module from docs

upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server unix:/tmp/backend3;
    
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

CodePudding user response:

When you define your backend server domain name as a constant string, nginx resolves its IP address during the startup. But when you are using variables in your backend server domain name, nginx can't resolve it at the startup time and needs to do a DNS lookup to find out what its IP address is. And to make this lookup it needs a working resolver address. Here is a quote from some blog post:

Linux, POSIX and the like offer only one way to get an IP from a name: gethostbyname. If you take time to read the man page (always a safe thing to do... ;)) you'll realise there is a lot to do to resolve a name: open files, ask NIS or YP what they think about it, ask a DNS server (may be in a few different ways). And all this is synchronous. Now that you are used to the nginx way, you know how bad this is and you don't want to go down the ugly synchronous way. So Igor, faithful to himself, reimplemented a DNS lookup (with an in-memory cache, mind you) just to avoid calling this ugly blocking gethostbyname... And that's why we have this extra resolver directive. Yes, coding the fastest web server comes at a price...

  • Related