nginxのproxy_passで名前解決してくれない

nginxのproxy_passで名前解決してくれない

nginxが動作しているホストの /etc/hosts にこんな行を書いて

172.18.1.1 hogehoge.example.com

nginxでこんな感じにかいてみました。

location ~ /hogehoge/ {
    proxy_pass http://hogehoge.example.com ;
}

ところが、これだと502エラーになっちゃう。
errorlogに名前解決できないみたいなエラー。

022/06/13 17:59:00 [error] 28796#0: *21619 no resolver defined to resolve hogehoge.example.com

どうもnginxさんはhostsはみてくれないみたい。

じゃあどうするの

dnsmasqをいれて、localhostで名前させる。その上でresolverを指定する。

dnsmasqをインストール

# yum -y install dnsmasq

dnsmasqを起動

# systemctl start dnsmasq

resolverを記述

location ~ /hogehoge/ {
 resolver 127.0.0.1 ;
    proxy_pass http://hogehoge.example.com ;
}

めんどくさいな。