Skip to content

The ability to detect when nginx reload happens #914

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
pyhedgehog opened this issue May 20, 2025 · 4 comments
Open

The ability to detect when nginx reload happens #914

pyhedgehog opened this issue May 20, 2025 · 4 comments
Labels

Comments

@pyhedgehog
Copy link

pyhedgehog commented May 20, 2025

Problem: Cached config with externally changed config

I have module that needs changeable config (i.e. js_preload_object is not an option).
Module implements API (via js_body_content) to alter config.
Initial version of config is read from file and stored in ngx.shared dict.
When config changed via API it updates in ngx.shared and file.
Sometimes (mostly from test scripts) I need to update file and reload nginx.
But in this case config version in ngx.shared becomes unsynced with file on disk so I need to stop/start nginx instead of reload.

Solution1

Some sort of hook - like js_reload_hook mod.func; directive or ngx.on("reload", callback); function.

Solution2

Variable that changed on reload and can be cached. Then I can check if r.variable.nginx_config_id changed since storing config in ngx.shared. Such variable can be just integer that increments on each reload.

@xeioex
Copy link
Contributor

xeioex commented May 20, 2025

Hi @pyhedgehog,

I am currently working on a statefile for the shared dict. It will allow to save the state of the shared dict in a file, it will also sync the dict content to the disk from time to time.

Will it help?

@pyhedgehog
Copy link
Author

pyhedgehog commented May 21, 2025

@xeioex I have a problem to sync dict from disk. Sync to disk is just use of fs.promises.writeFile(). My problem is that I don't know when to use readFile(). Obvious solution is to add another API ala /api/v0/configreload, but this is not in line with nginx config lifecycle.

@jo-carter
Copy link
Contributor

jo-carter commented May 28, 2025

@pyhedgehog If occasional false positives are not a problem, you can use js_periodic for this purpose.

For example, with the configuration:

...
location @periodic {
            js_periodic main.handler interval=9999999999;
        }
...

js_periodic always runs at least once per worker "lifespan",, right at the startup. The periodic above is set to run on worker zero only. By setting a very high interval, it is ensured that the periodic will only ever run at startup, and will never repeat / timer will never expire.

False positives will occur if worker zero process is respawned as the result of a crash of worker, rather than reload.

@pyhedgehog
Copy link
Author

@jo-carter, Thank you.

pyhedgehog added a commit to pyhedgehog/norenye that referenced this issue Jun 1, 2025
+ reloadconf (see nginx/njs#914
+ norenye_api: /_/static/... and /favicon.ico via new staticfile func
+ rewrite test_api with class inheritance to test against several configs
+ tests for all API endpoints (some fixes as a result)
+ initial attempts to support QJS failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants