大网数据广告活动
服务器运维问答

make nginx ignore site config when its upstream cannot be reached

· 52CE 我爱测

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 …

问题原文

原帖链接 · cgcgbcbc · CC BY-SA 3.0

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.

社区高分解答

原帖链接 · cgcgbcbc · CC BY-SA 4.0

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 $target;
    }
}

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-06-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。