diff --git a/setup/start/nginx/nginx.conf b/setup/start/nginx/nginx.conf index cd246b0b4..ce6803ab2 100644 --- a/setup/start/nginx/nginx.conf +++ b/setup/start/nginx/nginx.conf @@ -39,4 +39,5 @@ http { limit_req_zone $binary_remote_addr zone=admin_login:10m rate=10r/s; # 10 request a second include applications/*.conf; + include applications/*/*.conf; } diff --git a/src/reverseproxy.js b/src/reverseproxy.js index e7dde99fb..c7ecc7084 100644 --- a/src/reverseproxy.js +++ b/src/reverseproxy.js @@ -525,7 +525,7 @@ async function writeAppNginxConfig(app, vhost, type, certificatePath) { } const nginxConf = ejs.render(NGINX_APPCONFIG_EJS, data); - const filename = path.join(paths.NGINX_APPCONFIG_DIR, `${app.id}-${vhost.replace('*', '_')}.conf`); + const filename = path.join(paths.NGINX_APPCONFIG_DIR, app.id, `${vhost.replace('*', '_')}.conf`); debug(`writeAppNginxConfig: writing config for "${vhost}" to ${filename} with options ${JSON.stringify(data)}`); if (!safe.fs.writeFileSync(filename, nginxConf)) { debug(`Error creating nginx config for "${app.fqdn}" : ${safe.error.message}`); @@ -538,6 +538,8 @@ async function writeAppConfigs(app) { const locations = apps.getLocationsSync(app); + if (!safe.fs.mkdirSync(path.join(paths.NGINX_APPCONFIG_DIR, app.id), { recursive: true })) throw new BoxError(BoxError.FS_ERROR, `Could not create nginx config directory: ${safe.error.message}`); + for (const location of locations) { const certificatePath = await getCertificatePath(location.fqdn, location.domain); await writeAppNginxConfig(app, location.fqdn, location.type, certificatePath); @@ -581,15 +583,7 @@ async function configureApp(app, auditSource) { async function unconfigureApp(app) { assert.strictEqual(typeof app, 'object'); - const configFilenames = safe.fs.readdirSync(paths.NGINX_APPCONFIG_DIR); - if (!configFilenames) throw new BoxError(BoxError.FS_ERROR, `Error loading nginx config files: ${safe.error.message}`); - - for (const filename of configFilenames) { - if (!filename.startsWith(app.id)) continue; - - safe.fs.unlinkSync(path.join(paths.NGINX_APPCONFIG_DIR, filename)); - } - + if (!safe.fs.rmSync(path.join(paths.NGINX_APPCONFIG_DIR, app.id), { recursive: true, force: true })) throw new BoxError(BoxError.FS_ERROR, `Could not remove nginx config directory: ${safe.error.message}`); await reload(); } @@ -734,12 +728,15 @@ async function checkCerts(auditSource, progressCallback) { function removeAppConfigs() { const dashboardConfigFilename = `${settings.dashboardFqdn()}.conf`; - debug('removeAppConfigs: reomving nginx configs of apps'); + debug('removeAppConfigs: removing app nginx configs'); // remove all configs which are not the default or current dashboard - for (const appConfigFile of fs.readdirSync(paths.NGINX_APPCONFIG_DIR)) { - if (appConfigFile !== constants.NGINX_DEFAULT_CONFIG_FILE_NAME && appConfigFile !== dashboardConfigFilename) { - fs.unlinkSync(path.join(paths.NGINX_APPCONFIG_DIR, appConfigFile)); + for (const entry of fs.readdirSync(paths.NGINX_APPCONFIG_DIR, { withFileTypes: true })) { + const fullPath = path.join(paths.NGINX_APPCONFIG_DIR, entry.name); + if (entry.isDirectory()) { + fs.rmSync(fullPath, { recursive: true, force: true }); + } else if (entry.isFile() && entry.name !== constants.NGINX_DEFAULT_CONFIG_FILE_NAME && entry.name !== dashboardConfigFilename) { + fs.unlinkSync(fullPath); } } }