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

Commit

Permalink
Merge pull request #136 from root-io/canonical-host
Browse files Browse the repository at this point in the history
Add support for canonical host
  • Loading branch information
joshwlewis authored Jul 29, 2019
2 parents bde9522 + b613e77 commit 8bea3b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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 $http_x_forwarded_proto://<%= canonical_host %>$request_uri;
}
<% end %>

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

0 comments on commit 8bea3b3

Please sign in to comment.