本地运行的 Azure Functions 应用程序可从 http://localhost:7071 获取。 。 服务器端渲染的 React 应用程序公开于 http://localhost:7070 。 函数应用程序和 React 应用程序都是从本地托管的简单 nginx 容器 ( http://localhost:7072 ) 进行反向代理。为了完整起见,这是 nginx 配置:
http {
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mywebapp-localdev;
location /api {
proxy_pass http://10.0.75.1:7071;
}
location / {
proxy_pass http://10.0.75.1:7070;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
问题是:如果地址 http://10.0.75.1:7071/api/hello-world直接从浏览器点击,该函数的行为符合预期。但是,如果通过反向代理( http://localhost:7072/api/hello-world )调用该函数,则会超时。 nginx 容器内的一些测试还表明 10.0.75.1 上的端口 7071 无法访问。
问题是:如何从也在本地运行的 Docker 容器访问本地运行的 Azure Function(使用 func host start
)?
最佳答案
明白了:
不使用上述 IP 地址,只需使用内部主机解析地址 (host.docker.internal) 即可:
{
location / {
proxy_pass http://host.docker.internal:7070;
}
}
根据 Docker 的版本,我偶然发现了许多不同的答案。这解决了它。
关于无法从 Docker 容器访问本地运行的 Azure Functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428750/