我的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/*;
}
并且 sites-enabled/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;
}
}
并且 sites-enabled/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;
}
}
当我重新启动机器时,此时 example2.service.example.com 已关闭,nginx 根本无法启动,即即使 example1.service.example.com 可用,nginx 也不会为 example1 提供服务
=====更新“is down”的解释:所有子域名都会在我的 DNS 服务器上自动注册/取消注册,因此如果服务器关闭,DNS 在尝试解析时将不会响应这样的域名。
答案1
最后我找到了解决方法,解析域名 insdie 位置有效!
例子:
server {
listen 9000;
server_name example1.example.com;
location / {
set $target http://something.service.lab.mu;
proxy_pass $target;
}
}
并且 nginx 不会http://something.service.lab.mu
在启动时尝试解析。
答案2
对于任何偶然发现此问题的人来说,@cgcgbcbc 是正确的。
但你还需要添加一个
resolver 8.8.8.8;
上述指令
set $target http://something.service.lab.mu;
否则你会在 nginx 中收到一个错误,例如:
no resolver defined to resolve
答案3
有几种方法可以避免这种情况:
使用静态IP,如果nginx没有响应,将返回503。
使用解析器指令指向可以解析主机的某个东西,无论它当前是否启动。
如果您无法执行上述操作,请在位置级别使用解析器指令(这将允许 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; }