sorrowless.nginx

sbog/nginx

このロールは、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リクエストのためにReal 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) 任意のHTTP指示を使ってnginxをインストールするが、サイトは設定せず、追加の設定も行わない

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

2) 前の例とは異なるHTTP指示を使ってnginxをインストールするが、サイトは設定せず、追加の設定も行わない

- 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/ からリンクされます。

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            "Restricted";
             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証明書を生成またはコピーする例

 - 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ファイルのフォーマットは使用できず、有効なコンテンツとフォーマットを持つテンプレートを提供する責任はあなたにあります。

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