Skip to content

Tips

Handy tips always at your fingertips

A collection of 123 tips that come in real handy when you need them. Originally posted as tweets on my Twitter account, and now gathered here so you can browse them easily.

A Better Minimum Compression Size in Nginx

💡 Compressing anything that's small is unlikely to shrink much. In some cases, it even make it bigger!

👉🏼 That's why I always make sure to set "gzip_min_length" in Nginx to something higher than the default.

Set gzip_min_length directive to 256 to tell Nginx to start compressing at a bigger size. Default value of 20 usually leads to larger files after gzipping because of the small size. Read tip

Secure Phrase Generator for Passwords and Secrets

💡 I often find myself needing to come up with a secure phrase for app cookies and other secrets.

👉🏼 I love using https://randomkeygen.com/ for that. It generates strings from memorable to fort-knox secure. 😄

Different sets of generated phrases labeled as memorable, strong and fort knox passwords. Read tip

Disable Nginx Server Header

💡 You can tell Nginx to not reveal its version number to client responses. The less information an attacker has on your system, the better.

👉🏼 Set "server_tokens" value to "off" in the main http directive.

Set server_tokens value to off in the http block to disable emitting nginx version on error pages and in the Server response header. Read tip

Improve Nginx Performance: Multi-Threading

💡 Nginx is an awesome web server and reverse proxy. But some if its defaults are better changed to more sane values.

👉🏼 One such value is "worker_processes". By default, Nginx uses only one CPU core. Set this to "auto" to use all available cores on the system.

By setting worker_processes to auto, Nginx will spin up a number of threads equal to available CPU cores. Read tip

Use Systemd to Manage Processes

💡 Systemd may seem daunting at first, but it's pretty simple once you get the hang of it.

👉🏼 Place a unit file describing your process in /etc/systemd/system/[process-name].service, and run:

➡️ systemctl daemon-reload && systemctl start [process-name]

A basic systemd unit file with a service description and start command. Read tip

Get Process Environment From Terminal

💡 How do you get the ENV vars of a running Node.js (or any) process?

➡️ cat /proc/$PID/environ | tr '\0' '\n'

Prints out the initial environment of the process.

Get the ENV vars of a running process with cat /proc/$PID/environ | tr '0' '
'. Read tip

Get Node.js Port Using PM2 Name

💡 I recently needed to get the port of a @nodejs process from its PM2 name with a one-liner:

➡️ ss -ntlp | grep $(pm2 ls | grep <app_name> | awk '{print $12}') | awk '{print $4}' | grep -oP '(?<=:)[0-9]{2,}'

Get the port of a running Node.js process by its PM2 Name with the following command: ss -ntlp | grep $(pm2 ls | grep <app_name> | awk '{print $12}') | awk '{print $4}' | grep -oP '(?<=:)[0-9]{2,}'. Read tip

Quick Server for Incoming Webhooks

💡 Need a quick way to process incoming webhooks?

👉🏼 Webhook allows you to easily create HTTP endpoints (hooks), which you can use to execute commands. Useful if you're configuring auto-deployments from a repository.

https://github.com/adnanh/webhook

Read tip

Update Packages on Ubuntu

💡 First thing I do on a new VPS instance is to update all packages for the latest security & bug fixes:

➡️ apt update && apt upgrade

What commands do you run on a fresh new server?

Read tip

Assign a Random Port to Node.js Server

💡 How do you conveniently assign an available port to an app?

👉🏼 Use PORT=0! The kernel will pick a random unused port number and bind it to your app. This is useful when you're deploying multiple apps on a single server.

Use server.listen(0) to let the OS assign a randomly available port to the Node.js server. Print the assigned port with server.address().port. Read tip