教你如何解決Nginx禁止ip加端口訪問的問題
Nginx禁止IP加端口訪問
使用iptables 限制對應端口,再利用Nginx將80端口轉(zhuǎn)發(fā)到對應端口
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
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 + 端口的方式,也已經(jīng)無法訪問
iptables常用命令
# 查看iptables現(xiàn)有規(guī)則 iptables -L -n # 允許所有訪問 iptables -P INPUT ACCEPT # 清空所有默認規(guī)則 iptables -F # 清空所有自定義規(guī)則 iptables -X # 所有計數(shù)器歸0 iptables -Z # 允許來自于lo接口的數(shù)據(jù)包(本地訪問) 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 # 允許接受本機請求之后的返回數(shù)據(jù) RELATED,是為FTP設置的 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 其他入站一律丟棄 iptables -P INPUT DROP # 所有出站一律綠燈 iptables -P OUTPUT ACCEPT # 所有轉(zhuǎn)發(fā)一律丟棄 iptables -P FORWARD DROP
到此這篇關于Nginx禁止ip加端口訪問的文章就介紹到這了,更多相關Nginx禁止ip端口訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx安裝lua-nginx-module模塊的方法步驟
ngx_lua_module 是一個nginx http模塊,這篇文章主要介紹了Nginx安裝lua-nginx-module模塊的方法步驟,非常具有實用價值,需要的朋友可以參考下2018-12-12
Nginx出現(xiàn)403?Forbidden的幾種簡單解決方式
這篇文章主要介紹了Nginx出現(xiàn)403?Forbidden的幾種解決思路,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

