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

Blocking all ports except 80 and 443 with cloudflare

· 52CE 我爱测

I try to block all ports except 22, 80, and 443. I added the following rules to iptables . *filter :INPUT ACCEPT [36878:18003219] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [33947:26518456] -A INPUT -m state --state ESTABLISHED,RELATED…

问题原文

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

I try to block all ports except 22, 80, and 443. I added the following rules to iptables.

*filter
:INPUT ACCEPT [36878:18003219]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [33947:26518456]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -s MY_IP -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT 
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT 
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT 
-A INPUT -j DROP
COMMIT

Port 22 is working, but Cloudflare cannot connect to the webserver. -A INPUT -j DROP causes the problem. In fact, 443 connection does not match a rule until the end of the list.

已采纳解答

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

So after a lengthy comment session, here is what we've put together:

When you enabled your custom firewall, CloudFlare reported a 504 Gateway Timeout. But notably, it said that that was the error it received from your server, not that it timed out trying to reach your server. The difference is subtle but important: This means CloudFlare was talking to your server fine, but your server was not talking to itself.

You have a web server that proxies to an internal web application running on localhost. But your custom firewall did not allow localhost connections. This is required for the web server to talk to the web app, as well as for so many other internal services to talk to each other, that every professional firewall builder you ever may use will simply allow localhost traffic without question.

(PS: You should leave ICMP enabled to prevent other sorts of breakage, such as Path MTU Discovery.)

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

资料来源与适用范围

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

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

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