我在 linux aws ec2 上运行一个节点 js 服务器。我正在尝试创建一个子域,以便我可以在http://api.domain.com
运行我的节点 rest api。
httpd.conf
<VirtualHost *:80>
ServerName api.domain.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /server.js
</IfModule>
一切都很好,但是当我尝试击中http://api.domain.com/method
时,它会爆炸。但是http://api.domain//method
(强调双//(工作正常。如果我刚刚点击http://api.domain/
我会收到预期的"无法获取/"错误消息。
另外,http://ip-address:3000/method
工作得很好
我假设我需要在我的.htaccess
文件中添加一些额外的条目或其他东西,但我找不到任何东西......
解决方案
我通过将代理包装在双引号中来使其工作:
<VirtualHost *:80>
ServerName api.domain.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass "/" "http://localhost:3000/"
ProxyPassReverse "/" "http://localhost:3000/"
</VirtualHost>
运行sudo service httpd restart
后,一切都按预期工作。