From 29bebcc73eba4c3bef76e7f4cab6d516adb40cdd Mon Sep 17 00:00:00 2001 From: jc21 Date: Mon, 25 Feb 2019 10:34:55 +1000 Subject: [PATCH] Ignore default location when defined in advanced config (#79) --- src/backend/internal/nginx.js | 19 ++++++++++++++++--- src/backend/templates/dead_host.conf | 3 +++ src/backend/templates/proxy_host.conf | 3 +++ src/backend/templates/redirection_host.conf | 3 +++ src/frontend/js/app/nginx/proxy/form.ejs | 6 ++++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/backend/internal/nginx.js b/src/backend/internal/nginx.js index a0d84998..fd8fe165 100644 --- a/src/backend/internal/nginx.js +++ b/src/backend/internal/nginx.js @@ -1,5 +1,3 @@ -'use strict'; - const _ = require('lodash'); const fs = require('fs'); const Liquid = require('liquidjs'); @@ -92,7 +90,7 @@ const internalNginx = { }) .then(() => { return combined_meta; - }) + }); }, /** @@ -146,6 +144,7 @@ const internalNginx = { return new Promise((resolve, reject) => { let template = null; let filename = internalNginx.getConfigName(host_type, host.id); + try { template = fs.readFileSync(__dirname + '/../templates/' + host_type + '.conf', {encoding: 'utf8'}); } catch (err) { @@ -153,6 +152,12 @@ const internalNginx = { return; } + // Manipulate the data a bit before sending it to the template + host.use_default_location = true; + if (typeof host.advanced_config !== 'undefined' && host.advanced_config) { + host.use_default_location = !internalNginx.advancedConfigHasDefaultLocation(host.advanced_config); + } + renderEngine .parseAndRender(template, host) .then(config_text => { @@ -312,6 +317,14 @@ const internalNginx = { }); return Promise.all(promises); + }, + + /** + * @param {string} config + * @returns {boolean} + */ + advancedConfigHasDefaultLocation: function (config) { + return !!config.match(/^(?:.*;)?\s*?location\s*?\/\s*?{/im); } }; diff --git a/src/backend/templates/dead_host.conf b/src/backend/templates/dead_host.conf index 8d3534ab..da282a12 100644 --- a/src/backend/templates/dead_host.conf +++ b/src/backend/templates/dead_host.conf @@ -10,10 +10,13 @@ server { {{ advanced_config }} +{% if use_default_location %} location / { {% include "_forced_ssl.conf" %} {% include "_hsts.conf" %} return 404; } +{% endif %} + } {% endif %} diff --git a/src/backend/templates/proxy_host.conf b/src/backend/templates/proxy_host.conf index 52e70583..95f850a9 100644 --- a/src/backend/templates/proxy_host.conf +++ b/src/backend/templates/proxy_host.conf @@ -16,6 +16,7 @@ server { {{ advanced_config }} +{% if use_default_location %} location / { {%- if access_list_id > 0 -%} # Access List @@ -35,5 +36,7 @@ server { # Proxy! include conf.d/include/proxy.conf; } +{% endif %} + } {% endif %} diff --git a/src/backend/templates/redirection_host.conf b/src/backend/templates/redirection_host.conf index 7f55e91b..3e6c2b44 100644 --- a/src/backend/templates/redirection_host.conf +++ b/src/backend/templates/redirection_host.conf @@ -12,6 +12,7 @@ server { {{ advanced_config }} +{% if use_default_location %} location / { {% include "_forced_ssl.conf" %} {% include "_hsts.conf" %} @@ -22,5 +23,7 @@ server { return 301 $scheme://{{ forward_domain_name }}; {% endif %} } +{% endif %} + } {% endif %} diff --git a/src/frontend/js/app/nginx/proxy/form.ejs b/src/frontend/js/app/nginx/proxy/form.ejs index 0962916f..ef9b95b4 100644 --- a/src/frontend/js/app/nginx/proxy/form.ejs +++ b/src/frontend/js/app/nginx/proxy/form.ejs @@ -152,6 +152,12 @@
+

Nginx variables available to you are:

+
    +
  • $server # Host/IP
  • +
  • $port # Port Number
  • +
  • $forward_scheme # http or https
  • +