微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Puppet部署Nginx返代示例

一、创建目录并编辑Nginx安装模块

mkdir -pv /etc/puppet/modules/Nginx/{manifests,files,templates,spec,tests,lib}
]# vim Nginx/manifests/init.pp
class Nginx {
    package{'Nginx':
        ensure => latest
    } ->
    service{'Nginx':
        ensure => running,
        enable => true
    }
}

二、编辑Nginx配置文件

]# vim Nginx/manifests/web.pp 
class Nginx::web($port=8088)  inherits Nginx {
    file{'web.conf':
        path   => '/etc/Nginx/conf.d/web.conf',
        content => template('Nginx/web.conf.erb')
    }
    file{'/ngxdata/html':
        ensure  => directory
    }
    file{'index.html':
        ensure => file,
        path   => '/ngxdata/html/index.html',
        source => 'puppet:///modules/Nginx/index.html',
        require => File['/ngxdata/html']
    }
    Service['Nginx'] {
        subscribe  => File['web.conf']
    }
}

三、编辑反向代理配置

]# vim Nginx/manifests/proxy.pp
class Nginx::proxy($proxy_port=8088)  inherits Nginx {
    file{'proxy.conf':
        path   => '/etc/Nginx/conf.d/proxy.conf',
        content => template('Nginx/proxy.conf.erb'),
    }
    Service['Nginx'] {
        subscribe  => File['proxy.conf']
    }
}

四、配置模板与页面测试文件

]# vim Nginx/templates/web.conf.erb  
server {
    listen <%= @port %>;
    server_name <%= @fqdn %>;
    location /
        root /ngxdata/html;
    }
}
]# vim Nginx/files/index.html
<h1> Nginx ok </h1>
]# vim Nginx/templates/proxy.conf.erb
server {
    listen  <%= @proxy_port %>;
    server_name <%= @fqdn %>;
    location / {
        proxy_pass http://xxx.xxx.xxx:8080/;
    }
}

五、应用

]# puppet apply -v -e 'include Nginx::proxy'

查看模板是否成效:

]# cat /etc/Nginx/conf.d/proxy.conf 
server {
    listen  8088;
    server_name node1.danran.com;
    location / {
        proxy_pass http://xxx.xxx.xxx:8080/;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐