mirror of
https://github.com/HanXHX/ansible-nginx.git
synced 2026-02-28 09:22:10 +07:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
97c3d2e169 | ||
|
|
c2644d4e5d | ||
|
|
70529e223d | ||
|
|
803f435b48 | ||
|
|
710b3ad53b | ||
|
|
bf1b9950cb | ||
|
|
050c16401f |
12
README.md
12
README.md
@@ -5,7 +5,7 @@ Nginx for Debian Ansible role
|
|||||||
|
|
||||||
Install and configure Nginx on Debian.
|
Install and configure Nginx on Debian.
|
||||||
|
|
||||||
This role is not production ready. SSL management wille come later.
|
SSL management will come later.
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
------------
|
------------
|
||||||
@@ -53,7 +53,7 @@ You can see many examples in: [tests/test.yml](tests/test.yml).
|
|||||||
|
|
||||||
#### Common
|
#### Common
|
||||||
|
|
||||||
- `name`: (M) List of domain used. The first occurence is the most important!
|
- `name`: (M) Domain or list of domain used.
|
||||||
- `template`: (M) template used to create vhost
|
- `template`: (M) template used to create vhost
|
||||||
- `enable`: (O) Enable the vhost (default is true)
|
- `enable`: (O) Enable the vhost (default is true)
|
||||||
- `delete`: (O) Delete the vhost (default is false)
|
- `delete`: (O) Delete the vhost (default is false)
|
||||||
@@ -61,6 +61,8 @@ You can see many examples in: [tests/test.yml](tests/test.yml).
|
|||||||
- `location`: (O) Add new custom locations (it does not overwrite!)
|
- `location`: (O) Add new custom locations (it does not overwrite!)
|
||||||
- `more`: (O) Add more custom infos.
|
- `more`: (O) Add more custom infos.
|
||||||
- `upstream_params`: (O) Add upstream params (useful when you want to pass variables to PHP)
|
- `upstream_params`: (O) Add upstream params (useful when you want to pass variables to PHP)
|
||||||
|
- `override_try_files`: (O) overrides default try\_files defined in template
|
||||||
|
- `manage_local_content`: (O) Boolean. Set to false if you don't want to manage local content (images, css...). This option is useless if you use proxy `template`
|
||||||
|
|
||||||
(O) : Optional
|
(O) : Optional
|
||||||
(M) : Mandatory
|
(M) : Mandatory
|
||||||
@@ -68,10 +70,12 @@ You can see many examples in: [tests/test.yml](tests/test.yml).
|
|||||||
#### Templates
|
#### Templates
|
||||||
|
|
||||||
- `base`: static template
|
- `base`: static template
|
||||||
- `php`: PHP base template. Can work with many frameworks/tools.
|
|
||||||
- `wordpress`
|
|
||||||
- `dokuwiki`
|
- `dokuwiki`
|
||||||
|
- `phalcon`: Phalcon PHP Framework
|
||||||
|
- `php`: PHP base template. Can work with many frameworks/tools
|
||||||
|
- `php_index`: Same as above. But you can only run index.php
|
||||||
- `proxy`
|
- `proxy`
|
||||||
|
- `wordpress`
|
||||||
|
|
||||||
Templates works as parent-child.
|
Templates works as parent-child.
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
- name: FILE | Create root folders (foreach nginx_vhosts)
|
- name: FILE | Create root folders (foreach nginx_vhosts)
|
||||||
file: >
|
file: >
|
||||||
path={{ nginx_root }}/{{ item.name[0] }}/public
|
path={{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}/public
|
||||||
state=directory
|
state=directory
|
||||||
owner={{ item.owner | default('www-data') }}
|
owner={{ item.owner | default('www-data') }}
|
||||||
group={{ item.group | default('www-data') }}
|
group={{ item.group | default('www-data') }}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
- name: TEMPLATE | Create vhosts
|
- name: TEMPLATE | Create vhosts
|
||||||
template: >
|
template: >
|
||||||
src=etc/nginx/sites-available/{{ item.template }}.j2
|
src=etc/nginx/sites-available/{{ item.template }}.j2
|
||||||
dest=/etc/nginx/sites-available/{{ item.name[0] }}
|
dest=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }}
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
notify: reload nginx
|
notify: reload nginx
|
||||||
when: item.delete is not defined or not item.delete
|
when: item.delete is not defined or not item.delete
|
||||||
@@ -23,16 +23,16 @@
|
|||||||
# with_fileglob: "web/*"
|
# with_fileglob: "web/*"
|
||||||
|
|
||||||
- name: FILE | Delete vhosts
|
- name: FILE | Delete vhosts
|
||||||
file: dest=/etc/nginx/sites-enabled/{{ item.name[0] }} state=absent
|
file: dest=/etc/nginx/sites-enabled/{{ item.name if item.name is string else item.name[0] }} state=absent
|
||||||
file: dest=/etc/nginx/sites-available/{{ item.name[0] }} state=absent
|
file: dest=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }} state=absent
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
notify: reload nginx
|
notify: reload nginx
|
||||||
when: item.delete is defined and item.delete
|
when: item.delete is defined and item.delete
|
||||||
|
|
||||||
- name: FILE | Enable vhosts
|
- name: FILE | Enable vhosts
|
||||||
file: >
|
file: >
|
||||||
src=/etc/nginx/sites-available/{{ item.name[0] }}
|
src=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }}
|
||||||
dest=/etc/nginx/sites-enabled/{{ item.name[0] }}
|
dest=/etc/nginx/sites-enabled/{{ item.name if item.name is string else item.name[0] }}
|
||||||
state=link
|
state=link
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
notify: reload nginx
|
notify: reload nginx
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
(item.delete is not defined or not item.delete)
|
(item.delete is not defined or not item.delete)
|
||||||
|
|
||||||
- name: FILE | Disable vhosts
|
- name: FILE | Disable vhosts
|
||||||
file: dest=/etc/nginx/sites-enabled/{{ item.name[0] }} state=absent
|
file: dest=/etc/nginx/sites-enabled/{{ item.name if item.name is string else item.name[0] }} state=absent
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
notify: reload nginx
|
notify: reload nginx
|
||||||
when: item.enable is defined and not item.enable
|
when: item.enable is defined and not item.enable
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ server {
|
|||||||
{% for port in __listen %}
|
{% for port in __listen %}
|
||||||
listen {{ port }};
|
listen {{ port }};
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
server_name {{ item.name | join(' ') }};
|
server_name {% if item.name is string %}{{ item.name }}{% else %}{{ item.name | join(' ') }}{% endif %};
|
||||||
{% if item.root is defined %}
|
{% if item.root is defined %}
|
||||||
root {{ item.root }};
|
root {{ item.root }};
|
||||||
{% else %}
|
{% else %}
|
||||||
root {{ nginx_root }}/{{ item.name[0] }}/public;
|
root {{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}/public;
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% block template_index %}
|
{% block template_index %}
|
||||||
index {{ item.index | default('index.html index.htm') }};
|
index {{ item.index | default('index.html index.htm') }};
|
||||||
@@ -30,7 +30,7 @@ server {
|
|||||||
|
|
||||||
location / {
|
location / {
|
||||||
{% block template_try_files %}
|
{% block template_try_files %}
|
||||||
try_files $uri $uri/ =404;
|
try_files {{ override_try_files | default('$uri $uri/ =404') }};
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{% block template_local_content %}
|
{% block template_local_content %}
|
||||||
|
{% if item.manage_local_content is not defined or item.manage_local_content %}
|
||||||
location = /favicon.ico {
|
location = /favicon.ico {
|
||||||
expires 30d;
|
expires 30d;
|
||||||
access_log off;
|
access_log off;
|
||||||
@@ -54,6 +55,7 @@ server {
|
|||||||
expires 30d;
|
expires 30d;
|
||||||
log_not_found off;
|
log_not_found off;
|
||||||
}
|
}
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% if __location is iterable and __location | length > 0 %}
|
{% if __location is iterable and __location | length > 0 %}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block template_try_files %}
|
{% block template_try_files %}
|
||||||
try_files $uri $uri/ /index.php;
|
try_files {{ override_try_files | default('$uri $uri/ /index.php') }};
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block template_upstream_location %}
|
{% block template_upstream_location %}
|
||||||
|
|||||||
24
templates/etc/nginx/sites-available/_php_index.j2
Normal file
24
templates/etc/nginx/sites-available/_php_index.j2
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{% extends "_php.j2" %}
|
||||||
|
|
||||||
|
{% block template_upstream_location %}
|
||||||
|
location = /index.php {
|
||||||
|
fastcgi_pass php;
|
||||||
|
fastcgi_index index.php;
|
||||||
|
{% if item.upstream_params is defined and item.upstream_params is iterable %}
|
||||||
|
{% for param in item.upstream_params %}
|
||||||
|
{{ param }}
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
{% if nginx_version.stdout | version_compare('1.6.1', 'lt') %}
|
||||||
|
include fastcgi_params;
|
||||||
|
{% else %}
|
||||||
|
include fastcgi.conf;
|
||||||
|
{% endif %}
|
||||||
|
}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block template_custom_location %}
|
||||||
|
location ~ \.(php\d?|phtml)$ {
|
||||||
|
return 403;
|
||||||
|
}
|
||||||
|
{% endblock %}
|
||||||
@@ -21,6 +21,8 @@
|
|||||||
- 'test-alias.local'
|
- 'test-alias.local'
|
||||||
- 'test2-alias.local'
|
- 'test2-alias.local'
|
||||||
template: '_base'
|
template: '_base'
|
||||||
|
override_try_files: '$uri $uri index.htm index.html'
|
||||||
|
manage_local_content: false
|
||||||
more:
|
more:
|
||||||
- 'autoindex off;'
|
- 'autoindex off;'
|
||||||
location:
|
location:
|
||||||
@@ -28,37 +30,44 @@
|
|||||||
- 'return 403;'
|
- 'return 403;'
|
||||||
'/gunther':
|
'/gunther':
|
||||||
- 'return 404;'
|
- 'return 404;'
|
||||||
- name:
|
- name: 'test-php.local'
|
||||||
- 'test-php.local'
|
|
||||||
upstream_params:
|
upstream_params:
|
||||||
- 'fastcgi_param FOO bar;'
|
- 'fastcgi_param FOO bar;'
|
||||||
redirect_from:
|
redirect_from:
|
||||||
- 'www.test-php.local'
|
- 'www.test-php.local'
|
||||||
template: '_php'
|
template: '_php'
|
||||||
- name:
|
- name: 'test-php-index.local'
|
||||||
- 'test-proxy.local'
|
template: '_php_index'
|
||||||
|
- name: 'test-proxy.local'
|
||||||
listen:
|
listen:
|
||||||
- 8080
|
- 8080
|
||||||
template: '_proxy'
|
template: '_proxy'
|
||||||
upstream_name: 'test'
|
upstream_name: 'test'
|
||||||
- name:
|
- name: 'deleted.local'
|
||||||
- 'deleted.local'
|
|
||||||
template: '_base'
|
template: '_base'
|
||||||
delete: true
|
delete: true
|
||||||
roles:
|
roles:
|
||||||
- ../../
|
- ../../
|
||||||
post_tasks:
|
post_tasks:
|
||||||
- name: -- Add PHP file --
|
- name: -- Add PHP file --
|
||||||
copy: dest="{{ nginx_root }}/test-php.local/public/index.php" content="<?php phpinfo();"
|
copy: dest="{{ nginx_root }}/{{ item }}/public/index.php" content="<?php phpinfo();"
|
||||||
|
with_items: ['test-php.local', 'test-php-index.local']
|
||||||
- name: -- Add HTML file --
|
- name: -- Add HTML file --
|
||||||
copy: dest="{{ nginx_root }}/test.local/public/index.html" content="Index HTML test OK\n"
|
copy: dest="{{ nginx_root }}/test.local/public/index.html" content="Index HTML test OK\n"
|
||||||
- name: -- VERIFY VHOSTS --
|
- name: -- VERIFY VHOSTS --
|
||||||
shell: "curl -H 'Host: {{ item.name[0] }}' http://127.0.0.1{% if item.listen is defined and item.listen is iterable %}:{{ item.listen[0] }}{% endif %}/"
|
shell: "curl -H 'Host: {{ item.name if item.name is string else item.name[0] }}' http://127.0.0.1{% if item.listen is defined %}:{{ item.listen[0] }}{% endif %}/"
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
when: item.delete is undefined or not item.delete
|
when: item.delete is undefined or not item.delete
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
- name: -- VERIFY FORBIDDEN --
|
||||||
|
command: "curl -H 'Host: test-php-index.local' http://127.0.0.1/phpinfo.php"
|
||||||
|
register: f
|
||||||
|
failed_when: f.stdout.find('403 Forbidden') == -1
|
||||||
|
changed_when: false
|
||||||
- name: -- VERIFY REDIRECT VHOSTS --
|
- name: -- VERIFY REDIRECT VHOSTS --
|
||||||
shell: "curl -H 'Host: {{ item.redirect_from[0] }}' http://127.0.0.1/"
|
shell: "curl -H 'Host: {{ item.redirect_from[0] }}' http://127.0.0.1/"
|
||||||
with_items: nginx_vhosts
|
with_items: nginx_vhosts
|
||||||
when: item.redirect_from is defined and (item.delete is undefined or not item.delete)
|
when: item.redirect_from is defined and (item.delete is undefined or not item.delete)
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
register: r
|
||||||
|
failed_when: r.stdout.find('301 Moved Permanently') == -1
|
||||||
|
|||||||
Reference in New Issue
Block a user