Create virtual host for services running on different ports

Some services run on different ports and to access them in the browser we have to mention the port number also. For ex :

SonarQube : 9000 [http://127.0.0.1:9000]
Neo4j : 7474 [http://127.0.0.1:7474]
Jenkins : 8080 [http://127.0.0.1:8080]

It is difficult to remember different server IPs alone and now we have to remember the port number too. 😕
No need of remembering all this. We can use Apache’s mod_proxy module and can define the virtual host pointing to a specific port.

Just load the following config to apache.

ProxyRequests Off
ProxyPreserveHost On

<VirtualHost *:80>
    ServerName local.neo4j.com
    ProxyPass / http://127.0.0.1:7474/
    ProxyPassReverse / http://local.neo4j.com/
</VirtualHost>

We have defined a proxy and the process[Neo4j] will be running behind this proxy.

Note: Please make sure that you have proxy_module, proxy_html_module & proxy_http_module enabled in your apache config[httpd.conf].

For Nginx : 
Just load the following config :

server {
    listen 80;
     server_name local.neo4j.com;
 
     location / {
        proxy_pass http://127.0.0.1:7474;
     }
}

After loading these configs to server [Apache/Nginx] add the server name [local.neo4j.com] to your hosts file and browse http://local.neo4j.com, Voila, we see Neo4j server running.

UPDATE [04 August 2017]

Just found out another way to achieve this. Just add the following config in your httpd-vhosts.conf file.

<VirtualHost *:80>
   ServerName local.neo4j.com
   <Proxy *>
      Allow from localhost
   </Proxy>
   ProxyPass / http://localhost:7474/
</VirtualHost>

Note: Please make sure that you have proxy_module, proxy_html_module & proxy_http_module enabled in your apache config[httpd.conf].

Hope this helps. 🙂

4 thoughts on “Create virtual host for services running on different ports

Leave a comment