linux - 如何设置 iptables 以允许 Web 流量传输到我的 Spring Boot 应用程序?

标签 linux spring-boot vps iptables

我正在尝试在 Arvixe 的 VPS Lite 上托管我的 Spring Boot 应用程序。没有 CPanel,只有命令行。

当我启动 Spring Boot 应用程序并导航到服务器的 IP 地址时,我看到 ERR_CONNECTION_REFUSED 错误。

这是我的 iptables 配置。我按照此处找到的步骤http://crm.vpscheap.net/knowledgebase.php?action=displayarticle&id=29进行操作

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT
-A INPUT -p udp -i eth0 --sport 53 -j ACCEPT
-A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

-A INPUT -j DROP
COMMIT

这是上面创建的策略

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere            ctstate RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http state NEW,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https state NEW,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh state NEW,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:ssh state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:https state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:http state ESTABLISHED
ACCEPT     udp  --  anywhere             anywhere            udp spt:domain
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http limit: avg 25/min burst 100
DROP       all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:http state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:https state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:ssh state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh state NEW,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https state NEW,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http state NEW,ESTABLISHED
ACCEPT     udp  --  anywhere             anywhere            udp dpt:domain

最佳答案

在 iptables 配置中,您打开端口 80 和 443,但 spring-boot 应用程序默认在端口 8080 上启动。所以你有两个选择:

  1. 通过将 --server.port=80 添加到启动参数或在 application.properties 中设置 server.port=80 来启动端口 80 上的应用程序列表。这可以工作,但您必须以 root 身份启动应用程序,因为它尝试绑定(bind)到众所周知的端口。我不会推荐这个。
  2. 通过在 iptables 配置中添加如下行,使用 iptables 将外部端口 80 重定向到内部转发到端口 8080

    iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
    

关于linux - 如何设置 iptables 以允许 Web 流量传输到我的 Spring Boot 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31166228/

相关文章:

linux - 在 sed 中编程 rev

php - 如何在 Dreamhost 上为多个域创建共享文件夹

java - 我在 Windows 计算机上制作 java 程序然后在 Linux vps 上运行它会遇到问题吗?

python - 如何创建一个脚本来获取用户输入并在使用 python 3.6 的终端命令中使用它?

linux bash 计算文件夹中文件的大小

java - 如何使用 Spring Boot(服务器端)和 Angular(前端)将 100K 数据加载到表中

java - Spring Data JPA PagingAndSortingRepository 对加密数据而不是解密数据进行排序

java - swagger 2 spring boot - 有2个同名的API,但只显示其中之一

linux - 如何计算虚拟机中的 I/O 路径?

apache - 仅 Nginx 或 Nginx(反向代理)+ Apache