### Nginx Virtual Host Configuration Example (YAML) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Defines an Nginx virtual host with SSL, HTTP/2, and custom PHP-FPM fastcgi parameters. This example demonstrates how to configure server name, root directory, index files, error pages, logging, and additional server parameters using YAML syntax within Ansible. ```yaml nginx_vhosts: - 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)(/.+); fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; 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; ``` -------------------------------- ### Nginx Redirect Virtual Host Configuration Example (YAML) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Configures an Nginx virtual host to redirect traffic from one domain to another using a 301 permanent redirect. This example shows how to specify multiple server names and define a return directive for redirection, along with a custom filename to avoid conflicts. ```yaml nginx_vhosts: - listen: "80" server_name: "example.com www.example.com" return: "301 https://example.com$request_uri" filename: "example.com.80.conf" ``` -------------------------------- ### Install and Basic Nginx Configuration with Ansible Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Installs the geerlingguy.nginx Ansible role from Ansible Galaxy and includes it in a playbook to set up Nginx with default configurations. This is the most basic usage of the role. ```yaml # Install the role # ansible-galaxy install geerlingguy.nginx # playbook.yml - Basic usage - hosts: webservers roles: - role: geerlingguy.nginx ``` -------------------------------- ### Configure Virtual Hosts (vhosts) for Nginx with Ansible Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Demonstrates how to configure virtual hosts (server blocks) for Nginx using the `nginx_vhosts` variable. This example shows HTTPS configuration with PHP-FPM, HTTP to HTTPS redirection, and a static site configuration. The `nginx_remove_default_vhost` variable is used to remove the default Nginx welcome page. ```yaml # playbook.yml - Complete vhost configuration - hosts: webservers vars: nginx_remove_default_vhost: true nginx_vhosts: # HTTPS virtual host with PHP-FPM - 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" filename: "example.com.conf" access_log: "/var/log/nginx/example.com.access.log" error_log: "/var/log/nginx/example.com.error.log" extra_parameters: | location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+); fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ssl_certificate /etc/ssl/certs/example.com.pem; ssl_certificate_key /etc/ssl/private/example.com.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; # HTTP to HTTPS redirect - listen: "80" server_name: "example.com www.example.com" return: "301 https://example.com$request_uri" filename: "example.com.80.conf" # Static site vhost - listen: "80" server_name: "static.example.com" root: "/var/www/static" index: "index.html" state: "present" roles: - role: geerlingguy.nginx ``` -------------------------------- ### Manage Nginx Service State (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Controls the Nginx service state, allowing it to be started, stopped, or enabled/disabled. Useful for containerized environments or custom deployment workflows. ```yaml # playbook.yml - Container deployment (don't start service) - hosts: containers vars: nginx_service_state: stopped nginx_service_enabled: false roles: - role: geerlingguy.nginx --- # playbook.yml - Standard deployment with service running - hosts: webservers vars: nginx_service_state: started nginx_service_enabled: true roles: - role: geerlingguy.nginx ``` -------------------------------- ### Configure Platform-Specific Nginx Repositories (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Manages Nginx installation sources based on the operating system. Includes options for Ubuntu PPAs, RedHat/CentOS repositories, and SUSE zypper. ```yaml # playbook.yml - Ubuntu with official Nginx PPA - hosts: ubuntu_servers vars: nginx_ppa_use: true nginx_ppa_version: stable # or 'development' roles: - role: geerlingguy.nginx --- # playbook.yml - RedHat/CentOS with OS packages (no Nginx repo) - hosts: redhat_servers vars: nginx_yum_repo_enabled: false roles: - role: geerlingguy.nginx --- # playbook.yml - Debian with backports - hosts: debian_servers vars: nginx_default_release: "buster-backports" roles: - role: geerlingguy.nginx --- # playbook.yml - SUSE with OS packages - hosts: suse_servers vars: nginx_zypper_repo_enabled: false roles: - role: geerlingguy.nginx ``` -------------------------------- ### Ansible Playbook for Production Nginx Configuration Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt This playbook configures a production-ready Nginx server using the geerlingguy.nginx Ansible role. It defines performance tuning, caching, upstream servers, and virtual hosts with SSL and HTTP to HTTPS redirection. This example is suitable for automating complex web server setups. ```yaml # playbook.yml - Production web server - hosts: production_webservers vars: # Performance nginx_worker_processes: "auto" nginx_worker_connections: "4096" nginx_multi_accept: "on" nginx_keepalive_timeout: "30" nginx_keepalive_requests: "1000" nginx_client_max_body_size: "64m" nginx_server_tokens: "off" # Caching nginx_proxy_cache_path: "/var/cache/nginx keys_zone=app_cache:10m levels=1:2 inactive=60m max_size=1g" # Extra HTTP options nginx_extra_http_options: | 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_set_header Host $http_host; nginx_extra_conf_options: | worker_rlimit_nofile 65535; # Upstreams nginx_upstreams: - name: app_servers strategy: "least_conn" keepalive: 32 servers: - "127.0.0.1:3000" - "127.0.0.1:3001" # Virtual hosts nginx_remove_default_vhost: true nginx_vhosts: # HTTPS main site - listen: "443 ssl http2" server_name: "www.example.com example.com" root: "/var/www/example.com/public" filename: "example.com.conf" extra_parameters: | ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256; ssl_prefer_server_ciphers off; ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d; location /api/ { proxy_pass http://app_servers; proxy_cache app_cache; proxy_cache_valid 200 10m; } location / { try_files $uri $uri/ /index.html; } # HTTP to HTTPS redirect - listen: "80" server_name: "www.example.com example.com" return: "301 https://$host$request_uri" filename: "example.com.80.conf" roles: - role: geerlingguy.nginx ``` -------------------------------- ### Configure Nginx Load Balancers with Ansible Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Configures upstream server groups for load balancing using the `nginx_upstreams` variable. This example defines two upstream groups, 'myapp' and 'api_backend', with different strategies and server configurations, and then sets up a virtual host to proxy requests to the 'myapp' upstream. ```yaml # playbook.yml - Load balancer configuration - hosts: loadbalancers vars: nginx_upstreams: - name: myapp strategy: "ip_hash" keepalive: 16 servers: - "app1.example.com:8080" - "app2.example.com:8080 weight=2" - "app3.example.com:8080 backup" - name: api_backend strategy: "least_conn" servers: - "api1.example.com:3000" - "api2.example.com:3000" - "api3.example.com:3000 max_fails=3 fail_timeout=30s" nginx_vhosts: - listen: "80" server_name: "app.example.com" extra_parameters: | location / { proxy_pass http://myapp; 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; } roles: - role: geerlingguy.nginx ``` -------------------------------- ### Configure Nginx Logging and Package Repositories Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Settings for defining Nginx log formats and managing package repository sources across different Linux distributions. ```yaml nginx_log_format: |- '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' nginx_ppa_use: false nginx_yum_repo_enabled: true nginx_zypper_repo_enabled: true ``` -------------------------------- ### Configure Nginx HTTP and Global Options Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Variables for defining custom Nginx configuration blocks, including proxy caching, extra HTTP options, and global configuration directives. ```yaml nginx_client_max_body_size: "64m" nginx_server_names_hash_bucket_size: "64" nginx_proxy_cache_path: "" nginx_extra_http_options: | proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; nginx_extra_conf_options: | worker_rlimit_nofile 8192; ``` -------------------------------- ### Configure Nginx Gzip Settings with Ansible Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md This snippet demonstrates how to enable and configure gzip compression for Nginx. It involves setting a custom Nginx configuration template path in your Ansible playbook and then defining the gzip directives within that template, extending the base role's template. This allows for fine-grained control over compression levels, types, and other gzip-related settings. ```yaml nginx_conf_template: "{{ playbook_dir }}/templates/nginx.conf.j2" ``` ```jinja2 {% extends 'roles/geerlingguy.nginx/templates/nginx.conf.j2' %} {% block http_gzip %} gzip on; gzip_proxied any; gzip_static on; gzip_http_version 1.0; gzip_disable "MSIE [1-6]\."; gzip_vary on; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/xml+rss application/xhtml+xml application/x-font-ttf application/x-font-opentype image/svg+xml image/x-icon; gzip_buffers 16 8k; gzip_min_length 512; {% endblock %} ``` -------------------------------- ### Nginx Log Configuration (Ansible) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Sets up Nginx error and access logs with specified formats and buffering. This configuration allows customization of log file paths, log levels, and buffer sizes for better log management and analysis. ```yaml nginx_error_log: "/var/log/nginx/error.log warn" nginx_access_log: "/var/log/nginx/access.log main buffer=16k flush=2m" ``` -------------------------------- ### Nginx TCP Connection Options (Ansible) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Configures TCP connection optimizations for Nginx, including sendfile, tcp_nopush, and tcp_nodelay. These settings aim to improve network performance and reduce latency for Nginx traffic. ```yaml nginx_sendfile: "on" nginx_tcp_nopush: "on" nginx_tcp_nodelay: "on" ``` -------------------------------- ### Configure Per-Vhost Nginx Templates (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Applies custom Jinja2 templates to individual virtual hosts for complex configurations. This allows for distinct settings for each website hosted on the server. ```yaml # playbook.yml - Per-vhost templates - hosts: webservers vars: nginx_vhosts: - listen: "80 default_server" server_name: "site1.example.com" root: "/var/www/site1" template: "{{ playbook_dir }}/templates/site1.vhost.j2" - listen: "80" server_name: "site2.example.com" root: "/var/www/site2" template: "{{ playbook_dir }}/templates/site2.vhost.j2" roles: - role: geerlingguy.nginx ``` -------------------------------- ### Nginx Server Tokens Configuration (Ansible) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Controls the display of Nginx version information in HTTP response headers. Setting `nginx_server_tokens` to 'on' includes the version, while 'off' hides it for security purposes. ```yaml nginx_server_tokens: "on" ``` -------------------------------- ### Override Nginx Configuration Templates Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Methods for overriding default Nginx configuration templates or specifying custom virtual host templates for individual sites. ```yaml nginx_conf_template: "nginx.conf.j2" nginx_vhost_template: "vhost.j2" nginx_vhosts: - listen: "80 default_server" server_name: "site1.example.com" root: "/var/www/site1.example.com" template: "{{ playbook_dir }}/templates/site1.example.com.vhost.j2" ``` -------------------------------- ### Nginx Performance Tuning with Ansible Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Optimizes Nginx performance by configuring worker processes, connections, keepalive settings, and TCP optimizations. It also includes settings for upload size limits, hiding version information, and adjusting the server names hash bucket size. ```yaml # playbook.yml - Performance tuning - hosts: webservers vars: # Worker configuration (defaults to CPU cores) nginx_worker_processes: "auto" nginx_worker_connections: "4096" nginx_multi_accept: "on" # Keepalive settings nginx_keepalive_timeout: "65" nginx_keepalive_requests: "1000" # TCP optimization nginx_sendfile: "on" nginx_tcp_nopush: "on" nginx_tcp_nodelay: "on" # Upload size limit nginx_client_max_body_size: "128m" # Security - hide version info nginx_server_tokens: "off" # Server names hash (for many domains) nginx_server_names_hash_bucket_size: "128" roles: - role: geerlingguy.nginx ``` -------------------------------- ### Nginx Keepalive Settings (Ansible) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Configures Nginx keepalive timeout and maximum requests per connection. These settings control how long client connections are kept open and the number of requests allowed over a persistent connection, impacting performance for different types of websites. ```yaml nginx_keepalive_timeout: "65" nginx_keepalive_requests: "100" ``` -------------------------------- ### Configure Nginx Logging (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Configures Nginx access and error logs with custom formats and buffering. This allows for detailed logging and improved performance through log buffering. ```yaml # playbook.yml - Custom logging - hosts: webservers vars: nginx_error_log: "/var/log/nginx/error.log warn" nginx_access_log: "/var/log/nginx/access.log main buffer=32k flush=5m" nginx_log_format: -'\n $remote_addr - $remote_user [$time_local] \"$request\" ' \ '$status $body_bytes_sent \"$http_referer\" ' \ '\"$http_user_agent\" \"$http_x_forwarded_for\" ' \ 'rt=$request_time uct=\"upstream_connect_time\" ' \ 'uht=\"upstream_header_time\" urt=\"upstream_response_time\"' roles: - role: geerlingguy.nginx ``` -------------------------------- ### Configure Nginx Reverse Proxy and HTTP Options (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Sets custom HTTP block options for Nginx, such as proxy buffering, headers, and timeouts. This is useful for configuring reverse proxy behavior and other global HTTP settings. ```yaml # playbook.yml - Reverse proxy configuration - hosts: proxies vars: nginx_proxy_cache_path: "/var/cache/nginx keys_zone=cache:32m levels=1:2 inactive=60m max_size=1g" nginx_extra_http_options: | proxy_buffering off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; nginx_extra_conf_options: | worker_rlimit_nofile 65535; roles: - role: geerlingguy.nginx ``` -------------------------------- ### Nginx Worker Process Configuration (Ansible) Source: https://github.com/geerlingguy/ansible-role-nginx/blob/master/README.md Configures Nginx worker processes and connections using Ansible variables. It dynamically sets the number of worker processes based on available CPU cores and defines the maximum number of simultaneous connections per process. ```yaml nginx_worker_processes: "{{ ansible_processor_vcpus|default(ansible_processor_count) }}" nginx_worker_connections: "1024" nginx_multi_accept: "off" ``` -------------------------------- ### Override Nginx Configuration Template with Jinja2 (Ansible & Jinja2) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Overrides the default Nginx configuration template using Jinja2 inheritance. This allows for custom configurations like enabling gzip compression. ```yaml # playbook.yml - Custom template override - hosts: webservers vars: nginx_conf_template: "{{ playbook_dir }}/templates/nginx.conf.j2" roles: - role: geerlingguy.nginx ``` ```jinja2 # templates/nginx.conf.j2 - Custom gzip configuration {% extends 'roles/geerlingguy.nginx/templates/nginx.conf.j2' %} {% block http_gzip %} gzip on; gzip_proxied any; gzip_static on; gzip_http_version 1.0; gzip_disable "MSIE [1-6]\."; gzip_vary on; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml application/xhtml+xml image/svg+xml; gzip_buffers 16 8k; gzip_min_length 512; {% endblock %} ``` -------------------------------- ### Remove Nginx Virtual Hosts (Ansible) Source: https://context7.com/geerlingguy/ansible-role-nginx/llms.txt Removes virtual host configurations by setting the `state` to `absent` for specific vhost entries in the `nginx_vhosts` variable. ```yaml # playbook.yml - Remove a vhost - hosts: webservers vars: nginx_vhosts: - server_name: "old-site.example.com" state: "absent" - server_name: "active-site.example.com" root: "/var/www/active" state: "present" roles: - role: geerlingguy.nginx ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.