docker里nginx转发其他nginx失败问题
最近在学习nginx
目标是:想通过docker中的nginx容器1转发到 nginx2的页面中,目前转发失败,--name mynignx的log报错如下
2023/09/15 16:15:55 [error] 22#22: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.168.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://172.168.0.2:90/", host: "localhost:89"
请问一下应该如何应对呢?
创建了两个目录,导入了相关配置文件以及映射。
mynignx
docker run --name mynignx --net mynet -e TZ="Asia/Shanghai" -d -p 89:80 -v /usr/local/docker/nginx/html:/usr/share/nginx/html -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx/log:/var/log/nginx nginx
mynignx2
docker run --name mynignx2 --net mynet -e TZ="Asia/Shanghai" -d -p 90:80 -v /usr/local/docker/nginx2/html:/usr/share/nginx2/html -v /usr/local/docker/nginx2/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx2/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx/log:/var/log/nginx nginx
创建了一个network 包含了这俩容器。
docker network inspect mynet
[
{
"Name": "mynet",
"Id": "15d6d83ce0705f497ce4ea53903b99ee621ad49af36bf0ae5f491c4547374c65",
"Created": "2023-09-15T15:43:49.123571543+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.168.0.0/16",
"Gateway": "172.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"96c13169bb0e0339563480ed8586ffd36abecf7cd5ca8659813da4b6bfe68872": {
"Name": "mynignx2",
"EndpointID": "5f45e8c63a5c40001acde55bf4021ff36eb86a37a912102c361139079fb28aa4",
"MacAddress": "02:42:ac:a8:00:02",
"IPv4Address": "172.168.0.2/16",
"IPv6Address": ""
},
"ee55a89ca011fd1c820d69f16713cb9ab39d994e3510d3843a50aff7a67839a1": {
"Name": "mynignx",
"EndpointID": "c5a478956482d17ddcb2ca0cf4520d7640156481d992de81d9ab686191256355",
"MacAddress": "02:42:ac:a8:00:03",
"IPv4Address": "172.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
补充 测试容器的连通性
docker exec 172.168.0.2 ping 172.168.0.3
Error response from daemon: No such container: 172.168.0.2
docker exec mynignx ping mynignx2
OCI runtime exec failed: exec failed: unable to start container process: exec: "ping": executable file not found in $PATH: unknown
docker exec mynignx curl mynignx2
返回的是mynignx2的html页面数据说明是连通的
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 264 100 264 0 0 388k 0 --:--:-- --:--:-- --:--:-- 257k
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<div>222222222222222222</div>
</body>
</html>
docker exec mynignx telnet mynignx2:90
OCI runtime exec failed: exec failed: unable to start container process: exec: "telnet": executable file not found in $PATH: unknown
补充:nginx配置conf
server {
listen 80;
# listen [::]:80;
server_name localhost;
location / {
proxy_pass http://172.168.0.2:90;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
172.168.0.3(mynignx)->通过conf的proxy_pass 请求 http://172.168.0.2:90/(mynignx2)
这条错误日志表示在服务器上收到了一个来自客户端 172.168.0.1 的 GET 请求,该请求要求连接到主机为 localhost,端口为 89 的上游服务器。然而,连接到上游服务器时遇到了连接拒绝的错误,导致无法完成请求。可能的原因包括上游服务器未运行或在指定端口未监听连接。您可以检查上游服务器的状态以及是否正确配置了端口监听来解决此问题。
改为--net=host ,模式
配置了两个映射,
http://test.player3.cn 映射到 本地localhsot
http://dev.player3.cn 映射到 本地localhsot
docker run --name mynignx --net=host -e TZ="Asia/Shanghai" -d -v /usr/local/docker/nginx/html:/usr/share/nginx/html -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx/log:/var/log/nginx nginx
docker run --name mynignx2 --net=host -e TZ="Asia/Shanghai" -d -v /usr/local/docker/nginx2/html:/usr/share/nginx/html -v /usr/local/docker/nginx2/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx2/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx2/log:/var/log/nginx nginx
mynignx的conf
server {
listen 80;
server_name test.player3.cn;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://dev.player3.cn:90/;
## root /usr/share/nginx/html;
## index index.html index.htm;
}
}
mynignx2的conf
server {
listen 90;
server_name dev.player3.cn;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
标签:--,nginx,usr,conf,转发,docker,local
From: https://www.cnblogs.com/q1359720840/p/17706146.html