You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nginx is restarted instead of reloaded on configuration changes #15906
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?
Reacted by Chris Martin
Reacted by Alexander Ried, Danylo Hlynskyi, ibrahim Sağıroğlu, Robin Lambertz, Profpatsch and Luka Teras
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.
@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?
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.
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.
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!
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...
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.
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 beforeactivationScripts, 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?
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).
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?
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.
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.
@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.
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.
@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.
Activity
domenkozar commentedon Jun 1, 2016
I wonder if we could just have a derivation that does
nginx -tand systemd depends on it. So if it fails to build, NixOS won't come to restarting services.bobvanderlinden commentedon Jun 1, 2016
@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?domenkozar commentedon Jun 1, 2016
well with
_noChroot = true:(bobvanderlinden commentedon Jun 1, 2016
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.domenkozar commentedon Jun 1, 2016
We could start by having it by default off. It is just a build check nevertheless.
bobvanderlinden commentedon Jun 2, 2016
Hmm, I gave this a try. This is the result: bobvanderlinden@6ebda67
I tested this using:
First problem is that
build-use-chroot = falseshould be set innix.conf(or using commandline). After thatnginx -tdoes run, but the check gives errors, because the file permissions aren't correct. The check is run undernixbldinstead of thenginxuser. I'm not sure how to work around this yet, butbuild-use-chroot = falseby 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.plandnixos-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#L335Some kind of assertion script (just like the
/bin/switch-to-configurationor/activatescript) 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!
edolstra commentedon Jun 2, 2016
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 -tis likehttpd -t, it will barf on things like missing user accounts, which are created by the activation script...edolstra commentedon Jun 2, 2016
Regarding reload, you can set
reloadIfChanged = trueon the service. However, that's not a good idea, because it would prevent nginx from being restarted ever. So upgrading wouldn't work anymore.bobvanderlinden commentedon Jun 3, 2016
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 beforeactivationScripts, 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 --rollbackwhen anactivationScripthas 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-rebuildorswitch-to-configuration.pl?nightkr commentedon Jun 5, 2016
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).
bobvanderlinden commentedon Jun 5, 2016
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 innixos-rebuild, maybe something similar tonixos-rebuild switch || nixos-rebuild switch --rollback?cstrahan commentedon Jun 20, 2016
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.
danbst commentedon Jul 12, 2016
Speaking of nginx, it has zero-downtime upgrade schema http://nginx.org/en/docs/control.html#upgrade
domenkozar commentedon Jul 12, 2016
If we'd like the executable to some state folder, that could work.
domenkozar commentedon Jul 23, 2016
There's a pretty decent implementation of this in #17208
teh commentedon Aug 23, 2016
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.
bobvanderlinden commentedon Aug 24, 2016
@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.
nginx service: reload instead of restart
Profpatsch commentedon Jun 24, 2018
(triage) cc @fpletz @globin
coretemp commentedon Jul 23, 2018
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.
bobvanderlinden commentedon Jul 23, 2018
@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>.listenvalues.nginx: expose generated config and allow nginx reloads
nginx: expose generated config and allow nginx reloads (#57429)