Nginx is an important web server and reverse proxy that powers a wide range of servers on the web (link to data). This article is an overview of setting up Nginx as a reverse proxyfor your backend application.

Things to consider

You need to consider the following:

  1. How you install Nginx - Best to install using their provided sources

  2. How you set up different proxy configs - use the /etc/nginx/conf.d/ directory

  3. How you handle SSL - use certbot. Assuming you’re on a debian based system, you can install using apt install certbot python3-certbot

  4. 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.

  5. 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.