如何在Ubuntu20.04上使用Apache或Nginx保护Tomcat10

来自菜鸟教程
跳转至:导航、​搜索

作为 Write for DOnations 计划的一部分,作者选择了 Free and Open Source Fund 来接受捐赠。

介绍

Apache Tomcat 是一个 Web 服务器和 servlet 容器,用于为 Java 应用程序提供服务。 它是 Jakarta Servlet、Jakarta Server Pages 和 Jakarta EE 平台的其他技术的开源实现。

安装后,Tomcat 默认提供未加密的流量,包括密码或其他敏感数据。 为了保护您的 Tomcat 安装,您将 Let's Encrypt TLS 证书集成到所有 HTTP 连接。 要合并 TLS,您可以使用已配置的证书设置反向代理,该代理将与客户端安全协商并将请求交给 Tomcat。

虽然可以在 Tomcat 本身中配置 TLS 连接,但不建议这样做,因为 Tomcat 没有可用的最新 TLS 标准和安全更新。 在本教程中,您将使用 Apache 或 Nginx 配置此连接。 它们都广泛使用且经过充分测试,而自动提供 Let's Encrypt 证书的软件,例如 certbot,不提供对 Tomcat 的支持。 如果您想同时尝试 Apache 和 Nginx,您将需要为每个单独的服务器。

先决条件

选项 1 – 使用 Apache 作为反向代理

部分先决条件

第 1 步 – 在 Apache 中配置虚拟主机

因为您在域中设置了一个基本虚拟主机,并在先决条件部分中使用 Let's Encrypt 对其进行了保护,所以您有两个可用的虚拟主机。 您只需要编辑配置 HTTPS 流量的那个。 要列出它们,请运行以下命令显示 Apache 配置:

sudo apache2ctl -S

输出将与此类似:

Output...
VirtualHost configuration:
*:443                  your_domain (/etc/apache2/sites-enabled/your_domain-le-ssl.conf:2)
*:80                   your_domain (/etc/apache2/sites-enabled/your_domain.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

提供 HTTPS 配置的文件是 /etc/apache2/sites-enabled/your_domain-le-ssl.conf。 通过运行以下命令打开它进行编辑,将 your_domain 替换为您的域名:

sudo nano /etc/apache2/sites-enabled/your_domain-le-ssl.conf

该文件将类似于以下内容:

/etc/apache2/sites-enabled/your_domain-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain.conf
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLCertificateFile /etc/letsencrypt/live/your_domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/your_domain/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

将突出显示的行添加到 VirtualHost:

/etc/apache2/sites-enabled/your_domain-le-ssl.conf

...
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8080/
    ProxyPassReverse / http://127.0.0.1:8080/
...

三个突出显示的指令指示 Apache 在保留 HTTP 标头值的同时启用 Tomcat 和外部世界之间的双向流量。 完成后,保存并关闭文件。

您现在已指示 Apache 将流量代理到您的 Tomcat 安装,但默认情况下未启用该功能。

第 2 步 - 测试新的 Apache 配置

在此步骤中,您将启用 mod_proxy 和 mod_proxy_http,它们是促进连接代理的 Apache 模块。 运行以下命令以启用它们:

sudo a2enmod proxy
sudo a2enmod proxy_http

然后,通过键入以下内容检查配置:

sudo apache2ctl configtest

输出应以 Syntax OK 结尾。 如果有任何错误,请查看您刚刚修改的配置。

最后,重启 Apache Web 服务器进程:

sudo systemctl restart apache2

当您从 Web 浏览器访问 your_domain 时,您现在应该会看到使用 TLS 证书保护的 Tomcat 安装。 您可以跳过 Nginx 部分并按照限制访问 Tomcat 的步骤进行操作。

选项 2 – 使用 Nginx 作为反向代理

部分先决条件

第 1 步 – 调整 Nginx 服务器块配置

在此步骤中,您将修改在先决条件部分中创建的域的服务器块配置,以使 Nginx 能够识别 Tomcat。

使用以下命令打开配置文件进行编辑:

sudo nano /etc/nginx/sites-available/your_domain

在文件顶部添加以下行:

/etc/nginx/sites-available/your_domain

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}

upstream 块定义了如何连接到 Tomcat,它让 Nginx 知道 Tomcat 的位置。

接下来,在为端口 443 定义的 server 块中,将 location / 块的内容替换为突出显示的指令:

/etc/nginx/sites-available/your_domain

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
...
    location / {
        include proxy_params;
        proxy_pass http://tomcat/;
    }
...

这两行指定所有流量都应该转到您刚刚定义的名为 tomcat 的 upstream 块。 您还订购了在代理过程中保留的所有 HTTP 参数。 完成后保存并关闭文件。

现在,您将通过访问域中的 Tomcat 来测试此配置。

第 2 步 – 测试新的 Nginx 配置

要测试您的配置更改没有引入语法错误,请运行以下命令:

sudo nginx -t

输出应如下所示:

Outputnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果您看到任何错误,请返回上一步查看您修改的配置。

然后,重启 Nginx 以重新加载配置:

sudo systemctl restart nginx

现在,当您在 Web 浏览器中访问域的 TLS 安全变体时,您可以看到您的 Tomcat 安装:

https://your_domain

限制 Apache 或 Nginx 对 Tomcat 的访问

现在您已经通过具有 TLS 证书的代理服务器在您的域中公开了您的 Tomcat 安装,您可以通过限制对它的访问来强化您的 Tomcat 安装。

对 Tomcat 的所有 HTTP 请求都应通过代理,但您可以将 Tomcat 配置为仅侦听本地环回接口上的连接。 此配置可确保外部各方无法尝试直接向 Tomcat 发出请求。

打开 server.xml 文件进行编辑(位于 Tomcat 配置目录中):

sudo nano /opt/tomcat/conf/server.xml

在 Service 下找到名为 Catalina 的 Connector 定义,如下所示:

/opt/tomcat/conf/server.xml

...
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
...

要限制对本地环回接口的访问,请将 127.0.0.1 指定为 address 参数:

/opt/tomcat/conf/server.xml

...
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               address="127.0.0.1"
               redirectPort="8443" />
...

完成后,保存并关闭文件。

要进行更改,请运行以下命令重新启动 Tomcat:

sudo systemctl restart tomcat

您的 Tomcat 安装现在应该只能通过您的 Apache 或 Nginx Web 服务器代理访问。 Apache 或 Nginx 将 Tomcat 与 Internet 屏蔽,因此您的域中的 8080 端口将无法访问它。

结论

在本教程中,您将在使用免费 Let's Encrypt TLS 证书保护的代理服务器后面设置 Tomcat。 您还通过限制通过本地环回接口 (localhost) 与 Tomcat 的连接来禁止直接外部访问,这意味着只有本地应用程序(例如 Apache 或 Nginx)可以连接。

虽然配置单独的 Web 服务器进程可能会增加为应用程序提供服务所涉及的软件,但它简化了保护 Tomcat 流量的过程。 有关代理流量的更多信息,请访问 Apache 和 Nginx 的官方文档。