`if` or `include` for mode-specific `server` directives?
petecooper
nginx-forum at forum.nginx.org
Sat May 16 13:13:00 UTC 2020
Hi Francis.
Francis Daly Wrote:
-------------------------------------------------------
> I suggest that you'll be happier in the long run using a templating
> language, or macro-substituting language, external to nginx; along
> with
> "source" conf files that are to have the substitutions applied; and
> change the value there and regenerate the nginx conf parts, and then
> reload nginx.
> [...]
> It is a significant change to your setup right now; but it means that
> your running nginx config will have exactly what you want, without
> needing
> to worry about levels of indirection of run-time variable
> substitution.
Excellent. Thank you. You're absolutely correct, this is a big step change
for me and will require some r&d, but I like the sounds of it.
> > == `if` in the `server` block ==
> >
> > I will readily admit I have never used `if`, having been scared away
> by If
> > Is Evil.
>
> "If Is Evil" is limited to "when used in location context"; so you're
> ok to do what you suggest here.
>
> It just is not as efficient as it could be.
Message received and understood on the `if` context, thank you.
> > == Stub `server` excerpts for each mode ==
> >
> > Rather than include `root` and `if` checks in the monolithic
> `server` block
> > file, I am considering outboarding each mode to its own stub file as
> a
> > sidecar to the main `server` block file, like this:
>
> This is better -- presumably you will edit the "include" line and
> reload
> nginx (or may use symlinks; and change where the symlink points and
> reload nginx) when you want to change mode.
Correct - edit, check config, then reload.
> If you can avoid the run-time variable substitution altogether, this
> is good.
This is useful, too. I have zero reliance on any `set` variables at a site
identification level, and I would prefer to reduce the workload Nginx has to
undertake generally for the sake of more human time when it comes to mode
switching.
> If your "mode"-conf files have lots of parts that are very similar
> apart
> from a mode-related word, then you may be ok with manually updating
> all
> of them when you want to add something else; an external thing that
> just
> generates the three "mode" files could avoid that manual-duplication
> step.
My intention is for the mode-related stubs to just contain the unique parts:
`root`, the `add_header` and anything else that crops up. The vast majority
of site directives are deliberately (at least currently) included in the
main `server` block file, the outboarding would be as little as possible
directive-wise.
> Objectively, using run-time variable substitution means that nginx
> does
> more work on every request than it would do without that.
>
> And nginx testing one or two extra "if" statements on every request
> means that nginx does more work than it would do without that.
>
> It's not a lot of extra work; and the machine-time saved over a year
> may not make up for the person-time taken in changing to a system that
> avoids those things.
...and I have my next lockdown project!
Thank you very much for your informative and useful reply, I really
appreciate it.
With best wishes,
Pete
Posted at Nginx Forum: https://forum.nginx.org/read.php?2,288057,288061#msg-288061
More information about the nginx
mailing list