Skip to content

Commit

Permalink
Merge pull request geerlingguy#105 from oxyc/vhost-filename
Browse files Browse the repository at this point in the history
Followup geerlingguy#91: Add `filename` option `nginx_vhosts`
  • Loading branch information
geerlingguy authored Jun 3, 2017
2 parents 57889e8 + 3a06154 commit 41f9d4f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ Available variables are listed below, along with default values (see `defaults/m
A list of vhost definitions (server blocks) for Nginx virtual hosts. Each entry will create a separate config file named by `server_name`. If left empty, you will need to supply your own virtual host configuration. See the commented example in `defaults/main.yml` for available server options. If you have a large number of customizations required for your server definition(s), you're likely better off managing the vhost configuration file yourself, leaving this variable set to `[]`.

nginx_vhosts:
- listen: "80 default_server"
- listen: "443 ssl http2"
server_name: "example.com"
server_name_redirect: "www.example.com"
root: "/var/www/example.com"
index: "index.php index.html index.htm"
error_page: ""
access_log: ""
error_log: ""
state: "present"
template: "{{ nginx_vhost_template }}"
filename: "example.com.conf"
extra_parameters: |
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
Expand All @@ -36,11 +38,24 @@ A list of vhost definitions (server blocks) for Nginx virtual hosts. Each entry
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

An example of a fully-populated nginx_vhosts entry, using a `|` to declare a block of syntax for the `extra_parameters`.

Please take note of the indentation in the above block. The first line should be a normal 2-space indent. All other lines should be indented normally relative to that line. In the generated file, the entire block will be 4-space indented. This style will ensure the config file is indented correctly.

- listen: "80"
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*

nginx_remove_default_vhost: false

Whether to remove the 'default' virtualhost configuration supplied by Nginx. Useful if you want the base `/` URL to be directed at one of your own virtual hosts configured in a separate .conf file.
Expand Down
4 changes: 3 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ nginx_extra_http_options: ""
nginx_remove_default_vhost: false
nginx_vhosts: []
# Example vhost below, showing all available options:
# - listen: "80 default_server" # default: "80 default_server"
# - listen: "80" # default: "80"
# server_name: "example.com" # default: N/A
# root: "/var/www/example.com" # default: N/A
# index: "index.html index.htm" # default: "index.html index.htm"
# filename: "example.com.conf" # Can be used to set the filename of the vhost file.
#
# # Properties that are only added if defined:
# server_name_redirect: "www.example.com" # default: N/A
# error_page: ""
# access_log: ""
# error_log: ""
Expand Down
4 changes: 2 additions & 2 deletions tasks/vhosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- name: Add managed vhost config files.
template:
src: "{{ item.template|default(nginx_vhost_template) }}"
dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
dest: "{{ nginx_vhost_path }}/{{ item.filename|default(item.server_name.split(' ')[0] ~ '.conf') }}"
force: yes
owner: root
group: root
Expand All @@ -26,7 +26,7 @@

- name: Remove managed vhost config files.
file:
path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
path: "{{ nginx_vhost_path }}/{{ item.filename|default(item.server_name.split(' ')[0] ~ '.conf') }}"
state: absent
when: item.state|default('present') == 'absent'
with_items: "{{ nginx_vhosts }}"
Expand Down
5 changes: 3 additions & 2 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

pre_tasks:
- name: Update apt cache.
apt: update_cache=yes cache_valid_time=86400
apt: update_cache=yes cache_valid_time=600
when: ansible_os_family == 'Debian'
changed_when: false

- name: Install dependencies.
package: name=curl
package: name=curl state=present

roles:
- role_under_test

0 comments on commit 41f9d4f

Please sign in to comment.