教你如何解決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 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問靜態(tài)資源文件的實(shí)現(xiàn)
這篇文章主要介紹了nginx 多個(gè)location轉(zhuǎn)發(fā)任意請(qǐng)求或訪問靜態(tài)資源文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11使用nginx搭建點(diǎn)播和直播流媒體服務(wù)器的方法步驟
本篇文章主要介紹了使用nginx搭建點(diǎn)播和直播流媒體服務(wù)器的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Nginx之upstream被動(dòng)式重試機(jī)制的實(shí)現(xiàn)
本文主要介紹了Nginx之upstream被動(dòng)式重試機(jī)制的實(shí)現(xiàn),可以通過proxy_next_upstream來自定義配置,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03nginx外網(wǎng)訪問內(nèi)網(wǎng)站點(diǎn)配置操作
這篇文章主要介紹了nginx外網(wǎng)訪問內(nèi)網(wǎng)站點(diǎn)配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08基于Nginx實(shí)現(xiàn)HTTPS網(wǎng)站設(shè)置的步驟
本文主要介紹了Nginx實(shí)現(xiàn)HTTPS網(wǎng)站設(shè)置的步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Centos7.x下Nginx安裝及SSL配置與常用命令詳解
這篇文章主要介紹了Centos7.x下Nginx安裝及SSL配置與常用命令詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02