Solve the problem that when the Alibaba Cloud server is deployed, it can access port 80, but cannot access other ports such as 8080 and 8888

  • First check if there is a problem with the configuration

For example, listen to port 8888 on Alibaba Cloud

  1. Open the Nginx configuration file. Usually by vim /etc/nginx/nginx.confopening a configuration file.
  2. Find serverthe block in the configuration file, listen:80modify the port to listen:8888, and save the modified file.
server {
    
    
    listen 8888;
    server_name example.com;
    ...
}
  1. Add security group rules in Alibaba Cloud Console -> Security Group: protocol: TCP, port range: 8888, authorized object: 0.0.0.0/0 or specify the public IP address you allow.
  2. Run the command nginx -tto check the modified configuration file for syntax errors.
  3. Restart nginx.

However, after modifying the configuration in the Alibaba Cloud server according to the above steps, it is still impossible to access other deployment ports.

Execute check iptables rules to view all rules

iptables -L

insert image description here

这是 centos 操作系统中的 iptables 防火墙规则,用于控制网络流量的传入、传出和转发。下面解释每个部分的含义:

Chain INPUT (policy ACCEPT)
这是一个输入链,用于处理传入流量。

ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
这条规则允许所有与已建立的或相关的连接状态相关的数据包通过。

ACCEPT icmp -- anywhere anywhere
这条规则允许来自任何源地址的 ICMP (Ping) 请求通过。

ACCEPT all -- anywhere anywhere
这条规则允许所有流量通过,不受限制。

ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
这条规则允许新的 TCP 连接通过 SSH 端口 (默认为22)。

ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp
这条规则允许新的 TCP 连接通过 FTP 端口 (默认为21)。

ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
这条规则允许新的 TCP 连接通过 HTTP 端口 (默认为80)。

REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
此规则将拒绝所有其他流量,并返回 "icmp-host-prohibited" 的 ICMP 拒绝消息。

Chain FORWARD (policy ACCEPT)
这是一个转发链,用于处理流量转发。

REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 此规则将拒绝所有转发流量,并返回 "icmp-host-prohibited" 的 ICMP 拒绝消息。
Chain OUTPUT (policy ACCEPT)
这是一个输出链,用于处理传出流量。

ACCEPT all -- anywhere anywhere 允许所有的传出流量通过。
这些 iptables 规则定义了如何处理传入、传出和转发的网络流量,并可根据需要进行修改。请注意,这只是一个示例规则集,实际的规则集可能会根据特定的网络配置和安全要求有所不同。
要将防火墙规则从拒绝改为允许,您可以使用以下命令:

iptables -D INPUT -j REJECT --reject-with icmp-host-prohibited
iptables -A INPUT -j ACCEPT

这两个命令将删除原来的拒绝规则,然后添加一个允许所有流量通过的规则。请确保以管理员权限运行这些命令。

解释:

第一个命令 -D INPUT -j REJECT --reject-with icmp-host-prohibited 会从 INPUT 链中删除之前的拒绝规则。
第二个命令 -A INPUT -j ACCEPT 会在 INPUT 链中添加一个允许所有流量通过的规则。
请注意,在更改防火墙规则之前,请确保您了解风险并采取适当的安全措施。通过允许所有的流量,您可能会暴露您的系统和网络于潜在的安全威胁

After executing the above command

insert image description here

Finally, restart the nginx service to access other ports.

Guess you like

Origin blog.csdn.net/qq_36660135/article/details/131642981