2020年8月9日 星期日

nginx 設定檔讀取與ansible安裝範例

在安裝好nginx 可以看到/etc/nginx下包含了
nginx.conf
conf.d/
sites-enabled/
sites-available/
幾個目錄
通常我們做異動修改的都會是sites-available/下
檢視一下幾個目錄之間的關係
nginx讀取的設定檔入口是nginx.conf檔案
可以看到包含了
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
也就是說預設會去讀讀目錄是在sites-enabled
而在sites-enabled目錄下看到只有一個default的檔案

實際運作的方式
可以sites-available/ 下建立default.conf
然後把default.conf 做symblelink到sites-enabled/default
就可以
以 ansible做nginx的設定與安裝範例如下

---
- name: Install host nginx
  hosts: all
  become: yes
  tasks:
    - name: ensure nginx is at the latest version
      apt: name=nginx state=latest
    - name: start nginx
      service:
        name: nginx
        state: started
    - name: copy the nginx main config
      copy:
        src: ../../files/nginx/default.conf
        dest: /etc/nginx/sites-available/default.conf
    - name: remove old nginx conf
      file:
        path: /etc/nginx/sites-available/conf
        state: absent
    - name: copy conf folder to sites-available
      copy:
        src: ../../files/nginx/conf
        dest: /etc/nginx/sites-available
    - name: create symlink
      file:
        src: /etc/nginx/sites-available/default.conf
        dest: /etc/nginx/sites-enabled/default
        state: link
    - name: restart nginx
      service:
        name: nginx
        state: restarted

沒有留言: