From 8de118d875e8d7484e2b2a8f312e5f1797584f69 Mon Sep 17 00:00:00 2001 From: Nikolaj Frey Date: Fri, 14 Aug 2020 15:00:22 +1000 Subject: [PATCH] Backend schema and migration modifications for allowing static hosts and locations --- backend/migrations/20180618015850_initial.js | 3 + backend/schema/definitions.json | 5 ++ backend/schema/endpoints/proxy-hosts.json | 60 +++++++++++++++++--- backend/templates/_listen.conf | 5 ++ backend/templates/_location.conf | 17 ++++-- backend/templates/default.conf | 1 + backend/templates/proxy_host.conf | 10 +++- 7 files changed, 85 insertions(+), 16 deletions(-) diff --git a/backend/migrations/20180618015850_initial.js b/backend/migrations/20180618015850_initial.js index a112e826..b0790870 100644 --- a/backend/migrations/20180618015850_initial.js +++ b/backend/migrations/20180618015850_initial.js @@ -69,6 +69,9 @@ exports.up = function (knex/*, Promise*/) { table.json('domain_names').notNull(); table.string('forward_ip').notNull(); table.integer('forward_port').notNull().unsigned(); + table.string('root_dir').notNull(); + table.string('index_file').notNull(); + table.integer('static').notNull().unsigned().defaultTo(0); table.integer('access_list_id').notNull().unsigned().defaultTo(0); table.integer('certificate_id').notNull().unsigned().defaultTo(0); table.integer('ssl_forced').notNull().unsigned().defaultTo(0); diff --git a/backend/schema/definitions.json b/backend/schema/definitions.json index 2aa538b2..164dd6fc 100644 --- a/backend/schema/definitions.json +++ b/backend/schema/definitions.json @@ -222,6 +222,11 @@ "description": "Should we cache assets", "example": true, "type": "boolean" + }, + "static": { + "description": "Should the proxy point to static files", + "example": true, + "type": "boolean" } } } diff --git a/backend/schema/endpoints/proxy-hosts.json b/backend/schema/endpoints/proxy-hosts.json index 9a3fff2f..94c0ba7a 100644 --- a/backend/schema/endpoints/proxy-hosts.json +++ b/backend/schema/endpoints/proxy-hosts.json @@ -24,14 +24,22 @@ }, "forward_host": { "type": "string", - "minLength": 1, + "minLength": 0, "maxLength": 255 }, "forward_port": { "type": "integer", - "minimum": 1, + "minimum": 0, "maximum": 65535 }, + "root_dir": { + "type": "string", + "minLength": 0, + }, + "index_file": { + "type": "string", + "minLength": 0, + }, "certificate_id": { "$ref": "../definitions.json#/definitions/certificate_id" }, @@ -53,6 +61,9 @@ "caching_enabled": { "$ref": "../definitions.json#/definitions/caching_enabled" }, + "static": { + "$ref": "../definitions.json#/definitions/static" + }, "allow_websocket_upgrade": { "description": "Allow Websocket Upgrade for all paths", "example": true, @@ -76,10 +87,7 @@ "items": { "type": "object", "required": [ - "forward_scheme", - "forward_host", - "forward_port", - "path" + "forward_scheme" ], "additionalProperties": false, "properties": { @@ -99,6 +107,15 @@ "forward_port": { "$ref": "#/definitions/forward_port" }, + "root_dir": { + "$ref": "#/definitions/root_dir" + }, + "index_file": { + "$ref": "#/definitions/index_file" + }, + "static": { + "$ref": "#/definitions/static" + }, "forward_path": { "type": "string" }, @@ -131,6 +148,12 @@ "forward_port": { "$ref": "#/definitions/forward_port" }, + "root_dir": { + "$ref": "#/definitions/root_dir" + }, + "index_file": { + "$ref": "#/definitions/index_file" + }, "certificate_id": { "$ref": "#/definitions/certificate_id" }, @@ -152,6 +175,9 @@ "caching_enabled": { "$ref": "#/definitions/caching_enabled" }, + "static": { + "$ref": "#/definitions/static" + }, "allow_websocket_upgrade": { "$ref": "#/definitions/allow_websocket_upgrade" }, @@ -204,9 +230,7 @@ "additionalProperties": false, "required": [ "domain_names", - "forward_scheme", - "forward_host", - "forward_port" + "forward_scheme" ], "properties": { "domain_names": { @@ -221,6 +245,12 @@ "forward_port": { "$ref": "#/definitions/forward_port" }, + "root_dir": { + "$ref": "#/definitions/root_dir" + }, + "index_file": { + "$ref": "#/definitions/index_file" + }, "certificate_id": { "$ref": "#/definitions/certificate_id" }, @@ -242,6 +272,9 @@ "caching_enabled": { "$ref": "#/definitions/caching_enabled" }, + "static": { + "$ref": "#/definitions/static" + }, "allow_websocket_upgrade": { "$ref": "#/definitions/allow_websocket_upgrade" }, @@ -294,6 +327,12 @@ "forward_port": { "$ref": "#/definitions/forward_port" }, + "root_dir": { + "$ref": "#/definitions/root_dir" + }, + "index_file": { + "$ref": "#/definitions/index_file" + }, "certificate_id": { "$ref": "#/definitions/certificate_id" }, @@ -315,6 +354,9 @@ "caching_enabled": { "$ref": "#/definitions/caching_enabled" }, + "static": { + "$ref": "#/definitions/static" + }, "allow_websocket_upgrade": { "$ref": "#/definitions/allow_websocket_upgrade" }, diff --git a/backend/templates/_listen.conf b/backend/templates/_listen.conf index 8f40bea2..08ec5369 100644 --- a/backend/templates/_listen.conf +++ b/backend/templates/_listen.conf @@ -13,3 +13,8 @@ {% endif %} {% endif %} server_name {{ domain_names | join: " " }}; +{% if static == 1 or static == true %} + root {{ root_dir }}; + index {{ index_file }}; +{% endif %} + diff --git a/backend/templates/_location.conf b/backend/templates/_location.conf index 0b8894d1..72f3ea50 100644 --- a/backend/templates/_location.conf +++ b/backend/templates/_location.conf @@ -1,9 +1,16 @@ location {{ path }} { - proxy_set_header Host $host; - proxy_set_header X-Forwarded-Scheme $scheme; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_pass {{ forward_scheme }}://{{ forward_host }}:{{ forward_port }}{{ forward_path }}; + + {% if static == 0 or static == false %} + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Scheme $scheme; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_pass {{ forward_scheme }}://{{ forward_host }}:{{ forward_port }}{{ forward_path }}; + {% else %} + alias {{ root_dir }}/$1; + try_files $uri /{{ index_file }} =200; + {% endif %} + {{ advanced_config }} } diff --git a/backend/templates/default.conf b/backend/templates/default.conf index 56b67090..dfb0b1b8 100644 --- a/backend/templates/default.conf +++ b/backend/templates/default.conf @@ -29,6 +29,7 @@ server { {%- if value == "html" %} root /data/nginx/default_www; + # root /var/www/test2; location / { try_files $uri /index.html; } diff --git a/backend/templates/proxy_host.conf b/backend/templates/proxy_host.conf index 17a52107..6f8ddd02 100644 --- a/backend/templates/proxy_host.conf +++ b/backend/templates/proxy_host.conf @@ -52,8 +52,14 @@ server { proxy_http_version 1.1; {% endif %} - # Proxy! - include conf.d/include/proxy.conf; + {% if static == 1 or static == true %} + alias {{ root_dir }}/$1; + try_files $uri /{{index_file}} =200; + {% else %} + # Proxy! + include conf.d/include/proxy.conf; + + {% endif %} } {% endif %}