大网数据广告活动
服务器运维问答

Ubuntu 18.04: Communication to server on localhost stops working after setting a port forwarding rule

· 52CE 我爱测

I have a Ubuntu 18.04 device with two network interfaces, ethernet and USB LTE modem. There is IP camera connected directly to the ethernet port and it has address 192.168.0.90 . I use the LTE interface for everything else - inte…

问题原文

原帖链接 · muliku · CC BY-SA 4.0

I have a Ubuntu 18.04 device with two network interfaces, ethernet and USB LTE modem. There is IP camera connected directly to the ethernet port and it has address 192.168.0.90. I use the LTE interface for everything else - internet, ssh, etc. - it has static IP address 172.10.0.10 (our company has its own APN).

My device has a Python app running that communicates with local Node.JS server on http://localhost/abc that communicates to my server via the LTE interface to https://myserver.com/app

The IP camera has its own web server for configuration purposes. In order to get to the camera's config page I set a port forward rule so I can access the IP camera on 172.10.0.10:8888:

sudo sysctl net.ipv4.ip_forward=1
sudo iptables -t nat -A PREROUTING -p tcp --dport 8888 -j DNAT --to-destination 192.168.0.90:80
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

It works fine and I can access the camera and shell all good. But the Python app stops communicating with the local Node.JS server with this error:

502 Server Error: Bad Gateway for url: http://localhost/abc My guess is that my port forward rule broke some ports that those two local apps were communicating trough. Any idea on how to fix that?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

Your MASQUERADE rule is wrong. It needs to specify the outbound interface, otherwise it will try to NAT all traffic. Since you have two interfaces to the Internet, it is OK to specify it twice. Each will apply only to traffic exiting that interface. For example:

iptables -t nat -A POSTROUTING -o enp4s0 -j MASQUERADE
iptables -t nat -A POSTROUTING -o wwp0s20f0u3 -j MASQUERADE

If you still get 502 errors after fixing this, check that your web application is actually running and listening on the port you expect. The usual cause of this error is the app is not running or listening on a different port.

P.S. If you are not working for La Jolla Baking Company in Plano, Texas, USA, you should use a different IP address block. They own the global 172.10.0.10 address you used in your question. Using other people's global IP addresses can also cause problems.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-08-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。