当无法到达上游时,使 nginx 忽略站点配置

cgc*_*cbc 17 nginx

我的 nginx 中有多个站点配置,当我重启机器时,如果其中一个站点的上游无法访问,则 nginx 根本不会启动,结果那些健康的站点也不会启动,如何让nginx 忽略那些无效站点?

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/*;
}
Run Code Online (Sandbox Code Playgroud)

和网站启用/example1 是

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;
}
}
Run Code Online (Sandbox Code Playgroud)

和网站启用/example2 是

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;
}
}
Run Code Online (Sandbox Code Playgroud)

当我重新启动机器时,此时example2.service.example.com 宕机,nginx 根本启动不了,即即使example1.service.example.com 可用,nginx 也不会为example1 提供服务

=====更新“已关闭”的解释:所有子域都在我自己的dns服务器上自动注册/注销,因此如果服务器关闭,dns在尝试解析时将响应无此类域。

cgc*_*cbc 17

最后我找到了解决办法,解决了域insdie位置的工作!

例子:

server {
    listen 9000;
    server_name example1.example.com;
    location / {
        set $target http://something.service.lab.mu;
        proxy_pass http://$target;
    }
}
Run Code Online (Sandbox Code Playgroud)

并且 nginx 不会http://something.service.lab.mu在开始时尝试解析。

  • 随着@EmilBurzo 的加入,这是有效的。 (3认同)
  • 没有为我工作。在执行 `proxy_pass $target;` 时,我得到“502 Bad Gateway”,`proxy_pass http://$target` 给我“500 Internal Server Error”。那是 Nginx 实际上能够解析主机的时候。 (2认同)

小智 15

对于任何在这个问题上绊倒的人,@cgcgbcbc 是正确的。

但是你还需要添加一个

resolver 8.8.8.8;
Run Code Online (Sandbox Code Playgroud)

上面的指令

set $target http://something.service.lab.mu;
Run Code Online (Sandbox Code Playgroud)

否则你会在 nginx 中得到一个错误,比如:

no resolver defined to resolve
Run Code Online (Sandbox Code Playgroud)


ISt*_*ger 7

有几种方法可以避免它:

  1. 使用静态IP,nginx如果没有响应将返回503。

  2. 使用解析器指令指向可以解析主机的内容,无论它当前是否启动。

  3. 如果您无法执行上述操作,请在位置级别使用解析器指令(这将允许 Nginx 启动):

     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;
     }
    
    Run Code Online (Sandbox Code Playgroud)