2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-10-29 14:55:25 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.haproxy;
|
|
|
|
haproxyCfg = pkgs.writeText "haproxy.conf" cfg.config;
|
|
|
|
in
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2013-10-29 14:55:25 +00:00
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.haproxy = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
2015-02-21 12:23:48 +00:00
|
|
|
type = types.bool;
|
2013-10-29 14:55:25 +00:00
|
|
|
default = false;
|
2015-02-21 12:23:48 +00:00
|
|
|
description = ''
|
|
|
|
Whether to enable HAProxy, the reliable, high performance TCP/HTTP
|
|
|
|
load balancer.
|
|
|
|
'';
|
2013-10-29 14:55:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkOption {
|
2015-02-21 12:23:48 +00:00
|
|
|
type = types.lines;
|
2013-10-29 14:55:25 +00:00
|
|
|
default =
|
|
|
|
''
|
|
|
|
global
|
|
|
|
log 127.0.0.1 local6
|
|
|
|
maxconn 24000
|
|
|
|
daemon
|
|
|
|
nbproc 1
|
|
|
|
|
|
|
|
defaults
|
|
|
|
mode http
|
|
|
|
option httpclose
|
|
|
|
|
|
|
|
# Remove requests from the queue if people press stop button
|
|
|
|
option abortonclose
|
|
|
|
|
|
|
|
# Try to connect this many times on failure
|
|
|
|
retries 3
|
|
|
|
|
|
|
|
# If a client is bound to a particular backend but it goes down,
|
|
|
|
# send them to a different one
|
|
|
|
option redispatch
|
|
|
|
|
|
|
|
monitor-uri /haproxy-ping
|
|
|
|
|
|
|
|
timeout connect 7s
|
|
|
|
timeout queue 300s
|
|
|
|
timeout client 300s
|
|
|
|
timeout server 300s
|
|
|
|
|
|
|
|
# Enable status page at this URL, on the port HAProxy is bound to
|
|
|
|
stats enable
|
|
|
|
stats uri /haproxy-status
|
|
|
|
stats refresh 5s
|
|
|
|
stats realm Haproxy statistics
|
|
|
|
'';
|
2015-02-21 12:23:48 +00:00
|
|
|
description = ''
|
|
|
|
Contents of the HAProxy configuration file,
|
|
|
|
<filename>haproxy.conf</filename>.
|
|
|
|
'';
|
2013-10-29 14:55:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
systemd.services.haproxy = {
|
|
|
|
description = "HAProxy";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "forking";
|
2015-02-21 12:23:48 +00:00
|
|
|
PIDFile = "/run/haproxy.pid";
|
2013-10-29 14:55:25 +00:00
|
|
|
ExecStartPre = "${pkgs.haproxy}/sbin/haproxy -c -q -f ${haproxyCfg}";
|
2015-02-21 12:23:48 +00:00
|
|
|
ExecStart = "${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /run/haproxy.pid";
|
|
|
|
ExecReload = "-${pkgs.bash}/bin/bash -c \"exec ${pkgs.haproxy}/sbin/haproxy -D -f ${haproxyCfg} -p /run/haproxy.pid -sf $MAINPID\"";
|
2013-10-29 14:55:25 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.haproxy ];
|
|
|
|
|
|
|
|
users.extraUsers.haproxy = {
|
|
|
|
group = "haproxy";
|
|
|
|
uid = config.ids.uids.haproxy;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraGroups.haproxy.gid = config.ids.uids.haproxy;
|
|
|
|
};
|
|
|
|
}
|