mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-19 14:40:27 +00:00
Every write of a host config now goes through one configure() that runs under a re-entrant lock (lib/nginx-lock.js), so concurrent saves can't test or rename each other's files: - the new config is written and checked with nginx -t; when nginx rejects this host, the previous file (and .err file) is put back and an NginxConfigError with structured details is thrown (scope host/global, file, line, humanised message and hint, raw nginx output) - when another host's file breaks the test, that host is only blamed when it fails on its own (lib/nginx-blame.js). It is regenerated from its database row first and only moved to .err and marked offline when that fails too. A conflict with the change being saved fails the save instead. An error that names no file (eg. an unknown variable) is only a conflict when it goes away with every other host's config moved aside; otherwise it is the host's own error and, when its advanced config uses the variable or path, reads "Advanced configuration: ..." - errors are humanised (lib/nginx-errors.js) and mapped to the advanced config of the host where possible (new begin/end markers in the templates) - the API returns error.details; a middleware redacts details that name a host the user can't see - bulkGenerateConfigs (certificates) and the default site go through the same path, a host nginx rejects is taken offline instead of failing all - nginx -s reload only signals the master, which reads the config later. reloadNginx() waits (bounded, 5 s) until the master runs a new generation of workers and the old ones are shutting down (lib/nginx-reload.js, from /proc) before the lock is released, otherwise the next holder's rejected candidate file could be what the master reads and the change saved before would never be applied. A reload that can't be confirmed is repeated once at the end of the locked section, after every rollback. When nginx logs an [emerg] for the reload (eg. a listen port in use, which nginx -t doesn't catch) the wait ends as soon as nginx gives up on it (for a bind() after its own retries), and after a repeated reload that isn't confirmed either, later saves don't repeat it until a reload is confirmed again, so they don't each wait 10 s - reconcile() regenerates missing / moved-aside configs and retries hosts whose upstream didn't resolve yet; the startup commit runs it from backend/index.js (NPM_RECONCILE_ON_START) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
import fs from "node:fs";
|
|
import errs from "../lib/error.js";
|
|
import settingModel from "../models/setting.js";
|
|
import internalNginx from "./nginx.js";
|
|
|
|
const internalSetting = {
|
|
/**
|
|
* @param {Access} access
|
|
* @param {Object} data
|
|
* @param {String} data.id
|
|
* @return {Promise}
|
|
*/
|
|
update: (access, data) => {
|
|
return access
|
|
.can("settings:update", data.id)
|
|
.then((/*access_data*/) => {
|
|
return internalSetting.get(access, { id: data.id });
|
|
})
|
|
.then((row) => {
|
|
if (row.id !== data.id) {
|
|
// Sanity check that something crazy hasn't happened
|
|
throw new errs.InternalValidationError(
|
|
`Setting could not be updated, IDs do not match: ${row.id} !== ${data.id}`,
|
|
);
|
|
}
|
|
|
|
return settingModel.query().where({ id: data.id }).patch(data);
|
|
})
|
|
.then(() => {
|
|
return internalSetting.get(access, {
|
|
id: data.id,
|
|
});
|
|
})
|
|
.then((row) => {
|
|
if (row.id === "default-site") {
|
|
// write the html if we need to
|
|
if (row.value === "html") {
|
|
fs.writeFileSync("/data/nginx/default_www/index.html", row.meta.html, { encoding: "utf8" });
|
|
}
|
|
|
|
// Configure nginx. On failure the default site config is removed and the error is thrown.
|
|
return internalNginx.configureDefaultSite(row).then(() => {
|
|
return row;
|
|
});
|
|
}
|
|
return row;
|
|
});
|
|
},
|
|
|
|
/**
|
|
* @param {Access} access
|
|
* @param {Object} data
|
|
* @param {String} data.id
|
|
* @return {Promise}
|
|
*/
|
|
get: (access, data) => {
|
|
return access
|
|
.can("settings:get", data.id)
|
|
.then(() => {
|
|
return settingModel.query().where("id", data.id).first();
|
|
})
|
|
.then((row) => {
|
|
if (row) {
|
|
return row;
|
|
}
|
|
throw new errs.ItemNotFoundError(data.id);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* This will only count the settings
|
|
*
|
|
* @param {Access} access
|
|
* @returns {*}
|
|
*/
|
|
getCount: (access) => {
|
|
return access
|
|
.can("settings:list")
|
|
.then(() => {
|
|
return settingModel.query().count("id as count").first();
|
|
})
|
|
.then((row) => {
|
|
return Number.parseInt(row.count, 10);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* All settings
|
|
*
|
|
* @param {Access} access
|
|
* @returns {Promise}
|
|
*/
|
|
getAll: (access) => {
|
|
return access.can("settings:list").then(() => {
|
|
return settingModel.query().orderBy("description", "ASC");
|
|
});
|
|
},
|
|
};
|
|
|
|
export default internalSetting;
|