我使用 pm2 在 centos 7 上运行 Nodejs 应用程序
当我运行时在服务器上
curl http://127.0.0.1:3021/products
它按预期返回数据。
现在我喜欢我的 api 可以访问外部服务器。
所以我正在尝试配置某种反向代理?
我已经在服务器上使用 puppet 模块运行并配置了 Apache https://forge.puppet.com/puppetlabs/apache
apache::vhost { 'my-api.local':
port => 80,
proxy_preserve_host => true,
proxy_dest_match => 'http://127.0.0.1:3021',
docroot => '/opt/nodejs-apis/my-api.local/current/build',
}
我的主机文件中有条目 my-api.local 指向我的 vagrant box ip。
当我访问http://my-api.local时在浏览器中我得到以下内容
{"statusCode":400,"error":"Bad Request","message":"Invalid URL: //"}
知道我缺少什么吗?
一些有用的链接
Running Node as a service with PM2 - connection refused https://www.tecmint.com/install-pm2-to-run-nodejs-apps-on-linux-server/
更新
看完这个https://www.youtube.com/watch?v=KTdv_DzwTS0
我什至更改了我的虚拟主机文件,现在看起来像这样。
<VirtualHost *:80>
ServerName my-api.local
ProxyPreserveHost On
ProxyPass / http://localhost:3021/
ProxyPassReverse / http://localhost:3021/
</VirtualHost>
但还是没有运气:(
最佳答案
花了几个小时:(,终于解决了这个问题
apache::vhost { 'my-api.local':
port => 80,
proxy_preserve_host => true,
proxy_pass => { 'path' => '/', 'url' => 'http://localhost:3021' } ,
docroot => false,
}
如上所述在/etc/httpd/conf.d/中生成了此内容
<VirtualHost *:80>
ServerName my-api.local
ProxyPreserveHost On
ProxyPass / http://localhost:3021/
ProxyPassReverse / http://localhost:3021/
</VirtualHost>
但还需要刷新 pm2 服务.. 不要忘记调用 api :) 例如 http://my-api.local/products
感谢https://serverfault.com/questions/804795/puppet-apache-vhost-proxypassreverse-configuration寻找线索
关于node.js - 使用pm2 apache配置nodejs api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029764/