Skip to content

Filter Nginx includes by sites present on target server #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dalepgrant
Copy link
Contributor

@dalepgrant dalepgrant commented May 1, 2025

One for those of us using multiple remote servers with Trellis.

Currently, Trellis templates out anything you put into nginx-includes/ to the remote servers. Take the following:

trellis/
└── group_vars/         
    └── production/
        ├── main.yml
        └── vault.yml
    └── staging/
└── host_vars
    └── server1/
        └── wordpress_sites.yml
    └── server2/
        └── wordpress_sites.yml
    └── staging1/
    └── staging2/

Assume server1 has a different site(s) to server2, and perhaps staging1 and staging 2 have additional sites. Your Nginx includes folder might look like this:

trellis/
└── nginx-includes/         
    └── all/
        ├── rewrites.conf.j2
        └── proxy.conf.j2
    └── site1/
        └── rewrites.conf.j2
    └── site2/
        └── rewrites.conf.j2
    └── site3/
        └── rewrites.conf.j2
...etc

All servers will end up with all Nginx includes files, even if the sites don't exist on that server:

/etc/nginx/includes.d/
└── all/  
    ├── rewrites.conf
    └── proxy.conf
└── site1/  
    └── rewrites.conf
└── site2/  
    └── rewrites.conf
└── site3/  
    └── rewrites.conf
└── site4/  
    └── rewrites.conf

This PR filters the Nginx includes that are to be 'included' by combining the sites that exist on the target server (taken from wordpress_sites keys) with a default 'all'. This follows with the default setup shown in the docs.

Also included is the ability to override the default 'all' folder. I've only included this because it is possible to override the block in the Nginx conf template and if someone did that, they may also want to determine their own Nginx includes locations.

To override or otherwise add to the Nginx includes locations, in e.g. group_vars/all/main.yml you can do

# single. will override default 'all' if changed. 
nginx_includes_extra_folders: all

# multiple
nginx_includes_extra_folders:
  - all
  - example
  - another

Note: in testing, the folders for sites not present on a remote are not cleaned up if they already exist but the files inside them are. That's to do with what happens when setting nginx_includes_d_cleanup: true which is a separate issue.

@dalepgrant dalepgrant marked this pull request as ready for review May 1, 2025 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant