Skip to content

Nginx is restarted instead of reloaded on configuration changes #15906

Closed

Description

@bobvanderlinden

When the Nginx configuration changes and nixos-rebuild switch is executed, Nginx is restarted using stop and start separately. Not only will HTTP/HTTPS not function during that time (minor issue), but if the Nginx configuration is incorrect Nginx fails to start. Note that an incorrect Nginx configuration includes missing a file that is referenced from the configuration.

On other distributions Nginx is reloaded. It'll show an error when it cannot load the new configuration, but continues to work with the old configuration. It would be nice if that was possible in NixOS as well.

Alternatively, Nginx configuration can be checked using nginx -t. If that was executed before the actual switch/restart is made, that would already be a great help.

This is also important when huge configuration changes are done from NixOS, like #15862. It could break existing configurations and testing the configuration would prevent actual down-time.

I'd like to give this a shot, but I'm not sure where to start. Activation scripts? Systemd service file?

Activity

  1. added
    0.kind: enhancement
    on Jun 1, 2016
  2. domenkozar commented on Jun 1, 2016

    @domenkozar
    Member

    I wonder if we could just have a derivation that does nginx -t and systemd depends on it. So if it fails to build, NixOS won't come to restarting services.

  3. bobvanderlinden commented on Jun 1, 2016

    @bobvanderlinden
    MemberAuthor

    @domenkozar That would be a good option for self-contained configurations, but what if the configuration refers to /var/ for example? Will that still work?

  4. domenkozar commented on Jun 1, 2016

    @domenkozar
    Member

    well with _noChroot = true :(

  5. bobvanderlinden commented on Jun 1, 2016

    @bobvanderlinden
    MemberAuthor

    How would that work exactly? Can one part of an derivation be executed as noChroot? If it's an option for Nginx (services.nginx.checkConfiguration = true;) it might be hacky, but it would be an improvement already.

  6. domenkozar commented on Jun 1, 2016

    @domenkozar
    Member

    We could start by having it by default off. It is just a build check nevertheless.

  7. bobvanderlinden commented on Jun 2, 2016

    @bobvanderlinden
    MemberAuthor

    Hmm, I gave this a try. This is the result: bobvanderlinden@6ebda67

    I tested this using:

    nix-build -E 'import ./nixos { configuration = { boot.loader.grub.enable = false; fileSystems = [{mountPoint = "/"; device="/dev/something";}]; services.nginx = { enable = true; checkConfiguration = true; }; }; }'
    

    First problem is that build-use-chroot = false should be set in nix.conf (or using commandline). After that nginx -t does run, but the check gives errors, because the file permissions aren't correct. The check is run under nixbld instead of the nginx user. I'm not sure how to work around this yet, but build-use-chroot = false by itself is kind of a show-stopper. I'd rather not have that setting on a production server ;).

    In search for a better solution I found switch-to-configuration.pl and nixos-rebuild.sh.

    (Potential solution 1) It has a list of services that it needs to reload (apart from restarting). I get why it would currently restart nginx (the configuration path has changed, so the whole service file changed, so a reload won't do much good), but how do you actually get services in that reload-list at all? (/run/systemd/reload-list) It seems a bit magical to me, but if Nginx would to be reloaded it would solve most problems already.

    From what I found there, the activationScripts are also not a viable option. It is run after services are stopped and, if the scripts were to fail, it seems the switch isn't rolled back.

    (Potential solution 2) It seems that the most promising step would be to modify nixos-rebuild.sh. After copying the system closure, but before setting the system profile to the new closure: https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/installer/tools/nixos-rebuild.sh#L335
    Some kind of assertion script (just like the /bin/switch-to-configuration or /activate script) that will cause nixos-rebuild to stop if the script fails.

    Would one of these potential solutions be worth working towards? Which one is most viable? Solution 2 is a rather fundamental change instead of just a simple addition for nginx... but I cannot find a better way to do this atm. Suggestions are very welcome!

  8. edolstra commented on Jun 2, 2016

    @edolstra
    Member

    I think a pre-switch hook that could prevent a switch could be useful. However, it won't necessarily help for nginx, because if nginx -t is like httpd -t, it will barf on things like missing user accounts, which are created by the activation script...

  9. edolstra commented on Jun 2, 2016

    @edolstra
    Member

    Regarding reload, you can set reloadIfChanged = true on the service. However, that's not a good idea, because it would prevent nginx from being restarted ever. So upgrading wouldn't work anymore.

  10. bobvanderlinden commented on Jun 3, 2016

    @bobvanderlinden
    MemberAuthor

    Ugh, you're right. This is harder than I initially anticipated.

    So, you'd want to run the test after the activationScripts, but before stopping the service. Whereas in some cases you need to stop the service before activationScripts, because otherwise the service would be running with a non-existent or incorrect user. It seems to be fully correct you'd want some kind of dependency system for the activation scripts, but even then I'm not sure all cases are covered.

    A different approach would be to automatically nixos-rebuild --rollback when an activationScript has failed. The service is still stopped and started, so connections will be dropped. However, it can be mitigated a bit using a systemd-socket.

    A systemd-socket by itself wouldn't be that bad of an idea to have as an NixOS option.

    With automatic rollback it would even be unnecessary to use nginx -t, as a service that cannot be started will also automatically rollback the configuration switch.

    Does this seem like a sane feature to add to nixos-rebuild or switch-to-configuration.pl?

  11. nightkr commented on Jun 5, 2016

    @nightkr
    Member

    I'd rather not have automatic rollback, or at least not without an option to opt out. For example, that would make it a pain to upgrade things like PostgreSQL, where you need to manually intervene and upgrade the data (which I'd consider a good thing, since it's more or less one-way and could be lossy).

  12. bobvanderlinden commented on Jun 5, 2016

    @bobvanderlinden
    MemberAuthor

    Indeed, automatic rollback isn't something you'd want in all cases. I was thinking of an extra option for nixos-rebuild switch. Then again, it might also be possible without changes in nixos-rebuild, maybe something similar to nixos-rebuild switch || nixos-rebuild switch --rollback?

  13. cstrahan commented on Jun 20, 2016

    @cstrahan
    Contributor

    I think a pre-switch hook that could prevent a switch could be useful. However, it won't necessarily help for nginx, because if nginx -t is like httpd -t, it will barf on things like missing user accounts, which are created by the activation script...

    Maybe a crazy idea, but perhaps activation could happen in separate stages, so as to ensure that the suggested pre-switch hooks have access to the referenced users and such. As long as every non-idempotent change is ordered after the checks (e.g. that would preclude postgres db updates and such), the activation can safely rollback automatically.

  14. danbst commented on Jul 12, 2016

    @danbst
    Contributor

    However, that's not a good idea, because it would prevent nginx from being restarted ever. So upgrading wouldn't work anymore.

    Speaking of nginx, it has zero-downtime upgrade schema http://nginx.org/en/docs/control.html#upgrade

  15. domenkozar commented on Jul 12, 2016

    @domenkozar
    Member

    If we'd like the executable to some state folder, that could work.

  16. domenkozar commented on Jul 23, 2016

    @domenkozar
    Member

    There's a pretty decent implementation of this in #17208

  17. teh commented on Aug 23, 2016

    @teh
    Contributor

    We solve part of this issue (losing connections) by using socket activation:

    https://freedesktop.org/wiki/Software/systemd/DaemonSocketActivation/

    but this only works if we can trust that the config uses port 80 and 443. I think this is a reasonable assumption for the automatic config but not so much otherwise. If people are interested I'll spend some time to create a PR with our local patches.

  18. bobvanderlinden commented on Aug 24, 2016

    @bobvanderlinden
    MemberAuthor

    @teh It is a step in the right direction, but it doesn't help against incorrect configuration. You still need to rollback to a previous configuration manually if you have noticed that Nginx failed to load.

    That said, if this were to be implemented in Nixpkgs, the port(s) can now be derived from the vhost options.

  19. added a commit that references this issue on Mar 30, 2017
    b12a3d1
  20. Profpatsch commented on Jun 24, 2018

    @Profpatsch
    Member

    (triage) cc @fpletz @globin

  21. coretemp commented on Jul 23, 2018

    @coretemp
    Contributor

    I don't agree that trusting the config to use port 80 and 443 is a reasonable assumption. You could make it such that there are assertions for your special case, but why can't it work in all cases.

  22. bobvanderlinden commented on Jul 23, 2018

    @bobvanderlinden
    MemberAuthor

    @coretemp I don't think it is required to 'trust' the port being 80 or 443. We can probably generate the socket activation using the services.nginx.virtualHosts.<name>.listen values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions