sorrowless.nginx

sbog/nginx

构建状态 Ansible 角色 Ansible 质量评分 Ansible 角色 GitHub

这个角色安装和配置 nginx 网络服务器。用户可以指定希望应用于其站点的任何 http 配置参数。可以根据自己的选择添加任意数量的站点及其配置。

要求

此角色需要 Ansible 2.4 或更高版本,平台要求在元数据文件中列出。(某些旧版本的角色支持 Ansible 1.4)对于 FreeBSD,需要有一个工作的 pkgng 设置(请参见: https://www.freebsd.org/doc/handbook/pkgng-intro.html)Nginx Amplify 代理的安装仅支持 CentOS、RedHat、Amazon、Debian 和 Ubuntu 发行版。

安装

ansible-galaxy install sorrowless.nginx

角色变量

可以传递给该角色的变量及其简要描述如下。(有关所有变量,请查看 defaults/main.yml

# 运行 nginx 的用户
nginx_user: "www-data"

# 事件部分的指令列表
nginx_events_params:
 - worker_connections 512
 - debug_connection 127.0.0.1
 - use epoll
 - multi_accept on

# 定义 nginx 服务器的哈希列表
nginx_sites:
 default:
     - listen 80
     - server_name _
     - root "/usr/share/nginx/html"
     - index index.html
 foo:
     - listen 8080
     - server_name localhost
     - root "/tmp/site1"
     - location / { try_files $uri $uri/ /index.html; }
     - location /images/ { try_files $uri $uri/ /index.html; }
 bar:
     - listen 9090
     - server_name ansible
     - root "/tmp/site2"
     - location / { try_files $uri $uri/ /index.html; }
     - location /images/ {
         try_files $uri $uri/ /index.html;
         allow 127.0.0.1;
         deny all;
       }

# 额外配置的哈希列表
nginx_configs:
  proxy:
      - proxy_set_header X-Real-IP  $remote_addr
      - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
  upstream:
      - upstream foo { server 127.0.0.1:8080 weight=10; }
  geo:
      - geo $local {
          default 0;
          127.0.0.1 1;
        }
  gzip:
      - gzip on
      - gzip_disable msie6

# 定义配置片段的哈希列表
nginx_snippets:
  error_pages:
    - error_page 500 /http_errors/500.html
    - error_page 502 /http_errors/502.html
    - error_page 503 /http_errors/503.html
    - error_page 504 /http_errors/504.html

# 定义用户/密码文件的哈希列表
nginx_auth_basic_files:
   demo:
     - foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo,由:htpasswd -nb foo demo生成
     - bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo,由:htpasswd -nb bar demo生成

# 为 CloudFlare 请求启用真实 IP
nginx_set_real_ip_from_cloudflare: True

# 启用 Nginx Amplify
nginx_amplify: true
nginx_amplify_api_key: "your_api_key_goes_here"
nginx_amplify_update_agent: true

# 定义要在配置中启用的模块
nginx_module_configs:
  - mod-http-geoip

示例

1) 安装 nginx,使用自选的 HTTP 指令,但不配置站点和额外配置:

- hosts: all
  roles:
  - {role: nginx,
     nginx_http_params: ["sendfile on", "access_log /var/log/nginx/access.log"]
                          }

2) 安装 nginx,使用与上一个示例不同的 HTTP 指令,但不配置站点和额外配置。

- hosts: all
  roles:
  - {role: nginx,
     nginx_http_params: ["tcp_nodelay on", "error_log /var/log/nginx/error.log"]}

注意:请确保传入的 HTTP 指令有效,因为此角色不会检查指令的有效性。有关详细信息,请参阅 nginx 文档。

3) 安装 nginx 并为配置添加站点。

- hosts: all

  roles:
  - role: nginx
    nginx_http_params:
      - sendfile "on"
      - access_log "/var/log/nginx/access.log"
    nginx_sites:
      bar:
        - listen 8080
        - location / { try_files $uri $uri/ /index.html; }
        - location /images/ { try_files $uri $uri/ /index.html; }
    nginx_configs:
      proxy:
        - proxy_set_header X-Real-IP  $remote_addr
        - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

4) 安装 nginx,并为默认配置添加额外变量

- hosts: all
  vars:
    - my_extra_params:
      - client_max_body_size 200M
  roles:
    - role: sorrowless.nginx
      nginx_http_params: "{{ nginx_http_default_params + my_extra_params }}"

注意:每添加的站点由哈希列表表示,生成的配置填充在 /etc/nginx/site-available/ 中,并通过链接从 /etc/nginx/site-enable/ 指向 /etc/nginx/site-available。

特定站点配置的文件名在哈希中用键 "file_name" 指定,可以将任何有效的服务器指令添加到哈希。额外的配置在 /etc/nginx/conf.d/ 中创建。

5) 安装 Nginx,添加 2 个站点(不同方法)并添加额外配置

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
         foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
         bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
      nginx_configs:
         proxy:
            - proxy_set_header X-Real-IP  $remote_addr
            - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

6) 安装 Nginx,添加 2 个站点,添加额外配置和上游配置块

---
- hosts: all
  roles:
    - role: nginx
      nginx_error_log_level: info
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
        foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
        bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - if ( $host = example.com ) { rewrite ^(.*)$ http://www.example.com$1 permanent; }
           - location / {
             try_files $uri $uri/ /index.html;
             auth_basic            "受限";
             auth_basic_user_file  auth_basic/demo;
           }
           - location /images/ { try_files $uri $uri/ /index.html; }
      nginx_configs:
        proxy:
            - proxy_set_header X-Real-IP  $remote_addr
            - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
        upstream:
            - upstream foo_backend { server 127.0.0.1:8080 weight=10; }
      nginx_auth_basic_files:
        demo:
           - foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo,由:htpasswd -nb foo demo生成
           - bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo,由:htpasswd -nb bar demo生成

7) 安装 Nginx,添加站点并使用特殊 yaml 语法使位置块以多行形式显示以提高清晰度

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_sites:
        foo:
           - listen 443 ssl
           - server_name foo.example.com
           - set $myhost foo.example.com
           - |
             location / {
               proxy_set_header Host foo.example.com;
             }
           - |
             location ~ /v2/users/.+?/organizations {
               if ($request_method = PUT) {
                 set $myhost bar.example.com;
               }
               if ($request_method = DELETE) {
                 set $myhost bar.example.com;
               }
               proxy_set_header Host $myhost;
             }

8) 示例,使用我的 ssl-certs 角色生成或复制 ssl 证书 (https://galaxy.ansible.com/sorrowless/ssl-certs)

 - hosts: all
   roles:
     - jdauphant.ssl-certs
     - role: sorrowless.nginx
       nginx_configs:
          ssl:
               - ssl_certificate_key {{ssl_certs_privkey_path}}
               - ssl_certificate     {{ssl_certs_cert_path}}
       nginx_sites:
          default:
               - listen 443 ssl
               - server_name _
               - root "/usr/share/nginx/html"
               - index index.html

9) 使用自定义模板进行站点配置。

可以使用哈希/字典来定义站点配置文件,包括替代模板的文件名。模板中的附加值可以通过 item.value 变量访问。

- hosts: all

  roles:
  - role: nginx
    nginx_sites:
      custom_bar:
        template: custom_bar.conf.j2
        server_name: custom_bar.example.com

自定义模板: custom_bar.conf.j2:

# {{ ansible_managed }}
upstream backend {
  server 10.0.0.101;
}
server {
  server_name {{ item.value.server_name }};
  location / {
    proxy_pass http://backend;
  }
}

使用自定义模板可以在配置站点配置文件时提供极大的灵活性。这个示例展示了在同一个文件中配置站点服务器块和其补充上游块的常见做法。 使用此选项时:

  • 哈希 必须 包含 template: 值,否则配置任务将失败。
  • 此角色无法检查自定义模板的有效性。如果使用此方法,格式由此角色提供的 conf 文件不可用,您需要提供格式有效的模板给 NGINX。

10) 安装 Nginx,添加 2 个站点,使用片段配置访问控制

---
- hosts: all
  roles:
    - role: nginx
      nginx_http_params:
        - sendfile on
        - access_log /var/log/nginx/access.log
      nginx_snippets:
        accesslist_devel:
          - allow 192.168.0.0/24
          - deny all
      nginx_sites:
        foo:
           - listen 8080
           - server_name localhost
           - root /tmp/site1
           - include snippets/accesslist_devel.conf
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }
        bar:
           - listen 9090
           - server_name ansible
           - root /tmp/site2
           - location / { try_files $uri $uri/ /index.html; }
           - location /images/ { try_files $uri $uri/ /index.html; }

依赖

许可证

BSD

作者信息

  • 原作者 : Benno Joy
  • 修改者 : DAUPHANT Julien
  • 重新工作的 : Stan Bogatkin
关于项目

Ansible role to install Nginx.

安装
ansible-galaxy install sorrowless.nginx
许可证
bsd-2-clause
下载
9.2k
拥有者
Barocco-style deployment engineer