Nginx is a web server and reverse proxy that serves over 33% of publicly known websites on the internet. You can run Nginx as a regular application, as a Docker container. This article is an overview of setting up Nginx as a reverse proxy for backend applications on debian-based Linux machines. We’ll skip the installation and go straight to set up tips.
Installing Nginx
You can install Nginx from the default package repositories of your debian system or better still from Nginx’s official packages from nginx.org. This gives you Nginx configuration in the way that Nginx intends.
Things to consider
You need to consider the following:
- How you set up different proxy configs - use the /etc/nginx/conf.d/ directory
- How you handle SSL - use certbot. You can install using
apt install certbot python3-certbot
- For uploads, set the maximum size, so your backend engineers don’t complain later that their files are not getting uploaded. You can do this in the http block like so:
http {
...
client_max_body_size 50M;
...
}
Note that this can be done at a server level to limit to a single server config or even location level to limit it to a single location block.
- Consider defining a utility bash function to speed up the addition to extra configs. You can add this function to your .bashrc or .profile so it’s available all the time. Don’t forget to source the file you added it when you’re adding it for the first time!
generate_nginx_config() {
if [ "$#" -ne 2 ]; then
echo "Usage: generate_nginx_config <domain> <port>"
return 1
fi
local domain=$1
local port=$2
cat <<EOF
server {
listen 80;
server_name $domain;
location / {
proxy_pass http://localhost:$port;
}
}
EOF
}
Wrap up
This article gave you practical steps you can take to ensure your Nginx setup is cleaner and more reliable for you and your backend engineers. Feel free to share this to your co-workers if you found it helpful.