DEV Community


Dynamic and Abstract Nginx

aakhtar3 profile image aakhtar3 ・1 min read

Dynamic and Abstract Nginx

Nginx is a powerful webserver and can built dynamically by injecting environment variables with envsubst and importing modular configs with the include directive.



  • Create /etc/nginx/nginx.conf.template with environment variables ${VAR}
  • Use envsubst to dynamically generate a new /etc/nginx/nginx.conf
# Define the environment variables
export SERVER='example' LOG='example'

# Run command to generate new nginx.conf
cd /etc/nginx
envsubst '${SERVER},${LOG}' < nginx.conf.template > nginx.conf
brew install

Mac users will need to install gettext which includes envsubst.

brew install gettext
brew link --force gettext 


From the example above using the sample http config.

* is a wildcard and will match on

http {
    include /etc/nginx/conf.d/example.*.conf;

Here is an example folder structure for your Nginx modules.

    nginx.conf                  # HTTP
    /conf.d                     # Server(s)        # Can contain multiple servers
        default.conf            # Comes with nginx
    /sites-enabled              # Includes
        /location               # Location Includes
            # /location blocks to static assets
        /SSL                    # SSL Includes
       # Contains SSL directives for
        /*                      # You can continue to modularize your code base

Check out this repo which has more include examples.

GitHub logo 10up / nginx_configs

Nginx Configuration Template for WordPress Sites


Editor guide