Tag: nodejs

Ubuntu Apache add reverse proxy to avoid port number visibility | NodeJS

When we create APIs using nodejs we often enable them to run on particular port number. It is all fine until we move into production environment where we don’t want our port number to visible to our clients while we are making the server request calls using the APIs.

So a solution would be to mask our port number and let apache handle the request and redirect the request to that server internally without revealing the port number. For this we need to edit the apache configuration like following.

Before editing run the below command

sudo a2enmod proxy_http

Open the file /etc/apache2/sites-enabled/000-default.conf using any editor and add the following lines under or above the directory tag. Note that if you are using SSL for your site i.e https enabled then you have to edit the file /etc/apache2/sites-enabled/000-default-le-ssl.conf to make it work properly for https enabled sites.

<Location /query-api-0.3.1>
ProxyPass http://localhost:3099/query-api-0.3.1
ProxyPassReverse http://localhost:3099/query-api-0.3.1
Order allow,deny
Allow from all
</Location>

Reverse Proxy exmaple in apache

Then save the file and restart apache and try to run the API request without the port number.

How to kill a node server using its pid(process id) that is running in background

Often we run node servers in background. Like using screen or using a cron job that starts while booting. So how do we kill it when when its running in the background.

Well, one way is if it is running in a screen we can resume the screen and terminate it.

Or even better we can find the process id of the server you are running. For this we need to remember the port on which you are running the node server.

For example if the server you want to terminate is running on the port 3029, run the below command to get the process id of that process.

lsof -i:8044

In the above command 8044 can be any port number. When we run the command we get output like below

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 108597 nagaraju 20u IPv6 318585 0t0 TCP *:8044 (LISTEN)

Notice the PID. That is what we need to terminate the process.

Now we run the below command to kill/terminate that process.

kill -9 108597

Using kill command to terminate the process and then checking if the process is still running after terminating it