【问题标题】:Nginx docker as nodejs proxy in localNginx docker作为本地的nodejs代理
【发布时间】:2017-09-29 07:48:50
【问题描述】:

目前我在 docker 中使用 nginx 来代理我的节点应用程序

例如,我在我的主机文件中添加website.dev

而我实际的 nginx 配置是

worker_processes 4;
pid /run/nginx.pid;

events {}

http {

    server {
      listen 80;
      server_name website.dev;
      location / {
          proxy_pass    http://localnode:3000;
          proxy_set_header X-Base-Path "/";
          proxy_set_header Website-Name "test";
      }
    }
}

我用这个命令启动我的 docker 容器

docker run --name infra-nginx --add-host localnode:$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev

在哪里

$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}') is getting my local ip, not working all the time.

问题是如果我想在没有互联网的情况下工作,我不能。 当我的 ip 改变时,我必须用新的 ip 重新启动容器。

我尝试使用此配置

worker_processes 4;
pid /run/nginx.pid;

events {}

http {

    server {
      listen 80;
      server_name localhost;
      location / {
          return 200 'gangnam style!';
      }
    }

    server {
      listen 80;
      server_name website.dev;
      location / {
          proxy_pass    http://localhost:3000;
          proxy_set_header X-Base-Path "/";
          proxy_set_header Website-Name "test";
      }
    }
}

然后运行

docker run --name infra-nginx --network host -d docker-registry.host.com:5000/infra-nginx:dev

在这种情况下,当我跑步时:

curl http://website.dev/

我有

curl: (7) Failed to connect to website.dev port 80: Connection refused

docker ps 正在给予

81da561dd131        ajouve/infra-nginx:dev   "nginx -g 'daemon ..."   32 minutes ago      Up 32 minutes                                                          infra-nginx

netstat -plant 给了我

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro
tcp        0      0 172.17.0.1:35962        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35938        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35994        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:45080        172.17.0.5:6379         ESTABLISHED -
tcp        0      0 172.17.0.1:35990        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 192.168.65.2:44900      151.101.0.204:80        TIME_WAIT   -
tcp        0      0 172.17.0.1:45126        172.17.0.5:6379         ESTABLISHED -
tcp        0      0 172.17.0.1:36000        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35958        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:59172        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:35976        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:59106        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:35980        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35996        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:58356        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35966        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:45112        172.17.0.5:6379         ESTABLISHED -
tcp        0      0 172.17.0.1:35932        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:58366        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35998        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 192.168.65.2:41386      206.251.255.63:80       TIME_WAIT   -
tcp        0      0 172.17.0.1:58358        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35956        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35924        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:36004        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:58360        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35964        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35916        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:58362        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:59148        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:59166        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:35944        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35912        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35954        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:45116        172.17.0.5:6379         ESTABLISHED -
tcp        0      0 172.17.0.1:58354        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35988        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:59122        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 192.168.65.2:34936      5.153.231.4:80          TIME_WAIT   -
tcp        0      0 192.168.65.2:44904      151.101.0.204:80        TIME_WAIT   -
tcp        0      0 172.17.0.1:59162        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:59180        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:45130        172.17.0.5:6379         ESTABLISHED -
tcp        0      0 172.17.0.1:59140        172.17.0.3:5672         ESTABLISHED -
tcp        0      0 172.17.0.1:36002        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35922        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:35970        172.17.0.4:27017        ESTABLISHED -
tcp        0      0 172.17.0.1:58364        172.17.0.4:27017        ESTABLISHED -
tcp6       0      0 :::6379                 :::*                    LISTEN      -
tcp6       0      0 :::15672                :::*                    LISTEN      -
tcp6       0      0 :::5672                 :::*                    LISTEN      -
tcp6       0      0 :::27017                :::*                    LISTEN      -

来自 docker 容器:

curl -v localhost

给予

* Rebuilt URL to: localhost/
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 80 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:10 GMT
< Content-Type: text/plain
< Content-Length: 14
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
gangnam style!

但是curl -v website.dev 正在返回

* Rebuilt URL to: website.dev/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to website.dev (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: website.dev
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
* Server nginx/1.10.3 is not blacklisted
< Server: nginx/1.10.3
< Date: Fri, 29 Sep 2017 08:46:37 GMT
< Content-Type: text/html
< Content-Length: 173
< Connection: keep-alive
<
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
* Connection #0 to host website.dev left intact

还有docker inspect infra-nginx

[
    {
        "Id": "16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e",
        "Created": "2017-09-29T08:31:21.144827953Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2656,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-09-29T08:31:21.548119911Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:25d085baee52923e32f8d134048238fb67e71173e01f758c391119235f7fc565",
        "ResolvConfPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hostname",
        "HostsPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/hosts",
        "LogPath": "/var/lib/docker/containers/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e/16941d22442a257f0874a772df935514c658ac16ec67eb3f65606b4d7c0ee62e-json.log",
        "Name": "/infra-nginx",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "host",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "moby",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "443/tcp": {},
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.10.3-1~jessie"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "ajouve/infra-nginx:dev",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "175272649c9a9c5abbfde7516328bdab5cb3825e1e027eee0580eb18f7ff77cb",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/default",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "host": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3e04a4c12b5a5b3f55e7b4e918dadec64806b5c926fc249e8aa3e28398a02954",
                    "EndpointID": "7de54daaa31230c9492a463792015af727e9562eaacbaa0c2d70cdc3d3b04236",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

【问题讨论】:

  • ERR_CONNECTION_REFUSED 在哪里?
  • curl http://website.dev/,我更新了帖子
  • docker ps 的输出添加到您的问题中
  • 更新,容器正在运行,没有使用network host的端口
  • 进入容器并运行 netstat -plant 并添加该输出

标签: node.js docker nginx proxy


【解决方案1】:

因此,当您使用 --net host 运行它时,它实际上仍然不在 mac 网络上。这就是它不起作用的原因。

来自documentation

Mac 有一个不断变化的 IP 地址(如果您没有网络访问权限,则没有)。从 17.06 开始,我们的建议是连接到特殊的 Mac 专用 DNS 名称 docker.for.mac.localhost,它将解析为主机使用的内部 IP 地址。

所以你需要将你的配置更改为

worker_processes 4;
pid /run/nginx.pid;

events {}

http {

    server {
      listen 80;
      server_name localhost;
      location / {
          return 200 'gangnam style!';
      }
    }

    server {
      listen 80;
      server_name website.dev;
      location / {
          proxy_pass    http://docker.for.mac.localhost:3000;
          proxy_set_header X-Base-Path "/";
          proxy_set_header Website-Name "test";
      }
    }
}

你应该像下面这样运行容器

docker run --name infra-nginx -p 80:80 -d docker-registry.host.com:5000/infra-nginx:dev

【讨论】:

    【解决方案2】:

    你把它弄得太复杂了。只需从您的应用容器中公开一个端口,例如 8080。

    在 nginx 中使用:

    listen 80;
    server_name _;
    
    location / {
       proxy_pass http://127.0.0.2:8080;
    }
    

    【讨论】:

    • 节点应用程序没有运行到容器中,我无法从容器中轻松访问我的 ip
    【解决方案3】:

    好的,我认为我所做的解决方案很简单。

    首先,我在本地机器的 3000 端口上运行了一个 nodejs 应用程序,所以如果我在浏览器中访问 localhost:3000,我可以看到该应用程序正在运行。

    然后我在我的机器~/projects/docker/default.conf 的文件夹中创建一个名为default.conf 的文件,您可以在任何地方创建该文件。并将这段代码粘贴到文件中:

    server {
    listen 80;
    
    server_name myapp.com;
    
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    

    }

    查看proxy_pass http://localhost:3000; 行,我正在重定向到在本地计算机上运行的应用程序。

    然后我使用以下命令运行nginx container

    sudo docker run -d -p 80:80 --name="nginx" --net="host" -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro  nginx
    

    -p 80:80 是将该端口绑定到nginx container

    --net="host"是告诉容器它要使用的网络和宿主机是一样的,意思和我的机器一样,这样我就可以从容器内部转发到localhost:3000了。

    -v ~/projects/docker/default.conf:/etc/nginx/conf.d/default.conf:ro 是告诉nginx,它的配置文件将是我在本地机器上的文件。

    意思是这样的:你会在这个~/projects/docker/default.conf位置找到这个/etc/nginx/conf.d/default.conf文件

    仅此而已。

    如果我在浏览器中输入localhost (没有端口),它将通过nginx container 并被重定向到正在运行的应用程序在我本地机器的localhost:3000

    如果这对你有帮助,请告诉我

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 2015-01-05
      • 2016-11-16
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多