make nginx ignore site config when its upstream cannot be reached

Finally I find out the walkaround, resolve the domain insdie location works!

example:

server {
    listen 9000;
    server_name example1.example.com;
    location / {
        set $target http://something.service.lab.mu;
        proxy_pass http://$target;
    }
}

And nginx won't try to resolve http://something.service.lab.mu at start time.


For anyone stumbling upon this issue, @cgcgbcbc is correct.

But you also need to add a

resolver 8.8.8.8;

directive above the

set $target http://something.service.lab.mu;

otherwise you'll get an error in nginx, like:

no resolver defined to resolve

There are few ways to avoid it:

  1. Use static IP, nginx will return 503's if it doesn't respond.

  2. Use the resolver directive to point to something that can resolve the host, regardless if it's currently up or not.

  3. Use the resolver directive at the location level, if you can't do the above (this will allow Nginx to start):

     location /some_path {
       resolver    127.0.0.1   valid=30s;   
       # resolver    8.8.8.8     valid=30s;   # or some other DNS
       # resolver    127.0.0.11  valid=30s;   # or Docker's DNS server
    
       set         $dummy_var  http://some_domain:80;
       proxy_pass  $dummy_var;
     }