30 Commits
1.0.2 ... 1.0.9

Author SHA1 Message Date
Emilien Mantel
0d65cd3c5c Add redirect_to_code in vhost 2015-12-01 16:40:34 +01:00
Emilien Mantel
9779555ac8 Few minor changes 2015-12-01 16:35:56 +01:00
Emilien Mantel
17905edf30 Begin Stretch support 2015-12-01 16:26:16 +01:00
Emilien Mantel
f2c6ec6c57 Travis badge on master branch 2015-12-01 15:52:10 +01:00
Emilien Mantel
80e3cae22e New feature on vhosts: "redirect_to" 2015-12-01 15:46:57 +01:00
Emilien Mantel
3fdc16578c Improve file deletions 2015-11-30 14:41:35 +01:00
Emilien Mantel
1d56fc249d Deleted vhost don't need a template 2015-11-30 12:16:17 +01:00
Emilien Mantel
bfe4a315f9 Vhost dir 2015-11-05 15:38:18 +01:00
Emilien Mantel
9fcba824f5 Create root directory 2015-11-04 09:16:59 +01:00
Emilien Mantel
d1dbdb83d4 Fix idempotence 2015-11-03 15:09:14 +01:00
Emilien Mantel
bee0cb9a66 Remove template validation 2015-11-03 14:54:35 +01:00
Emilien Mantel
6be44c7da8 Many minor changes 2015-11-03 14:45:01 +01:00
Emilien Mantel
a0a6e35a3a Only reload nginx 2015-11-03 14:38:47 +01:00
Emilien Mantel
f57ab08594 location / can be overwrited 2015-11-03 14:31:50 +01:00
Emilien Mantel
d26a6f26f5 shell module is not necessary 2015-11-02 17:44:09 +01:00
Emilien Mantel
97c3d2e169 Fix and improve tests/vhosts + php_index vhost 2015-11-02 16:30:46 +01:00
Emilien Mantel
c2644d4e5d update readme 2015-10-22 15:46:21 +02:00
Emilien Mantel
70529e223d nginx_vhosts[x].name now accepts list or string 2015-10-22 10:25:30 +02:00
Emilien Mantel
803f435b48 Bug bix with local_content 2015-10-19 11:03:05 +02:00
Emilien Mantel
710b3ad53b New vhost option: manage_local_content 2015-10-19 09:39:51 +02:00
Emilien Mantel
bf1b9950cb Merge pull request #6 from HanXHX/tryfiles
Add feature: override_try_files on vhost
2015-10-17 08:14:12 +02:00
Emilien Mantel
050c16401f Add feature: override_try_files on vhost 2015-10-15 16:21:36 +02:00
Emilien Mantel
eaf4032bc0 Fix jessie and add doc 2015-10-09 17:54:07 +02:00
Emilien Mantel
63fee94d90 Fix boolean values on upstream 2015-10-09 14:36:55 +02:00
Emilien Mantel
a50d7e8774 Test proxy with a real back-end: himself 2015-10-09 14:31:01 +02:00
Emilien Mantel
96bbc47d27 First shot proxy (unstable) 2015-10-08 18:21:40 +02:00
Emilien Mantel
a9a898e910 Test travis docker 2015-10-07 19:31:12 +02:00
Emilien Mantel
edb36acd5d Fix badge 2015-10-07 19:25:57 +02:00
Emilien Mantel
fb0b210d45 Update badge 2015-10-07 19:19:46 +02:00
Emilien Mantel
a02b44a2f0 Add Ansible Galaxy badge 2015-10-07 19:19:09 +02:00
20 changed files with 278 additions and 62 deletions

View File

@@ -1,11 +1,13 @@
env:
- PLATFORM=debian-wheezy
- PLATFORM=debian-jessie
- PLATFORM=debian-wheezy
- PLATFORM=debian-jessie
sudo: true
sudo: required
install:
- curl -sLo - http://j.mp/install-travis-docker | sh -xe
language: python
services:
- docker
script:
- ./run 'docker build -f tests/$PLATFORM.Dockerfile -t test-$PLATFORM . && docker run --name $PLATFORM test-$PLATFORM'
- docker build -f tests/$PLATFORM.Dockerfile -t test-$PLATFORM . && docker run --name $PLATFORM test-$PLATFORM

View File

@@ -1,11 +1,11 @@
Nginx for Debian Ansible role
=============================
[![Build Status](https://travis-ci.org/HanXHX/ansible-nginx.svg)](https://travis-ci.org/HanXHX/ansible-nginx)
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-HanXHX.nginx-blue.svg)](https://galaxy.ansible.com/list#/roles/4399) [![Build Status](https://travis-ci.org/HanXHX/ansible-nginx.svg?branch=master)](https://travis-ci.org/HanXHX/ansible-nginx)
Install and configure Nginx on Debian.
This role is not production ready. SSL management wille come later.
SSL management will come later.
Requirements
------------
@@ -44,35 +44,83 @@ Socket:
- `nginx_worker_processes`
- `nginx_events`: key/value in events block
- `nginx_http`: key/value in http block
- `nginx_pid`: daemon pid file
### Vhost management
You can see many examples in: [tests/test.yml](tests/test.yml).
- `nginx_vhosts`: List of dict. A vhost has few keys. See bellow.
####
#### Common
- `name`: (M) List of domain used. The first occurence is the most important!
- `template`: (M) template used to create vhost
- `name`: (M) Domain or list of domain used.
- `template`: (D) template used to create vhost. Optional if you set `delete` to true or using `redirect_tor`.
- `enable`: (O) Enable the vhost (default is true)
- `delete`: (O) Delete the vhost (default is false)
- `redirect_from`: (O) Domain list to redirect to the first `name`. You can use this key to redirect non-www to www
- `redirect_to`: (O) Redirect all requests to this domain. Please set scheme (http:// or https:// or $sheme).
- `redirect_to_code`: Redirect code (default: 302)
- `location`: (O) Add new custom locations (it does not overwrite!)
- `more`: (O) Add more custom infos.
- `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 or `redirect_to` feature.
(O): Optional
(M): Mandatory
(D): Depends other keys...
#### Templates
- `_base`: static template
- `_dokuwiki`
- `_redirect`: should not be called explicitly
- `_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`
- `_wordpress`
Templates works as parent-child.
#### About proxy template
Proxy template allow you to use Nginx as reverse proxy. Usefull when you have application serveur such as Redmine, Jenkins...
You have many key added to vhost key:
- `upstream_name`: (O) upstream name used to pass proxy
- `proxy_params`: (M) list of raw params passed to the vhost
(O) : Optional
(M) : Mandatory
#### Templates
### Upstream management
- `base`: static template
- `php`: PHP base template. Can work with many frameworks/tools.
- `wordpress`
- `dokuwiki`
- `nginx_upstreams`: List of dict. An upstream has few keys. See bellow.
Templates works as parent-child.
Note: Few params are unavailable on old Nginx version. But this role don't put it if your version is too old!
You can see many examples in: [tests/test.yml](tests/test.yml).
#### Upstream params
- `name`: upstream name. Can be use in vhost with *proxy_pass http://upstream_name*
- `params`: list of param (hash, zone...)
- `servers`: each upstream MUST have at least 1 server
#### Server params
You must set a `path`. For example: *192.168.0.50:8080* or *unix:/tmp/my.sock*.
All this params are optional. You should see [Nginx upstream doc](http://nginx.org/en/docs/http/ngx_http_upstream_module.html).
- `weight`
- `max_fails`
- `fail_timeout`
- `backup`
- `down`
- `route`
- `slow`start`
Dependencies
------------
@@ -82,9 +130,7 @@ None
Example Playbook
----------------
- hosts: servers
roles:
- { role: HanXHX.nginx }
See [tests/test.yml](tests/test.yml).
License
-------
@@ -94,6 +140,5 @@ GPLv2
Author Information
------------------
- You can find many other roles in my GitHub "lab": https://github.com/HanXHX/my-ansible-playbooks
- All issues, pull-request are welcome :)
- Twitter: [@hanxhx_](https://twitter.com/hanxhx_)

3
Vagrantfile vendored
View File

@@ -7,7 +7,8 @@ Vagrant.configure("2") do |config|
vms = [
[ "debian-wheezy", "deb/wheezy-amd64" , "192.168.33.27" ],
[ "debian-jessie", "deb/jessie-amd64", "192.168.33.28" ]
[ "debian-jessie", "deb/jessie-amd64", "192.168.33.28" ],
[ "debian-stretch", "sharlak/debian_stretch_64", "192.168.33.29" ]
]
config.vm.provider "virtualbox" do |v|

View File

@@ -8,8 +8,9 @@ nginx_apt_package: nginx-full
nginx_root: "/srv/www"
nginx_log_dir: '/var/log/nginx'
nginx_ssl_dir: '/etc/nginx/ssl'
nginx_pid: '/run/nginx.pid'
nginx_resolver:
hosts: ['208.67.222.222', '208.67.220.220'] # OpenDNS
hosts: ['8.8.8.8', '8.8.4.4']
valid: '300'
timeout: '5'
nginx_error_log_level: 'warn' # http://nginx.org/en/docs/ngx_core_module.html#error_log
@@ -24,6 +25,7 @@ nginx_dh_length: 2048
nginx_php: false
nginx_php_sockets:
- unix_socket: "/var/run/php5-fpm.sock"
nginx_upstreams: []
#
# Nginx configuration
@@ -69,4 +71,7 @@ nginx_http:
gzip_disable: '"msie6"'
# etag: 'off'
#
# Vhosts
#
nginx_vhosts: []

View File

@@ -1,6 +1,4 @@
---
- name: restart nginx
action: service name=nginx state=restarted enabled=yes
- name: reload nginx
action: service name=nginx state=reloaded enabled=yes

View File

@@ -4,7 +4,7 @@ galaxy_info:
description: Nginx for Debian
company:
license: GPLv2
min_ansible_version: 1.2
min_ansible_version: 1.6
platforms:
- name: Debian
versions:

View File

@@ -9,8 +9,10 @@
changed_when: false
- name: TEMPLATE | Deploy nginx.conf
template: src=etc/nginx/nginx.conf.j2 dest=/etc/nginx/nginx.conf validate= "nginx -t"
notify: restart nginx
template: >
src=etc/nginx/nginx.conf.j2
dest=/etc/nginx/nginx.conf
notify: reload nginx
- name: FILE | Create /etc/nginx/helpers
file: dest=/etc/nginx/helpers owner=root mode=0755 state=directory
@@ -24,7 +26,9 @@
# creates: "{{ nginx_dh_path }}"
- name: TEMPLATE | Deploy all helpers
template: src={{ item }} dest=/etc/nginx/helpers/{{ item | basename | regex_replace('\.j2$','') }}
template: >
src={{ item }}
dest=/etc/nginx/helpers/{{ item | basename | regex_replace('\.j2$','') }}
with_fileglob: '../templates/etc/nginx/helpers/*.j2'
notify: reload nginx
@@ -35,6 +39,3 @@
- name: INCLUDE | Vhosts configuration
include: vhost.yml
# TODO:
# - Python
# - Ruby (SHIT!)

View File

@@ -2,5 +2,10 @@
- name: TEMPLATE | Deploy PHP upstream to Nginx
template: src=etc/nginx/upstream/php.conf.j2 dest=/etc/nginx/conf.d/php.conf
when: nginx_php
notify: reload nginx
- name: TEMPLATE | Deploy other upstreams
template: src=etc/nginx/upstream/upstream.conf.j2 dest=/etc/nginx/conf.d/upstream-{{ item.name }}.conf
with_items: nginx_upstreams
notify: reload nginx

View File

@@ -1,19 +1,42 @@
---
- name: FILE | Create root directory
file: >
path={{ nginx_root }}
state=directory
- name: FILE | Create root folders (foreach nginx_vhosts)
file: >
path={{ nginx_root }}/{{ item.name[0] }}/public
path={{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}
state=directory
owner={{ item.owner | default('www-data') }}
group={{ item.group | default('www-data') }}
mode={{ item.mode | default('0755') }}
with_items: nginx_vhosts
when: item.root is not defined
when: >
item.root is not defined and
(item.template is defined and item.template != '_proxy') and
(item.delete is not defined or not item.delete) and
item.redirect_to is not defined
- name: FILE | Create root public folders (foreach nginx_vhosts)
file: >
path={{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}/public
state=directory
owner={{ item.owner | default('www-data') }}
group={{ item.group | default('www-data') }}
mode={{ item.mode | default('0755') }}
with_items: nginx_vhosts
when: >
item.root is not defined and
(item.template is defined and item.template != '_proxy') and
(item.delete is not defined or not item.delete) and
item.redirect_to is not defined
- name: TEMPLATE | Create vhosts
template: >
src=etc/nginx/sites-available/{{ item.template }}.j2
dest=/etc/nginx/sites-available/{{ item.name[0] }}
src=etc/nginx/sites-available/{{ item.template if item.redirect_to is not defined else '_redirect' }}.j2
dest=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }}
with_items: nginx_vhosts
notify: reload nginx
when: item.delete is not defined or not item.delete
@@ -23,16 +46,15 @@
# with_fileglob: "web/*"
- name: FILE | Delete vhosts
file: dest=/etc/nginx/sites-enabled/{{ item.name[0] }} state=absent
file: dest=/etc/nginx/sites-available/{{ item.name[0] }} state=absent
file: path=/etc/nginx/sites-available/{{ item.name if item.name is string else item.name[0] }} state=absent
with_items: nginx_vhosts
notify: reload nginx
when: item.delete is defined and item.delete
- name: FILE | Enable vhosts
file: >
src=/etc/nginx/sites-available/{{ item.name[0] }}
dest=/etc/nginx/sites-enabled/{{ 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 if item.name is string else item.name[0] }}
state=link
with_items: nginx_vhosts
notify: reload nginx
@@ -42,10 +64,10 @@
(item.delete is not defined or not item.delete)
- name: FILE | Disable vhosts
file: dest=/etc/nginx/sites-enabled/{{ item.name[0] }} state=absent
file: path=/etc/nginx/sites-enabled/{{ item.name if item.name is string else item.name[0] }} state=absent
with_items: nginx_vhosts
notify: reload nginx
when: item.enable is defined and not item.enable
when: (item.enable is defined and not item.enable) or (item.delete is defined and item.delete)
#- name: FILE | Create ssl dir per vhost (if needed)
# file: dest=/etc/nginx/ssl/{{ item.name }} owner=root mode=0750 state=directory

View File

@@ -4,7 +4,7 @@
user {{ nginx_user }};
worker_processes {{ nginx_worker_processes }};
pid /run/nginx.pid;
pid {{ nginx_pid }};
events {
{% for key, value in nginx_events.iteritems() %}

View File

@@ -12,12 +12,14 @@ server {
{% for port in __listen %}
listen {{ port }};
{% endfor %}
server_name {{ item.name | join(' ') }};
server_name {% if item.name is string %}{{ item.name }}{% else %}{{ item.name | join(' ') }}{% endif %};
{% block root %}
{% if item.root is defined %}
root {{ item.root }};
{% else %}
root {{ nginx_root }}/{{ item.name[0] }}/public;
root {{ nginx_root }}/{{ item.name if item.name is string else item.name[0] }}/public;
{% endif %}
{% endblock %}
{% block template_index %}
index {{ item.index | default('index.html index.htm') }};
{% endblock %}
@@ -28,17 +30,21 @@ server {
{% endfor %}
{% endif %}
{% if not __location.has_key('/') %}
location / {
{% block template_try_files %}
try_files $uri $uri/ =404;
try_files {{ override_try_files | default('$uri $uri/ =404') }};
{% endblock %}
}
{% endif %}
{% block template_upstream_location %}
{% endblock %}
{% block template_custom_location %}
{% endblock %}
{% block template_local_content %}
{% if item.manage_local_content is not defined or item.manage_local_content %}
location ~ /\.ht {
deny all;
}
@@ -53,6 +59,8 @@ server {
expires 30d;
log_not_found off;
}
{% endif %}
{% endblock %}
{% if __location is iterable and __location | length > 0 %}
# --> Custom locations

View File

@@ -4,7 +4,7 @@
{% endblock %}
{% block template_try_files %}
try_files $uri $uri/ /index.php;
try_files {{ override_try_files | default('$uri $uri/ /index.php') }};
{% endblock %}
{% block template_upstream_location %}

View 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 %}

View File

@@ -0,0 +1,23 @@
{% extends "_base.j2" %}
{% block template_try_files %}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://{{ item.upstream_name }};
proxy_read_timeout 90;
{#proxy_redirect http://{{ upstream.name }} https://jenkins.domain.tld;#}
{% if item.proxy_params is defined and item.proxy_params is iterable %}
{% for param in item.proxy_params %}
{{ param }}
{% endfor %}
{% endif %}
{% endblock %}
{# We flush the default local content (locations) #}
{% block template_local_content %}
{% endblock %}

View File

@@ -0,0 +1,14 @@
{% extends "_base.j2" %}
{% block root %}
{% endblock %}
{% block template_index %}
{% endblock %}
{% block template_try_files %}
return {{ item.redirect_to_code | default('302') }} {{ item.redirect_to }}$request_uri;
{% endblock %}
{% block template_local_content %}
{% endblock %}

View File

@@ -0,0 +1,19 @@
{%- macro s(key, value, is_bool, min_version) %}
{% if nginx_version.stdout | version_compare(min_version, 'ge') %}
{% if is_bool and value %} {{ key }}{% elif not is_bool %} {{ key }}={{ value }}{% endif %}
{% endif %}
{%- endmacro -%}
#
# {{ ansible_managed }}
#
upstream {{ item.name }} {
{% for server in item.servers %}
server {{ server.path }}{% for p in nginx_upstream_server_params if server[p.key] is defined %}{{ s(p.key, server[p.key] | default(p.default), p.is_bool | default(false), p.min_version | default('0.0.1')) }}{% endfor %};
{% endfor %}
{% if item.params is defined and item.params is iterable %}
{% for param in item.params %}
{{ param }};
{% endfor %}
{% endif %}
}

View File

@@ -2,5 +2,3 @@ FROM williamyeh/ansible:debian8-onbuild
RUN apt-get update
CMD ["sh", "tests/test.sh"]
EXPOSE 6379

View File

@@ -2,5 +2,3 @@ FROM williamyeh/ansible:debian7-onbuild
RUN apt-get update
CMD ["sh", "tests/test.sh"]
EXPOSE 6379

View File

@@ -8,12 +8,21 @@
- curl
vars:
nginx_php: true
nginx_upstreams:
- name: 'test'
servers:
- path: '127.0.0.1:80'
max_conns: 150
weight: 10
down: false
nginx_vhosts:
- name:
- 'test.local'
- 'test-alias.local'
- 'test2-alias.local'
template: '_base'
override_try_files: '$uri $uri index.htm index.html'
manage_local_content: false
more:
- 'autoindex off;'
location:
@@ -21,31 +30,51 @@
- 'return 403;'
'/gunther':
- 'return 404;'
- name:
- 'test-php.local'
- name: 'test-location.local'
template: '_base'
location:
'/':
- 'alias /var/tmp;'
- name: 'test-php.local'
upstream_params:
- 'fastcgi_param FOO bar;'
redirect_from:
- 'www.test-php.local'
template: '_php'
- name:
- 'deleted.local'
template: '_base'
- name: 'test-php-index.local'
template: '_php_index'
- name: 'test-proxy.local'
listen:
- 8080
template: '_proxy'
upstream_name: 'test'
- name: 'deleted.local'
delete: true
- name: 'redirect-to.local'
redirect_to: 'http://test.local'
roles:
- ../../
post_tasks:
- 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 --
copy: dest="{{ nginx_root }}/test.local/public/index.html" content="Index HTML test OK\n"
copy: dest="{{ item }}/index.html" content="Index HTML test OK\n"
with_items: ['{{ nginx_root }}/test.local/public', '/var/tmp']
- name: -- VERIFY VHOSTS --
shell: "curl -H 'Host: {{ item.name[0] }}' http://127.0.0.1/"
command: "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
when: item.delete is undefined or not item.delete
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 --
shell: "curl -H 'Host: {{ item.redirect_from[0] }}' http://127.0.0.1/"
command: "curl -H 'Host: {{ item.redirect_from[0] }}' http://127.0.0.1/"
with_items: nginx_vhosts
when: item.redirect_from is defined and (item.delete is undefined or not item.delete)
changed_when: false
register: r
failed_when: r.stdout.find('301 Moved Permanently') == -1

View File

@@ -1 +1,25 @@
nginx_dh_path: /etc/nginx/ssl/dhparams.pem
nginx_upstream_server_params:
- key: 'weight'
default: 1
- key: 'max_fails'
default: 1
- key: 'fail_timeout'
default: '10s'
- key: 'backup'
is_bool: true
- key: 'down'
is_bool: true
default: false
- key: 'route'
default: 'configuration_error'
- key: 'slow_start'
default: 0
# Nginx Plus only
# - key: 'max_conns'
# default: 0
# min_version: '1.5.9'
# - key: 'resolve'
# is_bool: true
# min_version: '1.5.12'