我有一个在 aws ec2 ubuntu 实例端口 3000 上运行的 NextJS 服务器,并且希望将流量从使用 Apache 的端口 443 重定向到使用 NextJS 应用程序的端口。我达到了预期的结果,但它只能在 Chrome 浏览器中运行。
这就是我的 Apache 配置文件的样子:
<VirtualHost *:443>
ServerName domain.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
SSLEngine on
SSLCertificateFile /etc/ssl/certs/www_domain_com.crt
SSLCertificateKeyFile /etc/ssl/private/domain_private.key
SSLCertificateChainFile /etc/ssl/certs/www_domain_com.ca-bundle
</VirtualHost>
这些是我来自 apache 的最后日志:
[Thu Jan 25 20:11:21.975521 2024] [mpm_event:notice] [pid 2267:tid 140152490424192] AH00492: caught SIGWINCH, shutting down gracefully
[Thu Jan 25 20:11:22.062393 2024] [mpm_event:notice] [pid 2367:tid 139778842716032] AH00489: Apache/2.4.52 (Ubuntu) OpenSSL/3.0.2 configured -- resuming normal operations
[Thu Jan 25 20:11:22.062521 2024] [core:notice] [pid 2367:tid 139778842716032] AH00094: Command line: '/usr/sbin/apache2'
当我尝试在 Chrome 中打开 domain.com 时,它工作正常,但任何其他浏览器都无法获取响应并等待 ttl 过期。
答案非常简单......默认情况下,除 Chrome 之外的其他浏览器都会尝试将对 example.com 的请求发送到端口 80。因此我应该将流量从端口 80 重定向到 443。
这是我的完整配置文件:
<VirtualHost *:80>
Redirect "/" "https://example.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ProxyPreserveHost On
ProxyPass "/" "http://localhost:3000/"
ProxyPassReverse "/" "http://localhost:3000/"
SSLEngine on
SSLCertificateFile /etc/ssl/certs/www_example_com.crt
SSLCertificateKeyFile /etc/ssl/private/example_private.key
SSLCertificateChainFile /etc/ssl/certs/www_example_com.ca-bundle
</VirtualHost>