如何在我的本地 Tomcat 中将 localhost:8443 解析为别名 URL,例如 "https://mydev.example"

How to resolve localhost:8443 to alias URL like "https://mydev.example" in my local Tomcat

我已经在本地 Tomcat 端口 8443 中成功设置了 SSL,并使用了我在本地生成的用于开发目的的证书。它工作正常。

在我的 /etc/hosts 文件中有条目:

127.0.0.1  mydev.example  mydevsecure.example

我可以通过输入以下内容来访问我的应用程序: https://mydevsecure.example:8443/myApp/myPage.jsp [https] 要么 http://mydev.example/myApp/myPage.jsp[http]

我想要的是能够输入:https://mydevsecure.example/myApp/myPage.jsp

我不想在浏览器中输入端口号 8443。

我无法在我知道的 /etc/hosts 文件中指定端口号。

还有哪些解决方案?

https 的默认端口是 443。在浏览器中输入 https://mydevsecure.example/myApp/myPage.jsp,它将(尝试)连接到默认的 https 端口 443.

因此您只需编辑 Apache Tomcat 的 server.xml 文件并找到 SSL 连接器。然后将端口 8443 更改为 443 并重新启动 Apache Tomcat.

编辑

如果您的 Tomcat 无法绑定端口 443,因为它不是 运行 root,则有多种选择:

我通过以下方法解决了这个问题:

创建自签名 SSL 证书

运行 OpenSSL 命令生成您的私钥和public 证书。根据提示回答问题并设置密码。

openssl req -newkey rsa:2048 -nodes -keyout mykey.pem -x509 -days 365 -out mycert.pem

安装 Apache

sudo yum install httpd

在 /etc/httpd/conf/httpd.conf

中添加 SSL 配置
<VirtualHost _default_:443>
SSLEngine On
SSLCertificateFile /<path_to_ssl_certificate>/mycert.pem
SSLCertificateKeyFile /<path_to_ssl_key>/mykey.pem
ServerName https:// mydev.example
ServerAlias https:// mydev.example
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

在主机文件中添加新的安全 URL 别名

127.0.0.1 mydev.example

启动 Tomcat 和 Apache

我现在可以通过安全方式访问我的应用程序 URL:

https://mydev.example/myApp/myPage.jsp