为了反向代理到我的本地应用程序,这个 nginx(dockerized)配置有什么问题?

为了反向代理到我的本地应用程序,这个 nginx(dockerized)配置有什么问题?

我的目标是使用 nginx 容器反向代理在我的 vps 中运行的应用程序的特定路径。我已通过手动设置 nginx 容器成功完成此操作,但使用 docker-compose 时则无法完成。

手动运行 nginx 容器(成功):

sudo docker run -it -d=true --restart=unless-stopped --name=nginx -p=80:80 nginx

然后更新:nginx:/etc/nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # custom
    location /ping {
        proxy_pass http://my-vps-bla-bla.com:13671/ping;
    }
}

通过此配置,我可以成功调用:

  • curl --location 'http://my-vps-bla-bla.com'✅(nginx 默认欢迎响应)
  • curl --location 'http://my-vps-bla-bla.com/ping'✅(我的服务回应)

使用docker-compose中的nginx(不成功):

sudo docker-compose up -d使用以下内容docker-compose.yaml

version: "3"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./data/nginx/conf.d/:/etc/nginx/conf.d/

(安装方法nginx:/etc/nginx/conf.d/default.conf同上)

/ping使用此配置,无法调用我的 vps

  • curl --location 'http://my-vps-bla-bla.com'
  • curl --location 'http://my-vps-bla-bla.com/ping'
<body>
    <h1>An error occurred.</h1>
    <p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>

我尝试比较两个输出,sudo docker inspect nginx但我不知道该查找/修复哪里。以下是两个输出:

sudo docker inspect nginx从“手动” nginx 容器设置

[
    {
        "Id": "b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39",
        "Created": "2023-11-30T17:13:10.075669841Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1928612,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-11-30T17:13:10.394942106Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a6bd71f48f6839d9faae1f29d3babef831e76bc213107682c5cc80f0cbb30866",
        "ResolvConfPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/hostname",
        "HostsPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/hosts",
        "LogPath": "/var/lib/docker/containers/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39/b9e77c86868575a83f63420c3dbf542dbf9bca1c49a649a489f917055bfe5c39-json.log",
        "Name": "/nginx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "unless-stopped",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                24,
                176
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9-init/diff:/var/lib/docker/overlay2/ccf450a15bc9b7180010e6d2e409a3c9df4979248c75426c60f1e9d161f868d5/diff:/var/lib/docker/overlay2/63815499c80141a4d1af5a8e2c142b4087e32f8f86455372dda5825470a8980a/diff:/var/lib/docker/overlay2/43497557d17d33626e4dedaef8ba71c1e379a4fb134ea97e1c706ed08a0656de/diff:/var/lib/docker/overlay2/6f03359d7e49862599ec61b7f085af5ddc2edb3a30dec2a9bc8812853ef955c7/diff:/var/lib/docker/overlay2/df62ce4c1da140df692bffc5dc5ba2db38b14497b0c8f6a586aa33d685923c7e/diff:/var/lib/docker/overlay2/b53502919ddaf322d2433b5c3194c0d04ab45d838f64fb1e8ca9ab363a77082c/diff:/var/lib/docker/overlay2/d9166dd4fa2cef1a1bed1401e4e8ea1745f1a8a4d50f5d2924d8c54bed019c4a/diff",
                "MergedDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/merged",
                "UpperDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/diff",
                "WorkDir": "/var/lib/docker/overlay2/fbf2e1d00d7fcad7ebb6a270d58090e19c99a07d9957e6c82e537818067043c9/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "b9e77c868685",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.25.3",
                "NJS_VERSION=0.8.2",
                "PKG_RELEASE=1~bookworm"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "0c9b693da3685ad37e758da8e67d5d537e744c77bba0c13b2b2fa95c37eeede8",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/0c9b693da368",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "7beba34d7a1c3739dadf6b14856295e1849007e86792c70e2d76483d94a00b4e",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "aa:bb:cc:dd:ee:ff,
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "751980ff5b2110b5c0072ddd62e953a5e6575819e3a25319868474ee4b530e65",
                    "EndpointID": "7beba34d7a1c3739dadf6b14856295e1849007e86792c70e2d76483d94a00b4e",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "aa:bb:cc:dd:ee:ff",
                    "DriverOpts": null
                }
            }
        }
    }
]

sudo docker inspect docker-stuff_default从 docker-compose nginx 容器设置

[
    {
        "Id": "2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f",
        "Created": "2023-11-30T17:09:18.381573448Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1928407,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-11-30T17:09:18.752743033Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a6bd71f48f6839d9faae1f29d3babef831e76bc213107682c5cc80f0cbb30866",
        "ResolvConfPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/hostname",
        "HostsPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/hosts",
        "LogPath": "/var/lib/docker/containers/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f/2129e96a18fcd494e1f4a6113aa95e3fd7db72fee9a8686a9e28b2d3be100c3f-json.log",
        "Name": "/docker-stuff_nginx_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/ubuntu/projects/docker-stuff/data/nginx/conf.d:/etc/nginx/conf.d:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "docker-stuff_default",
            "PortBindings": {
                "443/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "443"
                    }
                ],
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "80"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "ConsoleSize": [
                0,
                0
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "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": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b-init/diff:/var/lib/docker/overlay2/ccf450a15bc9b7180010e6d2e409a3c9df4979248c75426c60f1e9d161f868d5/diff:/var/lib/docker/overlay2/63815499c80141a4d1af5a8e2c142b4087e32f8f86455372dda5825470a8980a/diff:/var/lib/docker/overlay2/43497557d17d33626e4dedaef8ba71c1e379a4fb134ea97e1c706ed08a0656de/diff:/var/lib/docker/overlay2/6f03359d7e49862599ec61b7f085af5ddc2edb3a30dec2a9bc8812853ef955c7/diff:/var/lib/docker/overlay2/df62ce4c1da140df692bffc5dc5ba2db38b14497b0c8f6a586aa33d685923c7e/diff:/var/lib/docker/overlay2/b53502919ddaf322d2433b5c3194c0d04ab45d838f64fb1e8ca9ab363a77082c/diff:/var/lib/docker/overlay2/d9166dd4fa2cef1a1bed1401e4e8ea1745f1a8a4d50f5d2924d8c54bed019c4a/diff",
                "MergedDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/merged",
                "UpperDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/diff",
                "WorkDir": "/var/lib/docker/overlay2/38441d680a06140f681240786942a4eee6fbd37eb2697db64be1647427bca63b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/ubuntu/projects/docker-stuff/data/nginx/conf.d",
                "Destination": "/etc/nginx/conf.d",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "2129e96a18fc",
            "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.25.3",
                "NJS_VERSION=0.8.2",
                "PKG_RELEASE=1~bookworm"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:latest",
            "Volumes": {
                "/etc/nginx/conf.d": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "58e1e3097c631d13e6eff6c524cfc92917aeb08954585d0a455d0908f3ba728c",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "docker-stuff",
                "com.docker.compose.project.config_files": "docker-compose.yaml",
                "com.docker.compose.project.working_dir": "/home/ubuntu/projects/docker-stuff",
                "com.docker.compose.service": "nginx",
                "com.docker.compose.version": "1.29.2",
                "maintainer": "NGINX Docker Maintainers <[email protected]>"
            },
            "StopSignal": "SIGQUIT"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "8b61c1e6e2f1a1650da0ea014baaa6d174f3a6ae29e589015bd6ef6de3b0dc3c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "443/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "443"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "443"
                    }
                ],
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/8b61c1e6e2f1",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "docker-stuff_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "nginx",
                        "2129e96a18fc"
                    ],
                    "NetworkID": "6aa6d52fb8c8945e6a36f5229892d14cb8e39e1dbf6fcf8c351464fd59ba83a3",
                    "EndpointID": "5f3010c7f1c9119c50cdff6f924470f7ceab64bc66b8d09f12d524ffa8b9b1ff",
                    "Gateway": "172.24.0.1",
                    "IPAddress": "172.24.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "aa:bb:cc:dd:ee:ff",
                    "DriverOpts": null
                }
            }
        }
    }
]

答案1

不同之处在于网络。从命令行,容器,如docker 文档

默认情况下,容器会继承主机的 DNS 设置,如 /etc/resolv.conf 配置文件中所定义。连接到默认桥接网络的容器会收到此文件的副本。连接到自定义网络的容器会使用 Docker 的嵌入式 DNS 服务器。嵌入式 DNS 服务器会将外部 DNS 查询转发到主机上配置的 DNS 服务器。

如您所见,docker-compose.yml 文件生成的容器使用自定义网络,因此您应该配置(在同一个 YAML 文件中的 dns: 下)所需的 DNS 配置或添加

network-mode: bridge

相关内容