Ignore default location when defined in advanced config (#79)

This commit is contained in:
jc21 2019-02-25 10:34:55 +10:00 committed by GitHub
parent 26064b20b8
commit 29bebcc73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 3 deletions

View File

@ -1,5 +1,3 @@
'use strict';
const _ = require('lodash'); const _ = require('lodash');
const fs = require('fs'); const fs = require('fs');
const Liquid = require('liquidjs'); const Liquid = require('liquidjs');
@ -92,7 +90,7 @@ const internalNginx = {
}) })
.then(() => { .then(() => {
return combined_meta; return combined_meta;
}) });
}, },
/** /**
@ -146,6 +144,7 @@ const internalNginx = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let template = null; let template = null;
let filename = internalNginx.getConfigName(host_type, host.id); let filename = internalNginx.getConfigName(host_type, host.id);
try { try {
template = fs.readFileSync(__dirname + '/../templates/' + host_type + '.conf', {encoding: 'utf8'}); template = fs.readFileSync(__dirname + '/../templates/' + host_type + '.conf', {encoding: 'utf8'});
} catch (err) { } catch (err) {
@ -153,6 +152,12 @@ const internalNginx = {
return; 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 renderEngine
.parseAndRender(template, host) .parseAndRender(template, host)
.then(config_text => { .then(config_text => {
@ -312,6 +317,14 @@ const internalNginx = {
}); });
return Promise.all(promises); return Promise.all(promises);
},
/**
* @param {string} config
* @returns {boolean}
*/
advancedConfigHasDefaultLocation: function (config) {
return !!config.match(/^(?:.*;)?\s*?location\s*?\/\s*?{/im);
} }
}; };

View File

@ -10,10 +10,13 @@ server {
{{ advanced_config }} {{ advanced_config }}
{% if use_default_location %}
location / { location / {
{% include "_forced_ssl.conf" %} {% include "_forced_ssl.conf" %}
{% include "_hsts.conf" %} {% include "_hsts.conf" %}
return 404; return 404;
} }
{% endif %}
} }
{% endif %} {% endif %}

View File

@ -16,6 +16,7 @@ server {
{{ advanced_config }} {{ advanced_config }}
{% if use_default_location %}
location / { location / {
{%- if access_list_id > 0 -%} {%- if access_list_id > 0 -%}
# Access List # Access List
@ -35,5 +36,7 @@ server {
# Proxy! # Proxy!
include conf.d/include/proxy.conf; include conf.d/include/proxy.conf;
} }
{% endif %}
} }
{% endif %} {% endif %}

View File

@ -12,6 +12,7 @@ server {
{{ advanced_config }} {{ advanced_config }}
{% if use_default_location %}
location / { location / {
{% include "_forced_ssl.conf" %} {% include "_forced_ssl.conf" %}
{% include "_hsts.conf" %} {% include "_hsts.conf" %}
@ -22,5 +23,7 @@ server {
return 301 $scheme://{{ forward_domain_name }}; return 301 $scheme://{{ forward_domain_name }};
{% endif %} {% endif %}
} }
{% endif %}
} }
{% endif %} {% endif %}

View File

@ -152,6 +152,12 @@
<div role="tabpanel" class="tab-pane" id="advanced"> <div role="tabpanel" class="tab-pane" id="advanced">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<p>Nginx variables available to you are:</p>
<ul class="text-monospace">
<li>$server # Host/IP</li>
<li>$port # Port Number</li>
<li>$forward_scheme # http or https</li>
</ul>
<div class="form-group mb-0"> <div class="form-group mb-0">
<label class="form-label"><%- i18n('all-hosts', 'advanced-config') %></label> <label class="form-label"><%- i18n('all-hosts', 'advanced-config') %></label>
<textarea name="advanced_config" rows="8" class="form-control text-monospace" placeholder="# <%- i18n('all-hosts', 'advanced-warning') %>"><%- advanced_config %></textarea> <textarea name="advanced_config" rows="8" class="form-control text-monospace" placeholder="# <%- i18n('all-hosts', 'advanced-warning') %>"><%- advanced_config %></textarea>