由於linux的系統限制,普通用戶是無法打開1024以下端口的,這裏面就包括http的默認端口80,這就使得很多用戶使用root權限來執行node,這帶來了不可預計的安全問題,所以這並不是一個好辦法。
其實我們可以使用iptables的端口轉發功能來解決這個問題:
1,首先將node的主程序綁定到高於1024端口,比如8090,這樣普通用戶就可以啓動這個http server了,只不過不是在默認的80端口上監聽;
2,配置iptables將80端口轉發到8090上,如下命令:
#iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8090
該命令的意思就是在iptable中添加一條端口轉發規則,如果刪除該規則,重新綁定,則先查找出:
#iptables --line-numbers --list PREROUTING -t nat
然後使用行號刪除:
#iptables -t nat -D PREROUTING 行號
3,記得添加8090端口到iptables的INPUT ACCEPT規則中
#iptables -I INPUT -p tcp --dport 8090 -j ACCEPT
4,第2步和第3步添加的這些規則,都是臨時性的,重啓服務器之後就無效了,所以需要保存起來
#/sbin/service iptables save
端口轉發配置完成。
補充:
經過上面的設置後,遠程使用默認80端口訪問網站沒有問題,但是在本機訪問就要包Connect Refused的錯誤,如:
[use@host ~]$ wget http://localhost
--2014-04-17 11:32:41-- http://localhost/
Connecting to localhost:80... failed: Connection refused.
這是因爲本地連接的端口轉發與遠程連接的不一樣,所以我們還要做如下設置:
#iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j DNAT --to 127.0.0.1:8090
#iptables -t nat -A OUTPUT -p tcp -d 本機IP --dport 80 -j DNAT --to 本機IP:8090
#/sbin/service iptables save
這樣設置之後,本機就可以使用默認端口了。
參考:
1,http://blog.csdn.net/pengyouchuan/article/details/11026531
2,http://www.liugj.com/2013/04/Iptables-NAT/