|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
---
|
|
|
|
|
title: "Create a static site hosting platform"
|
|
|
|
|
date: 2020-07-30T01:05:00+02:00
|
|
|
|
|
aliases:
|
|
|
|
|
- /create-a-static-site-hosting-platform/
|
|
|
|
|
authors:
|
|
|
|
|
- robert-kaussow
|
|
|
|
|
tags:
|
|
|
|
@ -8,7 +10,7 @@ tags:
|
|
|
|
|
- Sysadmin
|
|
|
|
|
resources:
|
|
|
|
|
- name: feature
|
|
|
|
|
src: 'images/feature.jpg'
|
|
|
|
|
src: "images/feature.jpg"
|
|
|
|
|
params:
|
|
|
|
|
anchor: Center
|
|
|
|
|
credits: >
|
|
|
|
@ -17,7 +19,9 @@ resources:
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
There are a lot of static site generators out there and users have a lot of possibilities to automate and continuously deploy static sites these days. Solutions like GitHub pages or Netlify are free to use and easy to set up, even a cheap webspace could work. If one of these services is sufficient for your use case you could stop reading at this point.
|
|
|
|
|
|
|
|
|
|
<!--more-->
|
|
|
|
|
|
|
|
|
|
As I wanted to have more control over such a setup and because it might be fun I decided to create my own service. Before we look into the setup details, lets talk about some requirements:
|
|
|
|
|
|
|
|
|
|
- deploy multiple project documentation
|
|
|
|
@ -90,13 +94,13 @@ server {
|
|
|
|
|
|
|
|
|
|
We will go through this configuration to understand how it works.
|
|
|
|
|
|
|
|
|
|
__*Lines 1-3*__ defines a backend, in this case it's the Minio server running on `localhost:61000`.
|
|
|
|
|
**_Lines 1-3_** defines a backend, in this case it's the Minio server running on `localhost:61000`.
|
|
|
|
|
|
|
|
|
|
__*Lines 5-10*__ should also be straight forward, this block will redirect HTTP to HTTPS.
|
|
|
|
|
**_Lines 5-10_** should also be straight forward, this block will redirect HTTP to HTTPS.
|
|
|
|
|
|
|
|
|
|
__*Line 14*__ is where the magic starts. We are using a named regular expression to capture the first part of the subdomain and translate it into the bucket sub-directory. For a given URL like `demoproject.mydocs.com` Nginx will try to serve `mydocs/demoproject` from the Minio server. That's what __*Line 23*__ does. Some of you may notice that the used variable `${request_path}` is not defined in the vHost configuration.
|
|
|
|
|
**_Line 14_** is where the magic starts. We are using a named regular expression to capture the first part of the subdomain and translate it into the bucket sub-directory. For a given URL like `demoproject.mydocs.com` Nginx will try to serve `mydocs/demoproject` from the Minio server. That's what **_Line 23_** does. Some of you may notice that the used variable `${request_path}` is not defined in the vHost configuration.
|
|
|
|
|
|
|
|
|
|
Right, we need to add another configuration snippet to the `nginx.conf`. But why do we need this variable at all? For me, that was the hardest part to solve. As the setup is using `proxy_pass` Nginx will *not* try to lookup `index.html` automatically. That's a problem because every folder will at least contain an `index.html`. In general, it's required to tell Nginx to rewrite the request URI to `/index.html` if the origin is a folder and ends with `/`. One way would be an `if` condition in the vHost configuration but such conditions are evil[^if-is-evil] in most cases and should be avoided if possible. Luckily there is a better option:
|
|
|
|
|
Right, we need to add another configuration snippet to the `nginx.conf`. But why do we need this variable at all? For me, that was the hardest part to solve. As the setup is using `proxy_pass` Nginx will _not_ try to lookup `index.html` automatically. That's a problem because every folder will at least contain an `index.html`. In general, it's required to tell Nginx to rewrite the request URI to `/index.html` if the origin is a folder and ends with `/`. One way would be an `if` condition in the vHost configuration but such conditions are evil[^if-is-evil] in most cases and should be avoided if possible. Luckily there is a better option:
|
|
|
|
|
|
|
|
|
|
<!-- prettier-ignore-start -->
|
|
|
|
|
<!-- markdownlint-disable -->
|
|
|
|
@ -113,7 +117,7 @@ map $request_uri $request_path {
|
|
|
|
|
|
|
|
|
|
[Nginx maps](https://nginx.org/en/docs/http/ngx_http_map_module.html) are a solid way to create conditionals. In this example set `$request_uri` as input and `$request_path` as output. Each line between the braces is a condition. The first line will simply apply `$request_uri` to the output variable if no other condition match. The second condition applies `${request_uri}index.html` to the output variable if the input variable ends with a slash (and therefor is a directory).
|
|
|
|
|
|
|
|
|
|
__*Line 38-41*__ of the vHost configuration tries to deliver the custom error page of your site and will fallback to the default Nginx error page.
|
|
|
|
|
**_Line 38-41_** of the vHost configuration tries to deliver the custom error page of your site and will fallback to the default Nginx error page.
|
|
|
|
|
|
|
|
|
|
We are done! Nginx should now be able to server your static sites from a sub-directory of the Minio source bucket. I'm using it since a few weeks and I'm really happy with the current setup.
|
|
|
|
|
|
|
|
|
|