You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何子域名下的Node.js应用通过HTTPS访问返回404错误?

解决方案

一、修复HTTPS下Node.js应用的404问题

你的443端口VirtualHost配置里缺少Passenger核心运行配置,Apache无法识别这是Node.js应用,导致HTTPS请求被当作静态资源处理,返回404。

修改/etc/httpd/conf.d/nodeapp-ssl.conf的<VirtualHost *:443>块,补充Passenger相关指令:

<VirtualHost *:443>
    ServerName api.example.com
    DocumentRoot /home/user1/api.example.com

    # 启用Passenger并配置Node.js应用
    PassengerEnabled on
    # 指定Node.js应用根目录(若入口文件在子目录如public,需对应调整)
    PassengerAppRoot /home/user1/api.example.com
    # 指定系统Node.js路径,可通过`which node`命令获取实际路径
    PassengerNodejs /usr/bin/node
    # 指定你的Node应用入口文件(比如server.js或app.js)
    PassengerStartupFile server.js

    # Enable SSL
    SSLEngine on
    SSLCertificateFile /home/user1/ssl/certs/_wildcard__api_example_com_123.crt
    SSLCertificateKeyFile /home/user1/ssl/keys/123.key
    # 若有中间证书,添加此行(根据实际证书路径调整)
    # SSLCertificateChainFile /home/user1/ssl/certs/intermediate.crt

    # Optional: Logging
    ErrorLog /home/user1/api.example.com/error.log
    CustomLog /home/user1/api.example.com/access.log combined

    # Security Headers (optional but recommended)
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

    # Ensure the .htaccess rules are respected
    <Directory /home/user1/api.example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName api.example.com
    DocumentRoot /home/user1/api.example.com

    # Redirect all HTTP traffic to HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

修改完成后重启Apache服务:

systemctl restart httpd
# 若使用cPanel/EasyApache,可替换为:
# /usr/local/cpanel/scripts/restartsrv_httpd

二、修复Node.js日志输出到指定文件的问题

Passenger默认会将Node应用的日志重定向到Apache全局日志,要让日志输出到你指定的文件,需添加Passenger专属日志配置:

  1. 在<VirtualHost *:443>块中补充以下指令:
# 设置Passenger日志级别(debug级别用于排查问题,可选)
PassengerLogLevel 3
# 指定Node应用的日志文件路径
PassengerAppLogFile /home/user1/api.example.com/error.log
  1. 确保日志文件权限正确,让Apache用户(通常为apache或nobody)拥有写入权限:
chown apache:apache /home/user1/api.example.com/error.log
chmod 664 /home/user1/api.example.com/error.log
  1. 再次重启Apache服务使配置生效。

额外检查点

  • 验证通配符SSL证书是否包含api.example.com子域名,可通过浏览器访问https://api.example.com查看证书详情确认。
  • 若.htaccess文件存在Rewrite规则,可临时注释后测试是否存在冲突。

内容的提问来源于stack exchange,提问作者RRR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:43:19