nginx proxy_pass命令 地址为域名时, nginx启动报 host not found in upstream

我想访问本地网站的指定uri时,实际访问的是另一网站,于是使用了nginx作为代理

nginx相关配置如下
server {

listen 80;
server_name www.example.com;
.....

location /foo/ {
    proxy_store off;
    proxy_redirect off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Referer 'no-referrer-when-downgrade';
    proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36';
    proxy_pass https://api.example.com/foo/;
}

}

nginx 启动时报错 host not found in upstream 'api.example.com'

阅读 5.8k
2 个回答

已解决

location /foo/ {
    resolver 8.8.8.8; 
    set $backend "api.example.com";
    proxy_pass https://$backend;
}

你的upstream配置里面没有'api.example.com',可以贴一下你的upstream配置吗

类似这样

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏