Running Node.js with Nginx
Setting up Node.js App with Nginx is ideal for running node app in production. As Express’ performance best practice performance states, letting Nginx to handle cached requests, compressions and serve static files is recommended. Node.js is well known to be good at heavy IOs rather than handling static file serving and CPU intensive compressions.
I assume that the static files are at /usr/src/app/public
and Express uses 3000
port.
Remove Parts from Express
You may have static files serving configured in Express. You don’t need that any more since Nginx will handle that.
Find a line looks like this and remove it.
app.use(express.static('public'))
Also you may have configured gzip compression as well. Nginx is better at handle those as well. Remove this line.
app.use(compression())
Configure Nginx
Use the below configuration file.
Verify the Setup
Try to run curl -I http://yourIpAddressOrDomain
or you can check the header in the chrome browser’s network tab.
You will see nginx
header, if all the configuration has setup correctly.
More
You may install more plugins for Nginx. So more compression options like brotli is available. See link for reading official documentation for nginx brotli plguing.