Skip to content

nginx: Verify that configuration is syntactically correct - #17208

Merged
garbas merged 1 commit into
NixOS:masterfrom
roblabla:feature-nginxCheckConfig
Jul 27, 2016
Merged

garbas merged 1 commit into
NixOS:masterfrom
roblabla:feature-nginxCheckConfig

Conversation

@roblabla

Copy link
Copy Markdown
Contributor
Motivation for this change

Making mistakes on a daily basis to the nginx config, I've made my website fall way too often due to a missing semicolon. With this change, the configFile writeText will fail if nginx -t doesn't report "syntax is ok". Note that nginx -t also checks if the files referenced in the config exists. However, I had to disable those checks since the user running the build (nixbld) doesn't necessarely have access to the files used by nginx (root for the master process).

If there is a way to run the checks under the correct user, that'd be even better. I haven't found a way to do that though.

Things done
  • Tested using sandboxing
    (nix.useChroot on NixOS,
    or option build-use-chroot in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • OS X
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@mention-bot

Copy link
Copy Markdown

@roblabla, thanks for your PR! By analyzing the annotation information on this pull request, we identified @domenkozar, @roconnor and @lethalman to be potential reviewers

@bobvanderlinden

Copy link
Copy Markdown
Member

This is very much appreciated. However, it needs to be an option as it will fail for a lot of people. nginx -t doesn't just check for syntax errors, it also checks whether referred files and directories actually exist.

For instance my nginx configuration depends on certificates that are generated by ACME before Nginx starts. Running this on a NixOS machine will not work when ACME (or its dummy) has not ran before. Nginx and ACME can only run after the configuration has been built.

In addition, tools like nixops and building VMs on a machine that isn't the target will also fail when Nginx is referring to files outside of /nix/store. This happens when I run automated tests in a VM, but build the VM using Nix. nginx -t will be ran on the machine that is building, not the target machine.

#15906 covers some of the problems around avoiding stopping/starting Nginx with an invalid configuration.

@domenkozar

Copy link
Copy Markdown
Member

But if you look at the implementation it ignores exit code but just checks
for syntax check

On Sat, Jul 23, 2016, 21:20 Bob van der Linden [email protected]
wrote:

This is very much appreciated. However, it needs to be an option as it
will fail for a lot of people. nginx -t doesn't just check for syntax
errors, it also checks whether referred files and directories actually
exist.

For instance my nginx configuration depends on certificates that are
generated by ACME before Nginx starts. Running this on a NixOS machine will
not work when ACME (or its dummy) has not ran before. Nginx and ACME can
only run after the configuration has been built.

In addition, tools like nixops and building VMs on a machine that isn't
the target will also fail when Nginx is referring to files outside of
/nix/store. This happens when I run automated tests in a VM, but build
the VM using Nix. nginx -t will be ran on the machine that is building,
not the target machine.

#15906 #15906 covers some of the
problems around avoiding stopping/starting Nginx with an invalid
configuration.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#17208 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAHtgynRqTfxZhFfS1fDiC7m-jCA73JRks5qYml_gaJpZM4JTZJ9
.

@roblabla

roblabla commented Jul 23, 2016 •

Copy link
Copy Markdown
Contributor Author

The implementation just verifies nginx sends a "syntax is ok" message. I have observed that this message is always sent (assuming syntax is OK), even if nginx fails to load the SSL files 'n stuff. I'm not 100% sure that it is always the I do not know what happens in case of missing includes though.

I'll admit this is a fairly bandaid solution though. I wish nginx -t had a syntax-only mode :(.

@bobvanderlinden

Copy link
Copy Markdown
Member

Aah, it took me a while to see what you meant. But after running nginx -t it makes sense. Somehow I thought syntax is ok was only outputted when everything in the file was ok.

Sorry for the confusion. This look good. 👍

@garbas
garbas merged commit 103805d into NixOS:master Jul 27, 2016
@thufschmitt

Copy link
Copy Markdown
Member

It seems that nginx -t doesn't output the syntax is ok line if it fails to load the SSL files.

On my computer, running nginx -t -c /tmp/nginx.conf says syntax is ok on the following file, but if I uncomment the ssl-related lines, I don't get the line anymore (and hence the builder fails).

user nginx nginx;
daemon off;

events {}

http {
  server {
    server_name test
    listen 443;
    # ssl on;
    # ssl_certificate /nonexistent-cert.pem;
    # ssl_certificate_key /nonexistent-key.pem;
  }
}

@globin

globin commented Jul 28, 2016

Copy link
Copy Markdown
Member

This breaks on nixops deployments:

2016/07/28 11:49:46 [emerg] 13847#0: open() "/srv/www/nixos.mayflower.de/channels/.redirects.conf" failed (2: No such file or directory) in /nix/store/p5iz8y608jp78r00fbq03c42mxpbjigc-nginx.conf:1266
nginx: configuration file /nix/store/p5iz8y608jp78r00fbq03c42mxpbjigc-nginx.conf test failed

The path /srv/www/nixos.mayflower.de/ obviously does not exist on the machine I deploy from. I'd strongly prefer reverting this.

@garbas

garbas commented Jul 28, 2016

Copy link
Copy Markdown
Member

@globin i'll revert it and lets get your PR in first. we can then make this check optional (disabled by default ofcourse)

@edolstra

Copy link
Copy Markdown
Member

No, don't make it optional. This sort of check fundamentally does not work, as we discovered years ago with the equivalent for Apache.

@roblabla

roblabla commented Jul 28, 2016 •

Copy link
Copy Markdown
Contributor Author

Well, I personally need this kind of check. Being able to optionally enable it with something like services.nginx.testConfig = true would be great. (Make it default to false to not further break working configs).

If you think that's OK, I can make a PR for this right now ?

@edolstra

Copy link
Copy Markdown
Member

The only viable way is to do the check outside of the build, but before switching to the new configuration. Having a pre-switch hook to prevent switching to a new configuration has been discussed, but I don't think we have it yet...

@roblabla

roblabla commented Jul 28, 2016 •

Copy link
Copy Markdown
Contributor Author

What I have done works (if only for the fact that the test isn't perfect). By making the derivation that puts the config in the store fail, we make the whole build fail, which prevents switching.

Outside of the syntax test being imperfect due to nginx trying to load SSL files in a syntax test..., what's the problem with my approach ?

@globin

globin commented Jul 28, 2016

Copy link
Copy Markdown
Member

It doesn't work at all as long as you don't build on the same machine you're running nginx on, which we for example never do, either the machine config is build on hydra or the machine is deployed via nixops.

@roblabla

Copy link
Copy Markdown
Contributor Author

Which is why having it opt-in (requiring users to do services.nginx.testConfig = true) would fix the issue...

@ibrahimsag

ibrahimsag commented Aug 1, 2016 •

Copy link
Copy Markdown
Contributor

can someone sketch the implementation of a pre-switch hook?
can we at least run a command like the following, between nixos-rebuild build and nixos-rebuild switch manually?
> {$new_nginx_store_dir}/bin/nginx -t -c {$new_nginx_config_store_dir} -p /var/spool/nginx

Edit: question then becomes how do i get the new store directory values with nix-store or something similar?

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.

9 participants