Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configure nodejs api using pm2 apache

I have nodejs app running on centos 7 using pm2

on server when I run

curl http://127.0.0.1:3021/products

It returns me data as expected.

Now I like my api to accessible outside server.

So I am trying to configure some kind of reverse proxy?

I am already have Apache running and configured using puppet module on server 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',
      }

I have entry in my host file which is pointed my-api.local to my vagrant box ip.

when I access http://my-api.local in browser I get following

{"statusCode":400,"error":"Bad Request","message":"Invalid URL: //"}

Any idea what I am missing?

some useful links

Running Node as a service with PM2 - connection refused https://www.tecmint.com/install-pm2-to-run-nodejs-apps-on-linux-server/

Update

after watching this https://www.youtube.com/watch?v=KTdv_DzwTS0

I even have changed my vhost file which now looks like this.

<VirtualHost *:80>
  ServerName my-api.local
  ProxyPreserveHost On
  ProxyPass / http://localhost:3021/
  ProxyPassReverse / http://localhost:3021/
</VirtualHost>

But still no luck :(

like image 489
Developer Avatar asked Oct 14 '25 23:10

Developer


1 Answers

after spending hours :(, finally this one solved it

  apache::vhost { 'my-api.local':
    port => 80,
    proxy_preserve_host => true,
    proxy_pass          => { 'path' => '/', 'url' => 'http://localhost:3021' } ,
    docroot             => false,
  }

Which generated this in /etc/httpd/conf.d/ as above

<VirtualHost *:80>
  ServerName my-api.local
  ProxyPreserveHost On
  ProxyPass / http://localhost:3021/
  ProxyPassReverse / http://localhost:3021/
</VirtualHost>

But also need to refresh pm2 service.. Don't forget to call api :) such as http://my-api.local/products

Thanks to https://serverfault.com/questions/804795/puppet-apache-vhost-proxypassreverse-configuration for clue

like image 128
Developer Avatar answered Oct 17 '25 13:10

Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!