如果有些服務的 Port (連接埠)不符合預期,除了調整服務的 Port 以外也可以使用 nftables 來轉送請求。
下面示範將 443 Port 的請求轉發到本機的 8200 Port 中,讓外部可以使用 443 Port 來存取服務,這樣網頁 URL 連結就不需要特別 :8200 了。
安裝 nftables 套件:
設定開機自動啟動:
執行 nftables:
確認 nftables 服務正在執行:
修改 nftables 設定檔:
原始內容如下:
增加以下內容:
重新讀取設定值使其生效:
查看規則,確認設定正確:
範例輸出:
不過預設會禁止存取 127.0.0.1 相關的封包,需要調整以下設定才可開啟:
加入以下內容
重新讀取設定值使其生效:
下面示範將 443 Port 的請求轉發到本機的 8200 Port 中,讓外部可以使用 443 Port 來存取服務,這樣網頁 URL 連結就不需要特別 :8200 了。
安裝 nftables 套件:
sudo apt update
sudo apt install nftables -y
設定開機自動啟動:
sudo systemctl enable nftables
執行 nftables:
sudo systemctl start nftables
確認 nftables 服務正在執行:
sudo systemctl status nftables
修改 nftables 設定檔:
sudo vi /etc/nftables.conf
原始內容如下:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter;
}
chain forward {
type filter hook forward priority filter;
}
chain output {
type filter hook output priority filter;
}
}
增加以下內容:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter;
}
chain forward {
type filter hook forward priority filter;
}
chain output {
type filter hook output priority filter;
}
}
table ip nat {
chain PREROUTING {
type nat hook prerouting priority 0; policy accept;
tcp dport 443 dnat to 127.0.0.1:8500
}
chain POSTROUTING {
type nat hook postrouting priority 100; policy accept;
}
}
重新讀取設定值使其生效:
sudo nft -f /etc/nftables.conf
查看規則,確認設定正確:
sudo nft list table ip nat
範例輸出:
sudo nft list table ip nat
table ip nat {
chain PREROUTING {
type nat hook prerouting priority filter; policy accept;
tcp dport 443 dnat to 127.0.0.1:8200
}
chain POSTROUTING {
type nat hook postrouting priority srcnat; policy accept;
}
}
不過預設會禁止存取 127.0.0.1 相關的封包,需要調整以下設定才可開啟:
sudo vi /etc/sysctl.conf
加入以下內容
net.ipv4.conf.all.route_localnet = 1
重新讀取設定值使其生效:
sudo sysctl --system
留言
張貼留言
如果有任何問題、建議、想說的話或文章題目推薦,都歡迎留言或來信: a@ruyut.com