Docker Nginx stopped: [emerg] 1#1: host not found in upstream

六月ゝ 毕业季﹏ 提交于 2019-12-02 20:43:20

Include to prevent Nginx from crashing if your site is down, include a resolver directive, as follows:

 server {
       listen      80;
       server_name     test.com;
       location / {
          resolver 8.8.8.8
          proxy_pass         http://dev-exapmle.io:5016/;
          proxy_redirect     off;
 ...

WARNING! Using a public DNS create a security risk in your backend since your DNS requests can be spoofed. If this is an issue, you should point the resolver to a secure DNS server.

Just adding a resolver did not resolve the issue in my case. But I was able to work around it by using a variable for the host. Also, I guess it makes more sense to use Docker's DNS at 127.0.0.11 (this is a fixed IP).

Example:

server {
  listen 80;
  server_name test.com;

  location / {
    resolver 127.0.0.11;
    set example dev-example.io:5016
    proxy_pass http://$example;
  }
}

I found the variable workaround on this page.

This usually means that the dns name you provided as upstream server cannot be resolved. To test it, log on nginx server and try pinging upstream server provided and see if the name resolution completes correctly, If its a docker container try docker exec -it to get a shell, then try pinging the upstream to test the name resolution. If the contianer is stopped try to use IP address instead of dns name in your server block.

proxy_pass         http://<IP ADDRESS>:5016/;

You can also use the resolver directive if you want to use different dns server for this location than the host system:

resolver 8.8.8.8;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!