Overview

Gunicorn pulls configuration information from three distinct places.

The first place that Gunicorn will read configuration from is the framework specific configuration file. Currently this only affects Paster applications.

The second source of configuration information is a configuration file that is optionally specified on the command line. Anything specified in the Gunicorn config file will override any framework specific settings.

Lastly, the command line arguments used to invoke Gunicorn are the final place considered for configuration settings. If an option is specified on the command line, this is the value that will be used.

Once again, in order of least to most authoritative:
  1. Framework Settings
  2. Configuration File
  3. Command Line

Framework Settings

Currently, only Paster applications have access to framework specific settings. If you have ideas for providing settings to WSGI applications or pulling information from Django's settings.py feel free to open an issue to let us know.

Paster Applications

In your INI file, you can specify to use Gunicorn as the server like such:

[server:main]
use = egg:gunicorn#main
host = 192.168.0.1
port = 80
workers = 2
proc_name = brim

Any parameters that Gunicorn knows about will automatically be inserted into the base configuration. Remember that these will be overridden by the config file and/or the command line.

Configuration File

The configuration file should be a valid Python source file. It only needs to be readable from the file system. More specifically, it does not need to be importable. Any Python is valid. Just consider that this will be run every time you start Gunicorn (including when you signal Gunicorn to reload).

To set a parameter, just assign to it. There's no special syntax. The values you provide will be used for the configuration values.

For instance:

import os

def numCPUs():
    if not hasattr(os, "sysconf"):
        raise RuntimeError("No sysconf detected.")
    return os.sysconf("SC_NPROCESSORS_ONLN")

bind = "127.0.0.1:8000"
workers = numCPUs() * 2 + 1

Command Line

If an option is specified on the command line, it overrides all other values that may have been specified in the app specific settings, or in the optional configuration file. Not all Gunicorn settings are available to be set from the command line. To see the full list of command line settings you can do the usual:

$ gunicorn -h

There is also a --version flag available to the command line scripts that isn't mentioned in the list of settings.

Settings

This is an exhaustive list of settings for Gunicorn. Some settings are only able to be set from a configuration file. The setting name is what should be used in the configuration file. The command line arguments are listed as well for reference on setting at the command line.

Config File

config

  • -c FILE, --config FILE
  • None

The path to a Gunicorn config file.

Only has an effect when specified on the command line or as part of an application specific configuration.

Server Socket

bind

  • -b ADDRESS, --bind ADDRESS
  • 127.0.0.1:8000

The socket to bind.

A string of the form: 'HOST', 'HOST:PORT', 'unix:PATH'. An IP is a valid HOST.

backlog

  • --backlog INT
  • 2048

The maximum number of pending connections.

This refers to the number of clients that can be waiting to be served. Exceeding this number results in the client getting an error when attempting to connect. It should only affect servers under significant load.

Must be a positive integer. Generally set in the 64-2048 range.

Worker Processes

workers

  • -w INT, --workers INT
  • 1

The number of worker process for handling requests.

A positive integer generally in the 2-4 x $(NUM_CORES) range. You'll want to vary this a bit to find the best for your particular application's work load.

worker_class

  • -k STRING, --worker-class STRING
  • egg:gunicorn#sync

The type of workers to use.

The default async class should handle most 'normal' types of work loads. You'll want to read http://gunicorn.org/design.hml for information on when you might want to choose one of the other worker classes.

An string referring to a 'gunicorn.workers' entry point or a MODULE:CLASS pair where CLASS is a subclass of gunicorn.workers.base.Worker.

The default provided values are:

  • egg:gunicorn#sync
  • egg:gunicorn#eventlet - Requires eventlet >= 0.9.7
  • egg:gunicorn#gevent - Requires gevent >= 0.12.2 (?)
  • egg:gunicorn#tornado - Requires tornado >= 0.2

worker_connections

  • --worker-connections INT
  • 1000

The maximum number of simultaneous clients.

This setting only affects the Eventlet and Gevent worker types.

timeout

  • -t INT, --timeout INT
  • 30

Workers silent for more than this many seconds are killed and restarted.

Generally set to thirty seconds. Only set this noticeably higher if you're sure of the repercussions for sync workers. For the non sync workers it just means that the worker process is still communicating and is not tied to the length of time required to handle a single request.

keepalive

  • --keep-alive INT
  • 2

The number of seconds to wait for requests on a Keep-Alive connection.

Generally set in the 1-5 seconds range.

Debugging

debug

  • --debug
  • False

Turn on debugging in the server.

This limits the number of worker processes to 1 and changes some error handling that's sent to clients.

spew

  • --spew
  • False

Install a trace function that spews every line executed by the server.

This is the nuclear option.

Server Mechanics

preload_app

  • --preload
  • False

Load application code before the worker processes are forked.

By preloading an application you can save some RAM resources as well as speed up server boot times. Although, if you defer application loading to each worker process, you can reload your application code easily by restarting workers.

daemon

  • -D, --daemon
  • False

Daemonize the Gunicorn process.

Detaches the server from the controlling terminal and enters the background.

pidfile

  • -p FILE, --pid FILE
  • None

A filename to use for the PID file.

If not set, no PID file will be written.

user

  • -u USER, --user USER
  • None

Switch worker processes to run as this user.

A valid user id (as an integer) or the name of a user that can be retrieved with a call to pwd.getpwnam(value) or None to not change the worker process user.

group

  • -g GROUP, --group GROUP
  • None

Switch worker process to run as this group.

A valid group id (as an integer) or the name of a user that can be retrieved with a call to pwd.getgrnam(value) or None to not change the worker processes group.

umask

  • -m INT, --umask INT
  • 0

A bit mask for the file mode on files written by Gunicorn.

Note that this affects unix socket permissions.

A valid value for the os.umask(mode) call or a string compatible with int(value, 0) (0 means Python guesses the base, so values like "0", "0xFF", "0022" are valid for decimal, hex, and octal representations)

tmp_upload_dir

  • None

Directory to store temporary request data as they are read.

This may disappear in the near future.

This path should be writable by the process permissions set for Gunicorn workers. If not specified, Gunicorn will choose a system generated temporary directory.

Logging

logfile

  • --log-file FILE
  • -

The log file to write to.

"-" means log to stdout.

loglevel

  • --log-level LEVEL
  • info

The granularity of log outputs.

Valid level names are:

  • debug
  • info
  • warning
  • error
  • critical

Process Naming

proc_name

  • -n STRING, --name STRING
  • gunicorn

A base to use with setproctitle for process naming.

This affects things like ps and top. If you're going to be running more than one instance of Gunicorn you'll probably want to set a name to tell them apart. This requires that you install the setproctitle module.

It defaults to 'gunicorn'.

default_proc_name

  • gunicorn

Internal setting that is adjusted for each type of application.

Server Hooks

pre_fork

  • def def_pre_fork(server, worker):
        pass
    

Called just before a worker is forked.

The callable needs to accept two instance variables for the Arbiter and new Worker.

post_fork

  • def def_post_fork(server, worker):
        server.log.info("Worker spawned (pid: %s)" % worker.pid)
    

Called just after a worker has been forked.

The callable needs to accept two instance variables for the Arbiter and new Worker.

when_ready

  • def def_start_server(server):
        pass
    

Called just after the server is started.

The callable needs to accept a single instance variable for the Arbiter.

pre_exec

  • def def_pre_exec(server):
        server.log.info("Forked child, reexecuting.")
    

Called just before a new master process is forked.

The callable needs to accept a single instance variable for the Arbiter.