sorrowless.nginx
sbog/nginx
Ta rola instaluje i konfiguruje serwer www nginx. Użytkownik może określić dowolne parametry konfiguracyjne HTTP, które chce zastosować na swojej stronie. Można dodać dowolną liczbę stron z konfiguracjami wedle wyboru.
Wymagania
Ta rola wymaga Ansible 2.4 lub nowszego, a wymagania dotyczące platformy są wymienione w pliku metadanych. (Niektóre starsze wersje roli obsługują Ansible 1.4) Dla FreeBSD wymagana jest działająca konfiguracja pkgng (zobacz: https://www.freebsd.org/doc/handbook/pkgng-intro.html) Instalacja agenta Nginx Amplify jest wspierana tylko na dystrybucjach CentOS, RedHat, Amazon, Debian i Ubuntu.
Instalacja
ansible-galaxy install sorrowless.nginx
Zmienne roli
Zmiennymi, które można przekazać do tej roli i ich krótki opis znajdują się poniżej. (Wszystkie zmienne można znaleźć w defaults/main.yml)
# Użytkownik, który uruchamia nginx
nginx_user: "www-data"
# Lista dyrektyw dla sekcji wydarzeń.
nginx_events_params:
- worker_connections 512
- debug_connection 127.0.0.1
- use epoll
- multi_accept on
# Lista hashy definiujących serwery dla nginx,
# wraz z parametrami http. Można zdefiniować tutaj dowolne prawidłowe parametry serwera.
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;
}
# Lista hashy definiujących dodatkową konfigurację
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
# Lista hashy definiujących fragmenty konfiguracji
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
# Lista hashy definiujących pliki z użytkownikiem/hasłem
nginx_auth_basic_files:
demo:
- foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo , wygenerowane: htpasswd -nb foo demo
- bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo , wygenerowane: htpasswd -nb bar demo
# Włącz Real IP dla żądań CloudFlare
nginx_set_real_ip_from_cloudflare: True
# Włącz Nginx Amplify
nginx_amplify: true
nginx_amplify_api_key: "tutaj_wstaw_klucz_api"
nginx_amplify_update_agent: true
# Zdefiniuj moduły do włączenia w konfiguracji
#
# Nginx zainstalowany przez EPEL i APT automatycznie zainstaluje niektóre moduły.
# Przy użyciu oficjalnego repozytorium Nginx należy ręcznie zainstalować pakiety modułów.
#
# Używając EPEL i APT, określ tę sekcję jako listę nazw plików konfiguracyjnych
# bez rozszerzenia .conf.
# Używając oficjalnego repozytorium Nginx, określ tę sekcję jako listę nazw plików modułów
# bez rozszerzenia .so.
#
# Dostępne pliki konfiguracyjne modułów w repozytoriach EPEL i APT:
# (APT ma rzeczywiście kilka więcej, zobacz https://wiki.debian.org/Nginx/)
# - mod-http-geoip
# - mod-http-image-filter
# - mod-http-perl
# - mod-http-xslt-filter
# - mod-mail
# - mod-stream
#
# Dostępne nazwy plików modułów w oficjalnym repozytorium NGINX:
# - ngx_http_geoip_module
# - ngx_http_image_filter_module
# - ngx_http_perl_module
# - ngx_http_xslt_filter_module
# - ngx_http_js_module
#
# Niestandardowo skompilowane moduły są również dozwolone, jeśli plik .so istnieje w tym samym miejscu, co pakiet modułu:
# - ngx_http_modsecurity_module
#
nginx_module_configs:
- mod-http-geoip
Przykłady
1) Zainstaluj nginx z wybranymi dyrektywami HTTP, ale bez skonfigurowanych stron i dodatkowej konfiguracji:
- hosts: all
roles:
- {role: nginx,
nginx_http_params: ["sendfile on", "access_log /var/log/nginx/access.log"]
}
2) Zainstaluj nginx z innymi dyrektywami HTTP niż w poprzednim przykładzie, ale bez
stron skonfigurowanych i bez dodatkowej konfiguracji.
- hosts: all
roles:
- {role: nginx,
nginx_http_params: ["tcp_nodelay on", "error_log /var/log/nginx/error.log"]}
Uwaga: Upewnij się, że przekazane dyrektywy HTTP są ważne, ponieważ ta rola nie sprawdzi ich poprawności. Zobacz dokumentację nginx w celu uzyskania szczegółów.
3) Zainstaluj nginx i dodaj stronę do konfiguracji.
- 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) Zainstaluj nginx i dodaj dodatkowe zmienne do domyślnej konfiguracji
- hosts: all
vars:
- my_extra_params:
- client_max_body_size 200M
# zachowaj domyślne i dodaj dodatkowy parametr `client_max_body_size`
roles:
- role: sorrowless.nginx
nginx_http_params: "{{ nginx_http_default_params + my_extra_params }}"
Uwaga: Każda dodana strona jest reprezentowana jako lista hashy, a wygenerowane konfiguracje są umieszczane w /etc/nginx/site-available/ i linkowane z /etc/nginx/site-enable/ do /etc/nginx/site-available.
Nazwa pliku dla konkretnej konfiguracji strony jest określona w hash z kluczem "file_name", dowolne prawidłowe dyrektywy serwera można dodać do hasha. Dodatkowe konfiguracje są tworzone w /etc/nginx/conf.d/
5) Zainstaluj Nginx, dodaj 2 strony (inną metodą) i dodaj dodatkową konfigurację
---
- 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) Zainstaluj Nginx, dodaj 2 strony, dodaj dodatkową konfigurację i blok konfiguracji upstream
---
- 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:
# Oczekiwany wynik:
# upstream foo_backend {
# server 127.0.0.1:8080 weight=10;
# }
- upstream foo_backend { server 127.0.0.1:8080 weight=10; }
nginx_auth_basic_files:
demo:
- foo:$apr1$mEJqnFmy$zioG2q1iDWvRxbHuNepIh0 # foo:demo , wygenerowane: htpasswd -nb foo demo
- bar:$apr1$H2GihkSo$PwBeV8cVWFFQlnAJtvVCQ. # bar:demo , wygenerowane: htpasswd -nb bar demo
7) Zainstaluj Nginx, dodaj stronę i użyj specjalnej składni yaml, aby uczynić bloki lokalizacji wielowierszowymi dla przejrzystości
---
- 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) Przykład użycia tej roli z moją rolą ssl-certs do generowania lub kopiowania certyfikatu 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) Konfiguracja strony za pomocą niestandardowego szablonu.
Zamiast definiować plik konfiguracyjny strony za pomocą listy atrybutów,
możesz użyć hasha/słownika, który zawiera nazwę pliku innego szablonu.
Dodatkowe wartości są dostępne w szablonie za pośrednictwem zmiennej item.value
.
- hosts: all
roles:
- role: nginx
nginx_sites:
custom_bar:
template: custom_bar.conf.j2
server_name: custom_bar.example.com
Niestandardowy szablon: 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;
}
}
Użycie niestandardowego szablonu pozwala na nieograniczoną elastyczność w konfigurowaniu pliku konfiguracyjnego strony. Ten przykład ilustruje powszechną praktykę konfigurowania bloku serwera strony w tym samym pliku, co jego powiązany blok upstream. Jeżeli używasz tej opcji:
- Hash musi zawierać wartość
template:
, w przeciwnym razie zadanie konfiguracyjne zakończy się niepowodzeniem. - Ta rola nie może sprawdzić poprawności twojego niestandardowego szablonu. Jeśli używasz tej metody, formatowanie pliku conf zapewnione przez tę rolę jest niedostępne i to do Ciebie należy zapewnienie szablonu z prawidłową zawartością i formatowaniem dla NGINX.
10) Zainstaluj Nginx, dodaj 2 strony, użyj fragmentów do konfigurowania kontroli dostępu
---
- 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; }
Zależności
Brak
Licencja
BSD
Informacje o autorze
- Oryginalny: Benno Joy
- Zmodyfikowany przez: DAUPHANT Julien
- Przerobiony przez: Stan Bogatkin
ansible-galaxy install sorrowless.nginx