nginx 反向代理,当 proxy_pass 指向的主机不存在的时候,能不能别中断整个 nginx,让他继续运行?

2018-01-12 10:26:04 +08:00
 find456789

根据访问域名,由 nginx 代理到不同的主机上

下面的配置,

当用户访问 aaa.com 就代理到 http://host1aaa:9001

当用户访问 bbb.com 就代理到 http://host2bbb:9001

这里的 host1aaa,host2bbb 是内网的主机名,

偶尔,如果 host2bbb 整个机器没有开起来,

nginx 就会找不到主机头,

然后就报错了,结果导致 用户也无法访问 aaa.com

大家有办法,让他在忽略错误,继续运行吗?

server { 
    listen 80; 
    server_name aaa.com; 

    location / { 
        proxy_pass http://host1aaa:9001; 
    } 
} 



server { 
    listen 80; 
    server_name bbb.com; 

    location / { 
        proxy_pass http://host2bbb:9001; 
    } 
}
3090 次点击
所在节点    问与答
22 条回复
BOYPT
2018-01-12 10:29:42 +08:00
不是有 health check 模块么。
rrfeng
2018-01-12 10:52:01 +08:00
并不会像你说的这样。
iyaozhen
2018-01-12 11:36:21 +08:00
「然后就报错了」截个图?贴个日志?
你后面只有一个的话肯定就挂了,还想啥呢?
Ellison
2018-01-12 11:42:08 +08:00
为什么会中断整个 nginx?只会 502 啊
Nioty
2018-01-12 11:52:32 +08:00
Lz 的问题不是很明白…
gstqc
2018-01-12 11:54:57 +08:00
你可能用了个假的 nginx
akira
2018-01-12 12:18:12 +08:00
普通的 nginx 并没有你这个问题。你的问题可能是别的原因导致的
MonoLogueChi
2018-01-12 12:25:52 +08:00
这种问题 nginx 应该不会报错啊
kn007
2018-01-12 12:26:02 +08:00
楼主说的问题,确实是有的,反代域名,如果域名不可解析(dns 返回无 A 记录),那么 nginx 会启动失败。
你要么反代 ip,要么在 hosts 定义主机名 ip。楼上那些没遇到过,只能说实际应用没有这种场景。
kn007
2018-01-12 12:27:42 +08:00
你们可以反代下 test.test.test,能启动 nginx,算我输。
当然啦,你或是网络要是定义了 test.test.test,那不算。
oott123
2018-01-12 12:31:06 +08:00
set shabi_nginx example.com:12345;
proxy_pass http://$shabi_nginx;
find456789
2018-01-12 12:31:43 +08:00
@kn007 确实是你说的这种情况, 因为我是用 docker 启动的,有时候某个 docker 容器我不想运行,就临时注释(只想注释,懒得去改 nginx 配置文件),结果 nginx 就找不到了,然后就启动失败, 这时候,其他服务,也无法工作了, 这个比较尴尬
kn007
2018-01-12 12:35:17 +08:00
@find456789 因为 docker 是你管理的,他的 ip 是可控的,我的建议呢,是你在 nginx 反代 ip,而不是 host,如果你实在为了好看,好管理,想用 host,那么在 /etc/hosts,定义 host 的 ip。就解决了。
find456789
2018-01-12 12:42:22 +08:00
@oott123 大神,你给的这段代码,写进去,提示错误
-----------------------------------------

set shabi_nginx example.com:12345;
location / {
proxy_pass https://$shabi_nginx/;
}

-----------------------------------------

》~ $ /usr/local/bin/nginx -t
-----------------------------------------

nginx: [emerg] invalid variable name "shabi_nginx" in /usr/local/etc/nginx/nginx.conf:59
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
oott123
2018-01-12 12:45:48 +08:00
少了个 $ 你补上啊

set $shabi_nginx example.com:12345;
find456789
2018-01-12 12:51:20 +08:00
@oott123 大神

似乎还是不行,给$shabi_nginx 的参数加双引号,或者不加,都会出错

/usr/local/bin/nginx -t 返回测试成功

----------------------------------

set $shabi_nginx "https://www.baidu.com" ;
location / {
proxy_pass $shabi_nginx;
}



-----------------------------------------
报错如下

An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.
oott123
2018-01-12 12:56:06 +08:00
@find456789 #16 出错是 5xx 啊,十分正常,你自己看看 error_log 呗
find456789
2018-01-12 13:10:59 +08:00
@oott123 可以了, 错误解决办法是

在 nginx 的配置文件中的 http{}部分添加一行 resolver 8.8.8.8;即可
likuku
2018-01-12 13:14:42 +08:00
要高可用么?那么每个域名对应后端的真实主机就别是单个的,至少每个域名得有 2 个独立主机保持最低冗余。
cross874
2018-01-12 13:44:22 +08:00
next upstream
还有你可以把返回码当一个 location 来定义就行了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/422263

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX