Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Chef Nginx recipes
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
definitions Log Nginx Mainconfig modifications.
files/default Publishing Nginx Chef recipe v2.1.0
providers Mode should be quoted when setting permissions
test/cookbooks/nginx_test Mode should be quoted when setting permissions
metadata.rb Version bump: 2.2.4


Installs Nginx from package, sets up some default configuration and defines LWRP supposed to be used inside your own cookbooks, which you use to manage your infrastructure.


Sponsored by Evil Martians



At the moment this cookbook doesn't depend on other cookbooks but it will change in the future.


The cookbook has been tested to work on Debian 6.

I suppose it should also work for last versions of Ubuntu and CenOS/RHEL but no tests have been conducted yet. I have some certain plans for porting and testing this cookbook on the above mentioned platforms after I finish its main funcionality.

Chef version

Chef version >= 0.10.10 has to be used.


Defaults that are used to configure nginx. If you want to change one of this parameters in nginx consider using provided LWRP and definitions.

  • node['nginx']['directories']['conf_dir'] - Base nginx config directory. Default /etc/nginx.
  • node['nginx']['directories']['log_dir'] - Directory for nginx log files. Default /var/log/nginx.
  • node['nginx']['user'] - Default user that nginx will use to run worker processes. Default: www-data.
  • node['nginx']['worker_processes'] - Number of nginx workers. Default cpu['total'].
  • node['nginx']['worker_connections'] - Number of simultaneous connections that one worker can serve. Default8192`.
  • node['nginx']['worker_rlimit_nofile'] - Specifies the value for maximum file descriptors that can be opened by one worker process. Default 8192.


This cookbook provides only one recipe:


This default recipe will make some basic steps:

  • installs Nginx from the package that is provided by your OS's package manager ("pin" the desired version using another cookbook);
  • creates all directories for configuration, directory for log files, etc;
  • creates default nginx.conf file and associated files;
  • configures log rotation for Nginx;
  • enables and starts Nginx service;
  • runs resource that removes Nginx configuration files for sites, which are not defined by this cookbook's LWRP.


Wrapper-cookbook way

This cookbook has been designed to provide LWRP for your own infrastructure recipes. First of all, we should make our infrastructure cookbook to load this one.

  • Do it by adding the line depends nginx to your cookbook's metadata.rb.
  • To make all default preparations for using Nginx invoke include_recipe "nginx" inside your designated recipe.
  • Now feel free to use all available LWRP provided by this cookbook.

Roles-based way

Another way to use this cookbook is just to add recipe[nginx] to your run_list before your recipe, which is resonsible for your infrastructure.

I personally prefer the first way because if you stick to it you'll eventually get complete and explicit "documentation" for your specific server installation. But in any case, you'll get Nginx installed, nginx.conf configured from the default template we provide, LWRP defined and ready to use.



This resource manages your Nginx sites configuraions.


Action Description Default
create Creates site configuration file inside "sites-available" directory, but doesn't enable it.
enable Creates site configuration file inside "sites-available" directory, enables it (puts a symlink to it into "sites-enabled" directory) Yes
disable Ensures that site configuration file is disabled.
delete Disables and deletes site configuration file.


Attribute Description Default Value
name Name attribute: the name of the site's configuration file. nil
template Defines what erb template file from the cookbook that invokes this resource we should use. name.conf.erb
variables Variables to be used in the template.


# We want to use "" template file, do not want to pass any variables.
nginx_site ''

# Using custom-named template file and passing some variables, which can be used inside the template.
nginx_site '' do
  action :enable
  template 'forum-nginx.erb'
    :listen_ip => '',
    :remote_ips => [ '', '' ]

# Making sure that old site's configuration is disabled even if somebody has enabled it by hands.
nginx_site '' do
  action :disable


If you want to customize template for Nginx main configuration file, you can use this definition. It will apply passed attributes to the nginx.conf. You can only use a fixed number of core arguments that are listed below. All other Nginx arguments are meant to be set via site configurations(or conf.d directory). It can be invoked any number of times and overwrite previous changes from previous invocations so be cautious!

List of allowed attributes

There are also attributes that can accept either a string or an array of strings:


nginx_mainconfig do
  error_log '/var/log/nginx/new-error.log'
  pid '/var/run/nginx/'

  env [

  debug_connection [

# Another example for env and debug_connection
nginx_mainconfig do
  debug_connection ''

Small handy templates

As mentioned in the previous paragraph - main nginx config file acceps only limited set of options. But we always do some tuning like enabling compression, etc. I've created two small templates for a kind of configuration I usually use. It's a bit ugly and it's atemorary solution but may become usefull if you want some standart and clean confguration fast.

Invoke each of these templates only once otherwise you'll have invalid nginx config.


Defaults to:

gzip on;
gzip_http_version 1.0;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
gzip_disable msie6;
gzip_vary off;

How to use:

# Use its default parameters.
nginx_site '00-gzip' do
  cookbook 'nginx'
  template 'gzip.conf.erb'

# Or you can fine tune it.
nginx_site '01-gzip' do
  cookbook 'nginx'
  template 'gzip.conf.erb'
    enabled: true,
    http_version: '1.0',
    comp_version: 4,
    proxied: 'any',
    types: %w( text/plain text/css ),
    vary: 'off'


Defaults to:

sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
reset_timedout_connection off;

keepalive_timeout 65;

How to use:

# Use its default parameters.
nginx_site '02-some-handy-defaults' do
  cookbook 'nginx'
  template 'some-handy-defaults.conf.erb'

# Or you can fine tune it.
nginx_site '03-some-handy-defaults' do
  cookbook 'nginx'
  template 'some-handy-defaults.conf.erb'
    sendfile: 'on',
    tcp_nopush: 'on',
    tcp_nodelay: 'on',
    server_tokens: 'off',
    reset_timedout_connection: 'off',
    keepalive_timeout: 65


This definition controls default log rotation settings for Nginx. Definition is used here as a wrapper because the name attibute can be omitted this way. Log rotation is enabled by default and has following configuration:

/var/log/nginx/*.log {
    rotate 7
    create 640 root adm
      test -f /var/run/ && kill -USR1 "$(cat /var/run/"


Attribute Description Default Value
logs Log files to be rotated. Wildcards are allowed. /var/log/nginx/*.log
how_often Defines how often we should rotate logs. Allowed values are daily, weekly, monthly. daily
copytruncate If true truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. false
dateext If true archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number. The extension may be configured using the dateformat option. false
delaycompress If true postpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress. It can be used when some program cannot be told to close its logfile and thus might continue writing to the previous log file for some time. true
rotate Log files are rotated this number of times before being removed. 7
user Specifies the user name who will own the log file. root
group Specifies the group the log file will belong to. adm
mode Specifies the mode for the log file in octal. 640
pidfile Path to file that contains pid nuber of Nginx master process. If copytruncate is set to false we should send USR1 signal to that Nginx process to make it reopen log files after log rotation. /var/run/


# Store rotated logs for 30 days and make them readable by everybody
nginx_logrotate do
  rotate 30
  mode '644'

# Or full tuning
nginx_logrotate do
  logs ::File.join(node[:infrastucute_name][:logdir], '*.log')
  how_often 'weekly'
  rotate 8
  copytruncate true
  user 'root'
  group 'adm'
  mode '660'


This definition has no attributes or actions. Its invokation just disables the nginx_cleanup resource that is invoked by default.


The resource that is used by nginx_logrotate definition and default recipe of this cookbook and is not meant to be used inside your recipes.


It finds all enabled Nginx site's configuration symlinks (or files if somebody was was too lazy or inattentive :) ) that aren't created using nginx_site and deletes them. If you do not want this to happen use nginx_disable_cleanup definition described above.

This resource is invoked from default recipe of this cookbook and is made to be invoked only once.

License and Author

Kirill Kouznetsov (

Copyright (C) 2012-2014 Kirill Kouznetsov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.