geerlingguy.nginx
Ansible Role: Nginx
Note: It's a good idea to use the official NGINX Ansible role from NGINX, Inc.
This role installs Nginx on various operating systems like RedHat/CentOS, Debian/Ubuntu, Archlinux, FreeBSD, or OpenBSD.
The role fetches and installs the latest Nginx version from the respective repositories (yum for RedHat, apt for Debian, pacman for Archlinux, pkgng for FreeBSD, and pkg_add for OpenBSD). After installation, you may need to set up additional configurations, such as creating your own virtual host .conf
file in /etc/nginx/conf.d/
, to manage your website settings.
Requirements
There are no specific requirements.
Role Variables
Here’s a list of available variables with their default values (see defaults/main.yml
):
nginx_listen_ipv6:
true
- Indicates whether to enable IPv6 for all virtual hosts managed by this role.
nginx_vhosts:
[]
- A list of virtual host configurations. Each entry creates a unique config file based on
server_name
. If it’s empty, you must provide your own configuration. For complex server setups, manage the virtual host file yourself.
- A list of virtual host configurations. Each entry creates a unique config file based on
Example of a fully defined nginx_vhosts
entry:
nginx_vhosts:
- listen: "443 ssl http2"
server_name: "example.com"
root: "/var/www/example.com"
nginx_remove_default_vhost:
false
- Whether to delete the default virtual host provided by Nginx.
nginx_upstreams:
[]
- If using Nginx as a load balancer, define upstream servers here.
nginx_user:
"nginx"
- User under which Nginx runs. Defaults vary by operating system.
nginx_worker_processes: Will set based on the number of CPU cores.
nginx_worker_connections:
"1024"
- Maximum connections per worker.
nginx_error_log and nginx_access_log: Default log settings.
nginx_sendfile, nginx_tcp_nopush, nginx_tcp_nodelay: TCP options for optimized data transfer.
nginx_keepalive_timeout:
"65"
- Adjust timeouts based on website traffic needs.
nginx_client_max_body_size:
"64m"
- Sets the limit for file uploads.
nginx_proxy_cache_path: Default is empty; set if using Nginx as a reverse proxy.
nginx_extra_http_options: Add additional configurations to the main
http
block.nginx_log_format: Customize how logs are formatted.
nginx_service_state:
started
- Ensures the Nginx service is running after configuration.
Overriding Configuration Templates
If you need to customize Nginx configurations that are not exposed as variables, you can override the templates used to create the virtual host or main configuration files.
nginx_conf_template: "nginx.conf.j2"
nginx_vhost_template: "vhost.j2"
Example Playbook
Here is a simple playbook to use this role:
- hosts: server
roles:
- { role: geerlingguy.nginx }
License
MIT / BSD
Author Information
This role was created by Jeff Geerling in 2014, author of Ansible for DevOps.
Nginx installation for Linux, FreeBSD and OpenBSD.
ansible-galaxy install geerlingguy.nginx