Skip to main content

Creating a Basic Nginx Salt Formula

Hands-On Lab


Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content





The Salt State File (or SLS) is a core concept to understand when working with Salt. The data structures contained with the State File, represented by a YAML formatted text file, provides all of the information necessary to configure your environment's end state. In this activity, the student will have an opportunity to configure an Nginx state file for use within their environment.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Basic Nginx Salt Formula


As the intern at the special-interest publication Apartment 42, you have been tasked with creating an Nginx Salt formula so the web developer can explore the possibility of using Nginx to host their new website. He is currently testing this on CentOS-based servers, and requests that you present him an Nginx formula that can install Nginx, manage the default configuration file at /etc/nginx/nginx.conf, and a restart state that will trigger whenever the configuration file is updated.

A copy of the nginx.conf file is provided for your here. The CentOS 7 server provided works as both a master and a minion. Note that the epel-release repository also needs to be installed to add Nginx; creating a separate state for this is advised.


  1. Begin by logging in to the Salt Master lab server using the credentials provided on the hands-on lab page.

    ssh cloud_user@PUBLIC_IP_ADDRESS

Create an Nginx State That Adds the Epel Repository

  1. Change the directory.

    cd /srv/salt/
  2. Make a nginx directory and move into it.

    mkdir nginx
    cd nginx
  3. Create and edit the epel.sls file.

    vim epel.sls
  4. Add the following text to the file.

        - name: epel-release
  5. Save the changes and exit the editor.

  6. Edit the init.sls file.

    vim init.sls
  7. Add the following text to the file.

      - nginx.epel
        - name: nginx
        - name: nginx
        - enable: true
  8. Save the changes and exit the editor.

  9. Test the changes.

    sudo salt 'salt' state.sls nginx test=true

Set Up the Nginx Configuration File

  1. Make a config directory and transfer into it.

    mkdir config
    cd config
  2. Pull down the configuration file.

  3. Rename the config file.

    mv .nginx.conf nginx.conf
  4. Edit the config file.

    vim nginx.conf
  5. Add a note to the top of the file.

    # This file is managed by Salt. Please do not make changes.
  6. Save the changes and exit the editor.

  7. Return to the nginx folder.

    cd ..
  8. Create a directory.

    sudo mkdir /mnt/fileshare1
  9. Create and edit the config file.

    vim config.sls
  10. Add the following text to the file.

        - name: /etc/nginx/nginx.conf
        - source: salt://nginx/config/nginx.conf
        - require:
          - pkg: nginx
  11. Save the changes and exit the editor.

Create a State to Trigger a Restart Whenever the Configuration File Changes

  1. Create and edit a restart.sls file.

    vim restart.sls
  2. Add the following text to this file.

        - name: service.restart
        - m_name: nginx
        - onchanges:
          - nginx_configuration
  3. Save the changes and exit the editor.

  4. Verify nginx is installed.

    sudo salt 'salt' state.sls nginx
  5. Test the configuration changes.

    sudo salt 'salt' state.sls nginx,nginx.config,nginx.restart test=true
  6. After verifying the results, run the command.

    sudo salt 'salt' state.sls nginx,nginx.config,nginx.restart


Congratulations — you've completed this hands-on lab!