我正在尝试在端口 8000 上运行 Node js 应用程序,并让 Apache 2.4.7 将子域代理到本地主机上的该端口。我能够将节点应用程序作为页面提供(http://example.com/nodeapp)使用单个VirtualHost,使用启用站点的conf文件,如下所示:
<VirtualHost *:80>
ServerName example.com
ServerAlias "www.example.com"
DocumentRoot "/var/www/html"
ProxyPass "/nodeapp" "http://localhost:8000/"
</VirtualHost>
然而,我真正想要做的是让节点应用程序在子域上运行,例如http://nodeapp.example.com,这就是我遇到麻烦的地方。我尝试过许多不同的配置,包括单个和多个 VirtualHosts,但目前的配置是这样的:
<VirtualHost nodeapp.example.com:80>
ServerName "nodeapp.example.com"
# ProxyPreserveHost On
ProxyPass "/" "http://localhost:8000/"
# ProxyPassReverse "/" "http://localhost:8000/"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias "www.example.com"
DocumentRoot "/var/www/html"
</VirtualHost>
这是基于此处提出的问题:
使用 Apache VirtualHost 访问同一服务器上的 Node JS 实例
然而,当我进入子域(我在 GoDaddy 上设置为仅转发到同一个服务器 IP 地址)时,我只是遇到超时,如果我查看 apache 错误日志,我会看到如下几行:
[Tue Mar 29 04:53:56.909467 2016] [core:notice] [pid 15912] AH00094: Command line: '/usr/sbin/apache2'
[Tue Mar 29 04:56:33.332390 2016] [proxy:error] [pid 15917] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:8000 (localhost) failed
[Tue Mar 29 04:56:33.332441 2016] [proxy:error] [pid 15917] AH00959: ap_proxy_connect_backend disabling worker for (localhost) for 60s
我现在真的不太清楚我哪里做错了,但如果能提供任何帮助我将非常感激!
答案1
以下是我使用的:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName sub.example.com
DocumentRoot /var/www/html
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error-sub.example.com.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-sub.example.com.log combined
# sub:
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://example.com:8000/
ProxyPassReverse / http://example.com:8000/
</VirtualHost>