共计 2036 个字符,预计需要花费 6 分钟才能阅读完成。
导读 | 这篇文章主要介绍了 Nginx 禁止 ip 加端口访问的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值, 需要的朋友可以参考下 |
Nginx 禁止 IP 加端口访问
使用 iptables 限制对应端口,再利用 Nginx 将 80 端口转发到对应端口
CentOS7 默认的防火墙是 firewalle,先看看服务器中有没有安装 iptables
[ | ]|
Redirecting to /bin/systemctl status iptables.service | |
Unit iptables.service could not be found. |
安装 iptables
yum install -y iptables
关闭自带的防火墙 firewalld
# 停止 firewalld 服务 | |
systemctl stop firewalld | |
# 禁用 firewalld 服务 | |
systemctl mask firewalld |
服务命令
# 启动 iptables | |
systemctl start iptables.service | |
# 停止 iptables | |
systemctl stop iptables.service | |
# 重启 iptables | |
systemctl restart iptables.service |
禁止外部访问 8080 端口
iptables -I INPUT -p tcp --dport 8080 -j DROP
允许本机访问 8080 端口
iptables -I INPUT -s 127.0.0.1 -p tcp --dport 8080 -j ACCEPT
放行 80 端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT | |
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT |
到此,就可以使用域名直接访问 8080 端口了,并且 IP + 端口的方式,也已经无法访问
iptables 常用命令
# 查看 iptables 现有规则 | |
iptables -L -n | |
# 允许所有访问 | |
iptables -P INPUT ACCEPT | |
# 清空所有默认规则 | |
iptables -F | |
# 清空所有自定义规则 | |
iptables -X | |
# 所有计数器归 0 | |
iptables -Z | |
# 允许来自于 lo 接口的数据包 (本地访问) | |
iptables -A INPUT -i lo -j ACCEPT | |
# 开放 22 端口 | |
iptables -A INPUT -p tcp --dport 22 -j ACCEPT | |
# 开放 21 端口 (FTP) | |
iptables -A INPUT -p tcp --dport 21 -j ACCEPT | |
# 开放 80 端口 (HTTP) | |
iptables -A INPUT -p tcp --dport 80 -j ACCEPT | |
# 开放 443 端口 (HTTPS) | |
iptables -A INPUT -p tcp --dport 443 -j ACCEPT | |
# 允许 ping | |
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT | |
# 允许接受本机请求之后的返回数据 RELATED, 是为 FTP 设置的 | |
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT | |
# 其他入站一律丢弃 | |
iptables -P INPUT DROP | |
# 所有出站一律绿灯 | |
iptables -P OUTPUT ACCEPT | |
# 所有转发一律丢弃 | |
iptables -P FORWARD DROP | |
# 查看 iptables 现有规则 | |
iptables -L -n | |
# 允许所有访问 | |
iptables -P INPUT ACCEPT | |
# 清空所有默认规则 | |
iptables -F | |
# 清空所有自定义规则 | |
iptables -X | |
# 所有计数器归 0 | |
iptables -Z | |
# 允许来自于 lo 接口的数据包 (本地访问) | |
iptables -A INPUT -i lo -j ACCEPT | |
# 开放 22 端口 | |
iptables -A INPUT -p tcp --dport 22 -j ACCEPT | |
# 开放 21 端口 (FTP) | |
iptables -A INPUT -p tcp --dport 21 -j ACCEPT | |
# 开放 80 端口 (HTTP) | |
iptables -A INPUT -p tcp --dport 80 -j ACCEPT | |
# 开放 443 端口 (HTTPS) | |
iptables -A INPUT -p tcp --dport 443 -j ACCEPT | |
# 允许 ping | |
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT | |
# 允许接受本机请求之后的返回数据 RELATED, 是为 FTP 设置的 | |
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT | |
# 其他入站一律丢弃 | |
iptables -P INPUT DROP | |
# 所有出站一律绿灯 | |
iptables -P OUTPUT ACCEPT | |
# 所有转发一律丢弃 | |
iptables -P FORWARD DROP |
到此这篇关于 Nginx 禁止 ip 加端口访问的文章就介绍到这了。
正文完
星哥玩云-微信公众号
