node.js - 如何配置 nginx docker 容器以识别不同端口中的本地主机应用程序?

标签 node.js docker nginx nginx-location nginx-reverse-proxy

设置: 以下所有内容均在我的 Mac 操作系统上运行:

本地主机:8089 a nodejs REST api runing in my local, OUTSIDE of the nginx container, stand alone!

本地主机:80 nginx docker container

我能够在 nginx docker 容器内提供静态文件,但是当我将 nginx 的配置设置为:

http {
    server {
        location / {
            root /usr/share/nginx/html;
        }
        location /api/ {
            proxy_pass http://localhost:8089;
        }
    }
}

出于某种原因,任何 localhost:80/api打电话假设直接到 http://localhost:8089;调用返回 404 未找到页面

404 Not Found

nginx/1.13.6

知道我的配置哪里错了吗?我觉得也许我不应该使用 localhost:8089在 Nginx 内部?但那我应该使用什么呢?

可以在此处找到示例 https://github.com/adamchenwei/docker-nginx-playground

最佳答案

容器有自己的网络命名空间/网络堆栈。所以容器内的localhost就是容器本身的localhost。

如果您运行的是 Mac 版 Docker,则容器内有一个特殊的主机名,可让您连接到主机本身上运行的服务。请参阅此答案以获取更多信息; https://stackoverflow.com/a/43541732/1811501

如果您在 Linux 上运行 Docker,则此答案(同一问题)允许您找到要连接的 IP 地址; https://stackoverflow.com/a/31328031/1811501

关于node.js - 如何配置 nginx docker 容器以识别不同端口中的本地主机应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665676/

相关文章:

Openshift 中的 NGINX - NGINX 无法解析内部主机名

php - 带有while循环的mysql Node js动态数组

nginx - 如何负载均衡容器?

javascript - CSV 文件读取、过滤和处理并在 Node.js 中以控制台表形式显示

docker - Ubuntu集群设置后无法Ping Pod

docker - Docker卷NFS uid

php - Laravel 空白白屏 - Centos 7、NGINX 和 PHP7.1

http - nginx 代理请求缓冲未按预期工作

node.js - Nodejs new url.URL ("https://myurl.com:80") 将端口列为空

node.js - npm 安装不起作用