Nginx禁止IP加端口访问使用iptables 限制对应端口,再利用Nginx将80端口转发到对应端口 CentOS7默认的防火墙是 firewalle ,先看看服务器中有没有安装 iptables [root@VM-0-3-centos ~]# service iptables status Redirecting to /bin/systemctl status iptables.service Unit iptables.service could not be found.
安装 iptables 关闭自带的防火墙 firewalld # 停止firewalld服务systemctl stop firewalld# 禁用firewalld服务systemctl mask firewalld 服务命令 # 启动iptablessystemctl start iptables.service# 停止iptablessystemctl stop iptables.service# 重启iptablessystemctl 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 ACCEPTiptables -A OUTPUT -p tcp --sport 80 -j ACCEPT 到此,就可以使用域名直接访问8080 端口了,并且IP + 端口 的方式,也已经无法访问 iptables 常用命令
# 查看iptables现有规则iptables -L -n# 允许所有访问iptables -P INPUT ACCEPT# 清空所有默认规则iptables -F# 清空所有自定义规则iptables -X# 所有计数器归0iptables -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# 允许pingiptables -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做什么 教你如何搭建一个时间服务器 |