最近在学习nginx
目标是:x想通过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
配置了两个映射,
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;
}
}
问题虽然解决了,但是是属于换了其他的网络模式访问的,这个上面问题还是没有根除掉,希望大佬解惑一下。