Apache as a node.js proxy

Apache as a front-end server for Node.js application

Nowadays most of the projects are using Nginx as their front-end server.

In most cases the older Apache was relegated to the domain of legacy systems. Yet, it’s still a solid solution and there’s no need to replace it with something different if it works well.

In my case, my only need was to proxy HTTP requests to Node.js app.

Nginx config

In Nginx proxying HTTP requests was simple with following bit of code:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://localhost:8080/;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $http_host;
    }
}

Apache config

Translating this to Apache config syntax, especially after digging through the documentation:

<VirtualHost *:80>
    ServerName example.com
    Options -Indexes

    ProxyRequests on
    ProxyPass / http://localhost:8080/
</VirtualHost>

Apache itself takes care of adding X-Forwarded-For and X-Forwarded-Host headers, so there’s no need to worry about that.

Enable proxy_http module

Last thing I had to do (on Ubuntu) was to enable proxy_http module:

$ sudo a2enmod proxy_http

Apache restart and everything is ready to go.

$ sudo service apache2 restart