介绍
您好,我在为测试网站设置 Nginx 时遇到了问题。我有一个主机和一个虚拟机,虚拟机上的 docker 应用程序在端口 3000 上运行。
问题
现在当我执行 192.168.1.22:4444 (HOST MACHINE) 时,会出现 502 Bad Gateway。此外,似乎没有数据包到达我设置的 VM。
tcpdump -i eth0 'port 4444'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C
0 packets captured
2 packets received by filter
0 packets dropped by kernel
但是现在当我在浏览器中输入虚拟机的 IP(172.16.1.22)和服务端口(4444)时,我会收到数据包。
14:49:46.860869 IP .3458 > .4444: Flags [.], ack 1, win 229, options [nop,nop,TS val 211139829 ecr 9802156], length 0
14:49:46.860911 IP .3458 > .4444: Flags [.], ack 1, win 229, options [nop,nop,TS val 211139829 ecr 9802157], length 0
14:49:46.861545 IP .3458 > .4444: Flags [P.], seq 492:944, ack 244, win 237, options [nop,nop,TS val 211139829 ecr 9802133], length 452
14:49:46.862192 IP .3458 > .4444: Flags [P.], seq 1:451, ack 1, win 229, options [nop,nop,TS val 211139830 ecr 9802156], length 450
14:49:46.862248 IP .3458 > .4444: Flags [P.], seq 1:450, ack 1, win 229, options [nop,nop,TS val 211139830 ecr 9802157], length 449
14:49:46.862306 IP .4444 > .4444: Flags [.], ack 451, win 235, options [nop,nop,TS val 9802158 ecr 211139830], length 0
问题
我是否遗漏了某个配置,或者有人可以建议我如何解决这个问题?
Nginx /etc/nginx/conf.d/default.conf
http{
server {
listen 4444;
server_name vm3;
location / {
proxy_pass http://172.16.1.22;
# re-send the host header - this may not be necessary
proxy_set_header Host $host;
# set the X-Forwarded-For header, so that the public IP of the client is available to the backend server
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
/var/log/nginx/error.log
2019/05/26 17:09:02 [error] 12276#12276: *81 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.1.22, server: _, request: "GET / HTTP/1.1", upstream: "http://172.16.1.22:80/", host: "192.168.1.10:32322"
答案1
确保指定上游端口。
http{
server {
listen 4444;
server_name vm3;
location / {
proxy_pass http://172.16.1.22:3000;
# re-send the host header - this may not be necessary
proxy_set_header Host $host;
# set the X-Forwarded-For header, so that the public IP of the client is available to the backend server
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
答案2
就我而言,端口已关闭,我通过 telnet 检查,配置防火墙后,错误消失