repleo.nginx
Ansible Role - Nginx Server
This role installs and sets up the Nginx web server. You can customize the HTTP settings for your site, and you can add as many sites as you like with your chosen configurations.
The role also supports Let's Encrypt SSL, making it easy to set up HTTPS on your web server.
Requirements
- Ansible version 1.4 or higher.
- Platform requirements are detailed in the metadata file.
Role Variables
Here are some variables you can use with this role, along with a brief description:
create_nginx_conf: If set to true, it will create a new
nginx.conf
. Normally, it only writes the file if it does not exist.nginx_max_clients: Maximum number of clients allowed. Default: 512.
nginx_http_params: A list of HTTP parameters for Nginx. You can add any valid Nginx HTTP parameters here.
nginx_sites: A list of site configurations for Nginx. You can define valid server parameters for each site.
Example Configuration for Sites:
nginx_sites:
- file_name: foo
listen: 8080
server_name: localhost
root: "/tmp/site1"
ssl:
supplier: "local"
local_keystore_dir: "{{ playbook_dir }}/files/"
key: "ssl.key"
certificate: "ssl_chain.pem"
locations:
- name: /
lines:
- "try_files: $uri $uri/ /index.html"
- name: /images/
lines:
- "try_files: $uri $uri/ /index.html"
lines:
- "return 301 https://$http_host$request_uri;"
Examples
- Install Nginx with HTTP Settings, No Sites:
- hosts: all
roles:
- { role: nginx, create_nginx_conf: true, nginx_http_params: { sendfile: "on", access_log: "/var/log/nginx/access.log" }, nginx_sites: [] }
- Install Nginx with Different HTTP Settings, No Sites:
- hosts: all
roles:
- { role: nginx, create_nginx_conf: true, nginx_http_params: { tcp_nodelay: "on", error_log: "/var/log/nginx/error.log" }, nginx_sites: [] }
- Install Nginx and Add a Site:
- hosts: all
roles:
- { role: nginx, create_nginx_conf: true, nginx_http_params: { tcp_nodelay: "on", error_log: "/var/log/nginx/error.log" }, nginx_sites: [ { file_name: bar, server_name: localhost, listen: 8080, locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] } ] } }
- Install Nginx and Add Two Sites:
- hosts: all
roles:
- { role: nginx, create_nginx_conf: true, nginx_http_params: { tcp_nodelay: "on", error_log: "/var/log/nginx/error.log" }, nginx_sites: [ { file_name: bar, server_name: localhost, listen: 8080, locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] }, { file_name: bar, server_name: ansible, listen: 9090, root: "/tmp/site2", locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] } ] } }
- Add Virtual Hosts to Existing Nginx Install:
- hosts: all
roles:
- { role: nginx, nginx_sites: [ { file_name: bar, server_name: localhost, listen: 8080, locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] }, { file_name: bar, server_name: ansible, listen: 9090, root: "/tmp/site2", locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] } ] } }
- HTTPS Server Configuration with Local Keys:
- hosts: all
roles:
- { role: nginx, nginx_sites: [ { file_name: bar, server_name: localhost, listen: 8080, ssl: { supplier: "local", local_keystore_dir: "{{ playbook_dir }}/files/", key: localhost.key, certificate: localhost_chain.pem }, locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] } ] } }
- HTTPS Server Configuration with Let's Encrypt:
- hosts: all
roles:
- { role: nginx, nginx_sites: [ { file_name: bar.ssl, server_name: "example.com www.example.com", listen: 443, ssl: { supplier: "letsencrypt", domains: [ "example.com", "www.example.com" ], generate_redirect: true }, locations: [ { name: /, lines: [ "try_files: $uri $uri/ /index.html" ] }, { name: /images/, lines: [ "try_files: $uri $uri/ /index.html" ] } ] } ] } }
Handlers
The Nginx role includes two handlers:
- Reload Nginx
- Restart Nginx
Reloading allows updates to your web server without downtime, while restarting ensures all changes take effect, but it may cause a brief downtime.
Dependencies
None
License
BSD
Author Information
Repleo, Amstelveen, Holland -- www.repleo.nl
Jeroen Arnoldus (jeroen@repleo.nl)
This role was originally created by Benno Joy.
Ansible role for installing nginx, including support for SSL, letsencrypt and virtual host deployments
ansible-galaxy install repleo.nginx