Nginx For JAMStack Apps
Pre-rendering your web pages following the JAMStack methodology is a sure way to decrease your time-to-first-byte metric, but dropping your NodeJS server when you can is even better.
In any app, you need a webserver to send content back, but not all web servers are equal: a web server powered by Javascript like Express will be 6.5 times slower than one written in C, according to a recent study. 27 times if you go with PHP, 60 with Ruby, and 70 with Python.
Even among web servers written in C, the performance difference can be huge: Apache is 2.5 times slower than Nginx!
It’s in your best interest to use the most performance technology to not only decrease your costs (less RAM consumption) but also to bring the best experience to your user.
Great things happen with Nginx when you set it up as a reverse proxy to handle all incoming requests. You can for example tell Nginx in 6 lines of code to send static webpages if they exist, and fallback to a NodeJS server when it needs to. This way, you can for example have a prerendered website in one folder, and a full-blown backend application running in the background that you can call from within your web components: it’s the best of both worlds, without the monolithic architecture.
Using Nginx as a reverse proxy is also how you can implement features like Gatsby’s incremental builds, which are only available on paid plans.
I’m currently running experiments with a $3/month cloud hosting provider called Hetzner that allows me to set up my own virtual web server, and so far the aforementionned configuration works really well for a prerendered SaaS app. I don’t need much RAM since most of the content is served with Nginx and I don’t have any expensive NodeJS functions to run, but I can still rebuild and scale my website as much as I want without fearing huge bills. It feels truly magical.