如何在 Ubuntu 20.04 上使用 Apache 或 Nginx 确保 Tomcat 10 的安全

作者选择了 自由和开源基金作为 写给捐款计划的一部分接受捐款。

介绍

Apache Tomcat是一个用于服务Java(https://www.oracle.com/java/)应用程序的Web服务器和服务器容器,是Jakarta Servlet(https://jakarta.ee/specifications/servlet/),Jakarta Server Pages(https://jakarta.ee/specifications/pages/)和Jakarta EE(https://jakarta.ee/)平台其他技术的开源实现。

安裝後,Tomcat 會默認地提供未加密的流量,包括密碼或其他敏感數據。為了確保您的Tomcat 安裝,您將將 Let's Encrypt TLS 證書整合到所有 HTTP 連接中。

虽然TLS连接可以在Tomcat本身配置,但不建议,因为Tomcat没有最新的TLS标准和安全更新。 在本教程中,您将配置此连接使用Apache(https://httpd.apache.org/)或Nginx(https://nginx.org/)。

前提条件

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

部分前提条件

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

因为你在域中设置了一个基本的虚拟主机,并使用让我们加密在前提部分中加密,你有两个虚拟主机可用。你只需要编辑配置HTTPS流量的主机。

1sudo apache2ctl -S

结果将类似于此:

 1[secondary_label Output]
 2...
 3VirtualHost configuration:
 4*:443 your_domain (/etc/apache2/sites-enabled/your_domain-le-ssl.conf:2)
 5*:80 your_domain (/etc/apache2/sites-enabled/your_domain.conf:1)
 6ServerRoot: "/etc/apache2"
 7Main DocumentRoot: "/var/www/html"
 8Main ErrorLog: "/var/log/apache2/error.log"
 9Mutex watchdog-callback: using_defaults
10Mutex rewrite-map: using_defaults
11Mutex ssl-stapling-refresh: using_defaults
12Mutex ssl-stapling: using_defaults
13Mutex ssl-cache: using_defaults
14Mutex default: dir="/var/run/apache2/" mechanism=default
15PidFile: "/var/run/apache2/apache2.pid"
16Define: DUMP_VHOSTS
17Define: DUMP_RUN_CFG
18User: name="www-data" id=33
19Group: name="www-data" id=33

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

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

檔案將看起來像這樣:

 1[label /etc/apache2/sites-enabled/your_domain-le-ssl.conf]
 2<IfModule mod_ssl.c>
 3<VirtualHost *:443>
 4    ServerAdmin webmaster@localhost
 5    ServerName your_domain
 6    ServerAlias www.your_domain
 7    DocumentRoot /var/www/your_domain.conf
 8    ErrorLog ${APACHE_LOG_DIR}/error.log
 9    CustomLog ${APACHE_LOG_DIR}/access.log combined
10
11SSLCertificateFile /etc/letsencrypt/live/your_domain/fullchain.pem
12SSLCertificateKeyFile /etc/letsencrypt/live/your_domain/privkey.pem
13Include /etc/letsencrypt/options-ssl-apache.conf
14</VirtualHost>
15</IfModule>

将突出的行添加到VirtualHost:

1[label /etc/apache2/sites-enabled/your_domain-le-ssl.conf]
2...
3    CustomLog ${APACHE_LOG_DIR}/access.log combined
4
5    ProxyPreserveHost On
6    ProxyPass / http://127.0.0.1:8080/
7    ProxyPassReverse / http://127.0.0.1:8080/
8...

三个突出指令指示Apache允许Tomcat和外部世界之间的双向流量,同时保持HTTP标题值。

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

步骤 2:测试新的 Apache 配置

在此步骤中,您将启用mod_proxymod_proxy_http,这些是促进连接代理的Apache模块。

1sudo a2enmod proxy
2sudo a2enmod proxy_http

然后,通过键入检查配置:

1sudo apache2ctl configtest

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

最后,重新启动 Apache Web 服务器过程:

1sudo systemctl restart apache2

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

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

部分前提条件

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

在此步骤中,您将在先决条件部分中修改您创建的域的服务器封锁配置,以使 Nginx 意识到 Tomcat。

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

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

在文件的顶部添加下列行:

1[label /etc/nginx/sites-available/your_domain]
2upstream tomcat {
3    server 127.0.0.1:8080 fail_timeout=0;
4}

上游块定义了如何连接到Tomcat,这让Nginx知道Tomcat的位置。

接下来,在为端口443定义的服务器块中,用突出指令取代位置/块的内容:

 1[label /etc/nginx/sites-available/your_domain]
 2upstream tomcat {
 3    server 127.0.0.1:8080 fail_timeout=0;
 4}
 5
 6server {
 7...
 8    location / {
 9        include proxy_params;
10        proxy_pass http://tomcat/;
11    }
12...

这两行规定,所有的流量都应该去到你刚刚定义的名为tomcat上游块上。

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

步骤 2 – 测试新的 Nginx 配置

若要测试您的配置更改是否引入了语法错误,请运行此命令:

1sudo nginx -t

输出应该是这样的:

1[secondary_label Output]
2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3nginx: configuration file /etc/nginx/nginx.conf test is successful

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

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

1sudo systemctl restart nginx

您现在可以在浏览器中访问您的域的 TLS 安全版本时看到您的 Tomcat 安装:

1https://your_domain

限制 Apache 或 Nginx 的 Tomcat 访问

现在,您已通过具有 TLS 证书的代理服务器暴露您的 Tomcat 安装在您的域中,您可以通过限制访问它来加固您的 Tomcat 安装。

所有向 Tomcat 的 HTTP 请求都应该通过代理,但您可以将 Tomcat 配置为仅在本地 loopback 界面上收听连接。

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

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

在名为Catalina服务下找到连接器的定义,它看起来像:

1[label /opt/tomcat/conf/server.xml]
2...
3    <Connector port="8080" protocol="HTTP/1.1"
4               connectionTimeout="20000"
5               redirectPort="8443" />
6...

若要限制对本地环绕接口的访问,请指定‘127.0.0.1’作为‘地址’参数:

1[label /opt/tomcat/conf/server.xml]
2...
3    <Connector port="8080" protocol="HTTP/1.1"
4               connectionTimeout="20000"
5               address="127.0.0.1"
6               redirectPort="8443" />
7...

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

要进行更改,请通过运行重新启动Tomcat:

1sudo systemctl restart tomcat

您的 Tomcat 安装现在只能通过您的 Apache 或 Nginx 网页服务器代理服务器访问。

结论

在本教程中,您将Tomcat设置在免费Let's Encrypt TLS证书安全的代理服务器后面,您还通过局部路由接口(localhost)限制与Tomcat的连接,从而禁止直接外部访问,这意味着只有局部应用程序(如Apache或Nginx)才能连接。

虽然配置一个单独的 Web 服务器流程可能会增加服务您的应用程序的软件,但它简化了确保 Tomcat 流量的过程。

Published At
Categories with 技术
comments powered by Disqus