Skip to content

Commit

Permalink
Optionally support multiple listen directives (#3)
Browse files Browse the repository at this point in the history
* Optionally support multiple `listen` directives

* Check that `listen` is defined

Co-authored-by: Jan Heuermann <[email protected]>
  • Loading branch information
jotaen4tinypilot and jotaen authored Jul 6, 2021
1 parent 570b58f commit 9f5821a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ Please take note of the indentation in the above block. The first line should be
return: "301 https://example.com$request_uri"
filename: "example.com.80.conf"

`listen` can also be an array of strings, which will result in multiple `listen` directives for that block.

- listen: ["80", "8080"]
server_name: "example.com www.example.com"
return: "301 https://example.com$request_uri"
filename: "example.com.80.conf"

An example of a secondary vhost which will redirect to the one shown above.

*Note: The `filename` defaults to the first domain in `server_name`, if you have two vhosts with the same domain, eg. a redirect, you need to manually set the `filename` so the second one doesn't override the first one*
Expand Down
26 changes: 22 additions & 4 deletions templates/vhost.j2
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{% block server_redirect %}
{% if item.server_name_redirect is defined %}
server {
listen {{ item.listen | default('80') }};
listen [::]:{{item.listen | default('80') }};
{% if item.listen is defined and
item.listen is not string and
item.listen is iterable %}
{% for listen in item.listen -%}
listen {{ listen }};
listen [::]:{{ listen }};
{% endfor %}
{% else %}
listen {{ item.listen | default('80') }};
listen [::]:{{item.listen | default('80') }};
{% endif %}
server_name {{ item.server_name_redirect }};
return 301 $scheme://{{ item.server_name.split(' ')[0] }}$request_uri;
}
Expand All @@ -13,8 +22,17 @@ server {
{% block server_begin %}{% endblock %}

{% block server_basic -%}
listen {{ item.listen | default('80') }};
listen [::]:{{item.listen | default('80') }};
{% if item.listen is defined and
item.listen is not string and
item.listen is iterable %}
{% for listen in item.listen -%}
listen {{ listen }};
listen [::]:{{ listen }};
{% endfor %}
{% else %}
listen {{ item.listen | default('80') }};
listen [::]:{{item.listen | default('80') }};
{% endif %}

{% if item.server_name is defined %}
server_name {{ item.server_name }};
Expand Down

0 comments on commit 9f5821a

Please sign in to comment.