13

There are multiple site config in my nginx, and when I restart the machine, if one of the sites' upstream cannot be reached, nginx won't start at all, and those healthy site won't start as a result, how to let nginx ignore those invalid sites?

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

and sites-enabled/example1 is

upstream example1 {
    server example1.service.example.com;
}
server {
listen 80;
server_name example1.com;
location / {
    proxy_pass http://example1/;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}
}

and sites-enabled/example2 is

upstream example2 {
    server example2.service.example.com;
}
server {
listen 80;
server_name example2.com;
location / {
    proxy_pass http://example2/;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}
}

When I restart the machine and at that time example2.service.example.com is down, nginx will not start at all, i.e. even example1.service.example.com is available, nginx will not serve for example1

=====update Explanation of "is down": All the subdomains is automatically registered/deregistered on my own dns server, so if the server is down, the dns will respond no such domain when try to resolve it.

  • Could you show your config? – Tero Kilkanen Jun 23 '15 at 17:13
  • @TeroKilkanen added. – cgcgbcbc Jun 26 '15 at 4:26
  • @AD7six yes, I mean the upstream doesn't resolve, see question update for more details – cgcgbcbc Jun 26 '15 at 6:16
  • I don't think you can force nginx to start with what boils down to bad config. Since you control the DNS, perhaps set it up to return a valid result with nginx using a short resolver cache. – AD7six Jun 26 '15 at 6:28
  • @AD7six would backup in upstream work around? I mean if I added another host(which will always be resolvable) in the upstream as backup, would nginx start up when the normal upstream cannot resolve? – cgcgbcbc Jun 26 '15 at 7:11
15

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.

| improve this answer | |
  • 1
    Did not work for me. When doing proxy_pass $target;, I get "502 Bad Gateway", proxy_pass http://$target gives me "500 Internal Server Error". That is when Nginx is in fact able to resolve the host. – kba Jan 14 '18 at 17:34
  • 3
    With @EmilBurzo's addition, this works. – kba Jan 14 '18 at 17:39
13

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.