- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序(服务器)正在监听端口 8080。我希望能够将端口 80 转发给它,以便点击 http://localhost
解析我的应用程序(在 本地主机:8080
)。
这应该适用于任何端口映射(例如 80:8080
=> P_src:P_target
),并使用现代 *nix 机器(例如 Ubuntu)的最佳实践.
注意这一切都是在本地完成的,因此不需要接受除本地主机之外的任何人的连接。
最佳答案
经过大量搜索后,我发现答案是使用 iptables、设置 NAT 并使用内置的 PREROUTING 和 OUTPUT。
首先,您必须启用端口转发:
echo "1">/proc/sys/net/ipv4/ip_forward
然后,您必须使用您自己的 ${P_src}
和 ${P_target}
值将以下规则添加到 iptables NAT 表中:
iptables -t nat -A PREROUTING -s 127.0.0.1 -p tcp --dport ${P_src} -j REDIRECT --to ${P_target}`
iptables -t nat -A OUTPUT -s 127.0.0.1 -p tcp --dport ${P_src} -j REDIRECT --to ${P_target}`
如果您想删除规则,只需对每个规则使用 -D
开关而不是 -A
即可。
我为此构建了一个很好的小脚本,用于添加和删除映射。
#!/bin/bash
#
# API: ./forwardPorts.sh add|rm p1:p1' p2:p2' ...
#
# Results in the appending (-A) or deleting (-D) of iptable rule pairs that
# would otherwise facilitate port forwarding.
#
# E.g
# sudo iptables -t nat -A PREROUTING -s 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8080
# sudo iptables -t nat -A OUTPUT -s 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8080
#
if [[ $# -lt 2 ]]; then
echo "forwardPorts takes a state (i.e. add or rm) and any number port mappings (e.g. 80:8080)";
exit 1;
fi
case $1 in
add )
append_or_delete=A;;
rm )
append_or_delete=D;;
* )
echo "forwardPorts requires a state (i.e. add or rm) as it's first argument";
exit 1; ;;
esac
shift 1;
# Do a quick check to make sure all mappings are integers
# Many thanks to S.O. for clever string splitting:
# http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
for map in "$@"
do
IFS=: read -a from_to_array <<< "$map"
if [[ ! ${from_to_array[0]} =~ ^-?[0-9]+$ ]] || [[ ! ${from_to_array[1]} =~ ^-?[0-9]+$ ]]; then
echo "forwardPorts port maps must go from an integer, to an integer (e.g. 443:4443)";
exit 1;
fi
mappings[${#mappings[@]}]=${map}
done
# We're shooting for transactional consistency. Only manipulate iptables if all
# the rules have a chance to succeed.
for map in "${mappings[@]}"
do
IFS=: read -a from_to_array <<< "$map"
from=${from_to_array[0]}
to=${from_to_array[1]}
sudo iptables -t nat -$append_or_delete PREROUTING -s 127.0.0.1 -p tcp --dport $from -j REDIRECT --to $to
sudo iptables -t nat -$append_or_delete OUTPUT -s 127.0.0.1 -p tcp --dport $from -j REDIRECT --to $to
done
exit 0;
关于iptables - 如何使用iptables进行本地端口转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28170004/
我已经为启用了完整 netfilter 功能的 android 模拟器编译了 Linux。从源代码构建 android 后得到一个 iptables 二进制文件。 当我将这个二进制文件推送到模拟器时
由于大量机器人每秒访问我的 Web 服务器太多次,我进入我的服务器并发出以下命令,试图在 5 秒内将连接限制为最多 25 个。我可能需要更严格,但我不是因为我有一个很少访问的页面,它请求 50 个图像
我正在尝试阻止在短时间内打开大量连接的网络机器人。我正在使用这个语法: -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set
我正在尝试在 iptables 中记录一些来自恶意 IP 地址的丢弃数据包,这些 IP 地址不断攻击我的服务器。 来自这个恶意 IP 的所有内容都被丢弃,我不再在 Web 服务器日志中看到它,这是一件
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我已经设置了 keepalived,只有当我停止 iptables 服务时它才能正常工作。我的iptables配置是这样的,请告诉我应该为keepalived添加什么规则 # Firewall con
我使用 DevStack 在 RHEL6 上安装了 OpenStack,并且运行良好。有一天,我们的一位“系统管理员”注意到 iptables 正在系统上运行,并决定将其关闭(chkconfig ip
测试环境为: 2 操作系统 ubuntu server 10.04 安装在 VirtualBox 上 iptables v1.4.4 已加载 ip_conntrack 模块 这些是我的测试规则:
当我清除 IPtables 然后添加以下规则时,传入连接可以毫无问题地连接到端口 1234 上的我的 KVM VM。 -A PREROUTING -i br0 -p tcp -m tcp --dpor
我正在为我的工作使用 iptables 最近的匹配项,因为它保存了 ip 地址和我需要的最后一次看到的值。 但是现在我需要从 iptables 最近的列表中删除一些条目,这些条目位于 ipset 中。
所以我加了 sudo iptables -t raw -A PREROUTING -p tcp --dport 25 -j TRACE 也 sudo iptables -t raw -A OUTPUT
我在专用的 ubuntu 服务器上有一个防火墙(基于 iptables)。我有几个 LAN 客户端。 在我的一个 LAN 客户端上,我正在运行可以根据 IP 限制访问的软件。对我来说,重要的是我可以通
我不知道如何解决我的问题。 是否可以使用 ipq_set_verdict() 重定向捕获的数据包? 我想将未经授权的用户重定向到登录页面。 请看我的代码: 数据包被接受,我的浏览器打开请求的页面(未更
我正在尝试使用字符串匹配模块获取“from”和“to”偏移量以在我的 iptables 规则中使用。这是我从 tcpdump 工具输出的数据包: listening on eth0, link-typ
我正在尝试使用 python-iptables 编写脚本来设置某些规则。我弄清楚了如何设置规则以允许所有连接和拒绝所有连接,但我需要弄清楚如何编写规则以允许已建立的连接。 例如,我需要使用 pytho
这是规则集: #!/bin/sh iptables-restore -v<
我正在尝试将数据从 xml 加载到 iptables。我正在使用以下命令: xsltproc /usr/share/iptables/iptables.xslt myiptable.xml | ipt
我正在尝试设置 iptable 规则,但在使用 iptable 时收到以下错误消息: iptables v1.4.14: can't initialize iptables table `nat':
我正在从 Windows Bash 运行 Ubuntu 18.04: uname -a Linux DESKTOP-M87DGAS 4.4.0-17134-Microsoft #112-Microso
有一个 IP(来自中国)试图下载我的整个网站。它下载我所有的页面并显着加载服务器(我有超过 500 000 个页面)。查看访问日志,我可以看出它绝对不是 Google 机器人或任何其他搜索引擎机器人。
我是一名优秀的程序员,十分优秀!