教你如何解決Nginx禁止ip加端口訪問的問題
Nginx禁止IP加端口訪問
使用iptables
限制對(duì)應(yīng)端口,再利用Nginx將80端口轉(zhuǎn)發(fā)到對(duì)應(yīng)端口
CentOS7默認(rèn)的防火墻是 firewalle
,先看看服務(wù)器中有沒有安裝 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
關(guān)閉自帶的防火墻 firewalld
# 停止firewalld服務(wù) systemctl stop firewalld # 禁用firewalld服務(wù) systemctl mask firewalld
服務(wù)命令
# 啟動(dòng)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
允許本機(jī)訪問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 # 清空所有默認(rèn)規(guī)則 iptables -F # 清空所有自定義規(guī)則 iptables -X # 所有計(jì)數(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 # 允許接受本機(jī)請(qǐng)求之后的返回?cái)?shù)據(jù) RELATED,是為FTP設(shè)置的 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
到此這篇關(guān)于Nginx禁止ip加端口訪問的文章就介紹到這了,更多相關(guān)Nginx禁止ip端口訪問內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Nginx服務(wù)器的配置中開啟文件Gzip壓縮的方法
這篇文章主要介紹了Nginx服務(wù)器的配置中開啟文件Gzip壓縮的方法,可以對(duì)CSS和JavaScript以及各種圖片等web傳輸?shù)奈募M(jìn)行壓縮,需要的朋友可以參考下2016-01-01nginx實(shí)現(xiàn)動(dòng)靜分離的方法示例
Nginx的靜態(tài)處理能力很強(qiáng),但是動(dòng)態(tài)處理能力不足,因此,在企業(yè)中常用動(dòng)靜分離技術(shù),本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2021-11-11Nginx安裝lua-nginx-module模塊的方法步驟
ngx_lua_module 是一個(gè)nginx http模塊,這篇文章主要介紹了Nginx安裝lua-nginx-module模塊的方法步驟,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-12-12Nginx啟動(dòng)失?。憾丝?0被占用問題的解決方案
在Linux服務(wù)器上部署 Nginx 時(shí),可能會(huì)遇到 Nginx 啟動(dòng)失敗的情況,尤其是錯(cuò)誤提示bind()to 0.0.0.0:80 failed,這種問題通常是由于端口80被其他進(jìn)程占用導(dǎo)致的,本文將詳細(xì)分析這一問題的原因,并提供多種解決方案,幫助你快速恢復(fù)Nginx的正常運(yùn)行2025-02-02Nginx出現(xiàn)403?Forbidden的幾種簡(jiǎn)單解決方式
這篇文章主要介紹了Nginx出現(xiàn)403?Forbidden的幾種解決思路,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12