Tag: reverse proxy

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.

Setting Reverse Proxy in Apache Ubuntu

Suppose we have a web app running on a port. For example nodejs runs server on a port. For accepting incoming connections on this port without actually exposing the port number is what we look for.

To solve this we can add few lines in our apache configuration to tell our web server to accept connections on a port.

Lets assume we are trying to access this url

http://127.0.0.1:8080/mybankapp

Open the file /etc/apache2/sites-available/000-default.conf using any editor and paste the below lines. You may take a backup of the above file before doing so.

<VirtualHost *:80>
    ProxyPreserveHost On

    ProxyPass /mybankapp http://127.0.0.1:8080/mybankapp
    ProxyPassReverse /mybankapp http://127.0.0.1:8080/mybankapp
</VirtualHost>

Before we restart apache we may enable some modules.

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

After we execute above command now we may restart apache.

sudo systemctl restart apache2

Now we can access our web app without the port number.