Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Add support for canonical host #136

Merged
merged 2 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ This allows you to specify a different asset root for the directory of your appl

By default this is set to `public_html/`

#### Canonical Host
This allows you to perform 301 redirects to a specific hostname, which can be useful for redirecting www to non-www (or vice versa).

```json
{
"canonical_host": "www.example.com"
}
```

You can use environment variables as well:

```json
{
"canonical_host": "${HOST}"
}
```

#### Default Character Set
This allows you to specify a character set for your text assets (HTML, Javascript, CSS, and so on). For most apps, this should be the default value of "UTF-8", but you can override it by setting `encoding`:

Expand Down
4 changes: 4 additions & 0 deletions scripts/config/lib/nginx_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class NginxConfig
DEFAULT = {
root: "public_html/",
encoding: "UTF-8",
canonical_host: false,
clean_urls: false,
https_only: false,
basic_auth: false,
Expand All @@ -26,6 +27,9 @@ def initialize(json_file)
json["root"] ||= DEFAULT[:root]
json["encoding"] ||= DEFAULT[:encoding]

json["canonical_host"] ||= DEFAULT[:canonical_host]
json["canonical_host"] = NginxConfigUtil.interpolate(json["canonical_host"], ENV) if json["canonical_host"]

index = 0
json["proxies"] ||= {}
json["proxies"].each do |loc, hash|
Expand Down
6 changes: 6 additions & 0 deletions scripts/config/templates/nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ http {
}
<% end %>

<% if canonical_host %>
if ($host != <%= canonical_host %>) {
return 301 $scheme://<%= canonical_host %>$request_uri;
root-io marked this conversation as resolved.
Show resolved Hide resolved
}
<% end %>

<% routes.each do |route, path| %>
location ~ ^<%= route %>$ {
set $route <%= route %>;
Expand Down