原创

【Nginx】host not found in upstream

前言

运维的站点使用docker-compose工具进行容器编排,使用nginx做了站点管理,允许同一台机器同时挂着多个站点,比如我现在有A,B,C三个站点,A,B两个作为主站点,是保证启动的状态,C站点作为工具站点,可以无所谓启动还是关闭状态,如果C站点关闭了,NGINX启动的时候,就会报错,提示[emerg] host not found in upstream。所以很麻烦,必须解决了。

解决目标

Nginx启动多站点的时候,忽略未启动的站点(服务)。

解决方式

解决方式如下:

原始的配置文件:

server {
        listen       443;
        ssl                  on;
        ssl_certificate      cert/domain/fullchain.cer;
        ssl_certificate_key  cert/domain/domain.key;
        ssl_trusted_certificate cert/domain/ca.cer;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        server_name website1.domain.cn;

        location / {
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
            proxy_pass              http://website1:80/;
            proxy_read_timeout      600s;
            proxy_send_timeout      600s;
        }
}

调整后:

server {
        listen       443;
        ssl                  on;
        ssl_certificate      cert/domain/fullchain.cer;
        ssl_certificate_key  cert/domain/domain.key;
        ssl_trusted_certificate cert/domain/ca.cer;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        server_name website1.domain.cn;

        location / {
            resolver 127.0.0.11 valid=30s;
            set $website1var website1:80;
            proxy_pass        http://$website1var;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;
            proxy_read_timeout      600s;
            proxy_send_timeout      600s;
        }
}

参考

https://serverfault.com/questions/700894/make-nginx-ignore-site-config-when-its-upstream-cannot-be-reached

本文来自:【Nginx】host not found in upstream-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2023年01月20日,已超过 671 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
本文目录