H A - P r o x y
---------------
- version 1.1.21
+ version 1.1.22
willy tarreau
- 2003/05/06
+ 2003/09/11
================
| Introduction |
- 'global'
- 'listen'
+ - 'defaults'
Tous les paramètres font référence à la section définie par le dernier mot clé
reconnu.
Les sections de service débutent par le mot clé "listen" :
- listen <nom_instance> <adresse_IP>:<port>
+ listen <nom_instance> [ <adresse_IP>:<plage_ports>[,...] ]
- <nom_instance> est le nom de l'instance décrite. Ce nom sera envoyé dans les
logs, donc il est souhaitable d'utiliser un nom relatif au service relayé. Aucun
test n'est effectué concernant l'unicité de ce nom, qui n'est pas obligatoire,
- mais fortement recommandée.
+ mais fortement recommandée.
+
+- <adresse_IP> est l'adresse IP sur laquelle le relais attend ses connexions.
+ L'absence d'adresse ainsi que l'adresse 0.0.0.0 signifient que les connexions
+ pourront s'effectuer sur toutes les adresses de la machine.
+
+- <plage_ports> correspond soit à un port, soit à une plage de ports sur
+ lesquels le relais acceptera des connexions pour l'adresse IP spécifiée.
+ Cette plage peut être :
+ - soit un port numérique (ex: '80')
+ - soit une plage constituée de deux valeurs séparées par un tiret
+ (ex: '2000-2100') représentant les extrémités incluses dans la
+ plage.
+ Il faut faire attention à l'usage des plages, car chaque combinaison
+ <adresse_IP>:<port> consomme une socket, donc un descripteur de fichier.
+ Le couple <adresse_IP>:<port> doit être unique pour toutes les instances
+ d'une même machine. L'attachement à un port inférieur à 1024 nécessite un
+ niveau de privilège particulier lors du lancement du programme (indépendamment
+ du paramètre 'uid' de la section 'global').
+
+- le couple <adresse_IP>:<plage_ports> peut être répété indéfiniment pour
+ demander au relais d'écouter également sur d'autres adresses et/ou d'autres
+ plages de ports. Pour cela, il suffit de séparer les couples par une virgule.
-- <adresse_IP> est l'adresse IP sur laquelle le relais attend ses
- connexions. L'adresse 0.0.0.0 signifie que les connexions pourront s'effectuer
- sur toutes les adresses de la machine.
+Exemples :
+---------
+ listen http_proxy :80
+ listen x11_proxy 127.0.0.1:6000-6009
+ listen smtp_proxy 127.0.0.1:25,127.0.0.1:587
+ listen ldap_proxy :389,:663
-- <port> est le numéro de port TCP sur lequel le relais attend ses
- connexions. Le couple <adresse_IP>:<port> doit être unique pour toutes les
- instances d'une même machine. L'attachement à un port inférieur à 1024
- nécessite un niveau de privilège particulier lors du lancement du programme
- (indépendamment du paramètre 'uid' de la section 'global').
+Si toutes les adresses ne tiennent pas sur une ligne, il est possible d'en
+rajouter à l'aide du mot clé 'bind'. Dans ce cas, il n'est même pas nécessaire
+de spécifier la première adresse sur la ligne listen, ce qui facilite parfois
+l'écriture de configurations :
-Exemple :
----------
- listen http_proxy 127.0.0.1:80
+ bind [ <adresse_IP>:<plage_ports>[,...] ]
+Exemples :
+----------
+ listen http_proxy
+ bind :80,:443
+ bind 10.0.0.1:10080,10.0.0.1:10443
2.1) Inhibition d'un service
----------------------------
le paramètre "dispatch" sous la forme <adresse_ip>:<port>. Il correspond à un
serveur d'assignation de cookie dans le cas où le service consiste à assurer
uniquement une persistence HTTP, ou bien simplement au serveur destination dans
-le cas de relayage TCP simple.
+le cas de relayage TCP simple. Cet ancien mode ne permet pas de tester l'état
+du serveur distant, et il est maintenant recommandé d'utiliser de préférence
+le mode 'balance'.
Exemple :
---------
2.8) Adresse de sortie
----------------------
-Il est possible de forcer l'adresse utilisée pour établir les connexions
-vers les serveurs à l'aide du paramètre "source". Il est même possible de
-forcer le port, bien que cette fonctionnalité se limite à des usages très
-spécifiques. C'est particulièrement utile en cas d'adressage multiple, et
-plus généralement pour permettre aux serveurs de trouver le chemin de
-retour dans des contextes de routage difficiles. Si l'adresse est 0.0.0.0,
-elle sera choisie librement par le systeme. Si le port est 0, il
-sera choisi librement par le système. Il est à noter que depuis la version
-1.1.18, les tests de bon foncitonnement des serveurs seront aussi effectués à
-partir de la source spécifiée par ce paramètre.
+Il est possible de forcer l'adresse utilisée pour établir les connexions vers
+les serveurs à l'aide du paramètre "source". Il est même possible de forcer le
+port, bien que cette fonctionnalité se limite à des usages très spécifiques.
+C'est particulièrement utile en cas d'adressage multiple, et plus généralement
+pour permettre aux serveurs de trouver le chemin de retour dans des contextes de
+routage difficiles. Si l'adresse est '0.0.0.0' ou '*' ou vide, elle sera choisie
+librement par le systeme. Si le port est '0' ou vide, il sera choisi librement
+par le système. Il est à noter que depuis la version 1.1.18, les tests de bon
+foncitonnement des serveurs seront aussi effectués à partir de la source
+spécifiée par ce paramètre.
Exemples :
----------
- listen http_proxy 0.0.0.0:80
+ listen http_proxy *:80
# toutes les connexions prennent l'adresse 192.168.1.200
source 192.168.1.200:0
- listen rlogin_proxy 0.0.0.0:513
+ listen rlogin_proxy *:513
# utiliser l'adresse 192.168.1.200 et le port réservé 900
source 192.168.1.200:900
Exemple :
---------
- listen http_proxy 0.0.0.0:80
+ listen http_proxy :80
mode http
cookie SERVERID
Exemple : le cookie SERVERID peut contenir server01 ou server02
---------
- listen http_proxy 0.0.0.0:80
+ listen http_proxy :80
mode http
cookie SERVERID
dispatch 192.168.1.100:80
Attention : la syntaxe a changé depuis la version 1.0.
-----------
-3) Répartiteur de charge interne
+3) Répartiteur de charge autonome
=================================
Le relais peut effectuer lui-même la répartition de charge entre les différents
Exemple : même que précédemment en répartition interne
---------
- listen http_proxy 0.0.0.0:80
+ listen http_proxy :80
mode http
cookie SERVERID
balance roundrobin
server web1 192.168.1.1:80 cookie server01
server web2 192.168.1.2:80 cookie server02
+Depuis la version 1.1.22, il est possible de déterminer automatiquement le port
+du serveur vers lequel sera envoyée la connexion, en fonction du port d'écoute
+sur lequel le client s'est connecté. En effet, il y a 4 possibilités pour le
+champ <port> de l'adresse serveur :
+
+ - non spécifié ou nul :
+ la connexion sera envoyée au serveur sur le même port que celui sur
+ lequel le relais a reçu la connexion.
+
+ - valeur numérique (seul cas supporté pour les versions antérieures) :
+ le serveur recevra la connexion sur le port désigné.
+
+ - valeur numérique précédée d'un signe '+' :
+ la connexion sera envoyée au serveur sur le même port que celui sur
+ lequel le relais a reçu la connexion, auquel on ajoute la valeur désignée.
+
+ - valeur numérique précédée d'un signe '-' :
+ la connexion sera envoyée au serveur sur le même port que celui sur
+ lequel le relais a reçu la connexion, duquel on soustrait la valeur
+ désignée.
+
+Exemples :
+----------
+
+# même que précédemment
+
+ listen http_proxy :80
+ mode http
+ cookie SERVERID
+ balance roundrobin
+ server web1 192.168.1.1 cookie server01
+ server web2 192.168.1.2 cookie server02
+
+# relayage simultané des ports 80 et 81 et 8080-8089
+
+ listen http_proxy :80,:81,:8080-8089
+ mode http
+ cookie SERVERID
+ balance roundrobin
+ server web1 192.168.1.1 cookie server01
+ server web2 192.168.1.2 cookie server02
+
+# relayage TCP des ports 25, 389 et 663 vers les ports 1025, 1389 et 1663
+
+ listen http_proxy :25,:389,:663
+ mode tcp
+ balance roundrobin
+ server srv1 192.168.1.1:+1000
+ server srv2 192.168.1.2:+1000
+
3.1) Surveillance des serveurs
------------------------------
- inter : 2000
- rise : 2
- fall : 3
+ - port : port de connexion du serveur
Le mode par défaut consiste à établir des connexions TCP uniquement. Dans
certains cas de pannes, des serveurs peuvent continuer à accepter les connexions
de cookie envoie un cookie vide, ce qui a pour effet de supprimer un éventuel
cookie affecté précédemment.
+Depuis la version 1.1.22, il est possible d'envoyer les tests de fonctionnement
+vers un port différent de celui de service. C'est nécessaire principalement
+pour les configurations où le serveur n'a pas de port prédéfini, par exemple
+lorsqu'il est déduit du port d'acceptation de la connexion. Pour cela, utiliser
+le paramètre 'port' suivi du numéro de port devant répondre aux requêtes.
+
Enfin, depuis la version 1.1.17, il est possible de visualiser rapidement l'état
courant de tous les serveurs. Pour cela, il suffit d'envoyer un signal SIGHUP au
processus proxy. L'état de tous les serveurs de tous les proxies est envoyé dans
Exemples :
----------
-# même que précédemment avec surveillance TCP
+# conf du paragraphe 3) avec surveillance TCP
listen http_proxy 0.0.0.0:80
mode http
cookie SERVERID
server web-backup 192.168.1.3:80 cookie server03 check backup
server web-excuse 192.168.1.4:80 check backup
+# relayage SMTP+TLS avec test du serveur et serveur de backup
+
+ listen http_proxy :25,:587
+ mode tcp
+ balance roundrobin
+ server srv1 192.168.1.1 check port 25 inter 30000 rise 1 fall 2
+ server srv2 192.168.1.2 backup
+
3.2) Reconnexion vers un répartiteur en cas d'échec direct
----------------------------------------------------------
# iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
--dport 80 -j REDIRECT --to-ports 65000
+Remarque :
+----------
+Si le port n'est pas spécifié sur le serveur, c'est le port auquel s'est adressé
+le client qui sera utilisé. Cela permet de relayer tous les ports TCP d'une même
+adresse avec une même instance et sans utiliser directement le mode transparent.
+
+Exemple :
+---------
+ listen http_proxy 0.0.0.0:65000
+ mode tcp
+ server server01 192.168.1.1 check port 60000
+ server server02 192.168.1.2 check port 60000
+
+ # iptables -t nat -A PREROUTING -i eth0 -p tcp -d 192.168.1.100 \
+ -j REDIRECT --to-ports 65000
+
4.2) Journalisation des connexions
----------------------------------
errorloc 503 http://192.168.114.58/error50x.html
errorloc 504 http://192.168.114.58/error50x.html
+4.6) Changement des valeurs par défaut
+--------------------------------------
+
+Dans la version 1.1.22 est apparue la notion de valeurs par défaut, ce qui évite
+de répéter des paramètres communs à toutes les instances, tels que les timeouts,
+adresses de log, modes de fonctionnement, etc.
+
+Les valeurs par défaut sont positionnées dans la dernière section 'defaults'
+précédent l'instance qui les utilisera. On peut donc mettre autant de sections
+'defaults' que l'on veut. Il faut juste se rappeler que la présence d'une telle
+section implique une annulation de tous les paramètres par défaut positionnés
+précédemment, dans le but de les remplacer.
+
+La section 'defaults' utilise la même syntaxe que la section 'listen', aux
+paramètres près qui ne sont pas supportés. Le mot clé 'defaults' peut accepter
+un commentaire en guise paramètre.
+
+Dans la version 1.1.22, seuls les paramètres suivants peuvent être positionnés
+dans une section 'defaults' :
+ - log (le premier et le second)
+ - mode { tcp, http, health }
+ - balance { roundrobin }
+ - disabled (pour désactiver toutes les instances qui suivent)
+ - enabled (pour faire l'opération inverse, mais c'est le cas par défaut)
+ - contimeout, clitimeout, srvtimeout, grace, retries, maxconn
+ - option { redispatch, transparent, keepalive, forwardfor, httplog,
+ dontlognull, persist }
+ - redispatch, redisp, transparent, source { addr:port }
+
+Ne sont pas supportés dans cette version, d'une manière générale, tous les
+paramètres qui nécessitent de mémoriser autre chose que des adresses IP uniques
+et valeurs numériques simples :
+ - dispatch, server,
+ - cookie, capture
+ - errorloc
+ - req*, rsp*,
+
+Enfin, il n'y a pas le moyen, pour le moment, d'invalider un paramètre booléen
+positionné par défaut. Donc si une option est spécifiée dans les paramètres par
+défaut, le seul moyen de la désactiver pour une instance, c'est de changer les
+paramètres par défaut avant la déclaration de l'instance.
+
+Exemples :
+----------
+ defaults applications TCP
+ log global
+ mode tcp
+ balance roundrobin
+ clitimeout 180000
+ srvtimeout 180000
+ contimeout 4000
+ retries 3
+ redispatch
+
+ listen app_tcp1 10.0.0.1:6000-6063
+ server srv1 192.168.1.1 check port 6000 inter 10000
+ server srv2 192.168.1.2 backup
+
+ listen app_tcp2 10.0.0.2:6000-6063
+ server srv1 192.168.2.1 check port 6000 inter 10000
+ server srv2 192.168.2.2 backup
+
+ defaults applications HTTP
+ log global
+ mode http
+ option httplog
+ option forwardfor
+ option dontlognull
+ balance roundrobin
+ clitimeout 20000
+ srvtimeout 20000
+ contimeout 4000
+ retries 3
+
+ listen app_http1 10.0.0.1:80-81
+ cookie SERVERID postonly insert indirect
+ capture cookie userid= len 10
+ server srv1 192.168.1.1:+8000 cookie srv1 check port 8080 inter 1000
+ server srv1 192.168.1.2:+8000 cookie srv2 check port 8080 inter 1000
+
+ defaults
+ # section vide qui annule tous les paramètes par défaut.
=======================
| Paramétrage système |
#include <linux/netfilter_ipv4.h>
#endif
-#define HAPROXY_VERSION "1.1.21"
-#define HAPROXY_DATE "2003/05/06"
+#define HAPROXY_VERSION "1.1.22"
+#define HAPROXY_DATE "2003/09/11"
/* this is for libc5 for example */
#ifndef TCP_NODELAY
#define MODE_QUIET 16
/* server flags */
-#define SRV_RUNNING 1
-#define SRV_BACKUP 2
+#define SRV_RUNNING 1 /* the server is UP */
+#define SRV_BACKUP 2 /* this server is a backup server */
+#define SRV_MAPPORTS 4 /* this server uses mapped ports */
/* what to do when a header matches a regex */
#define ACT_ALLOW 0 /* allow the request */
char *cookie; /* the id set in the cookie */
char *id; /* just for identification */
struct sockaddr_in addr; /* the address to connect to */
+ short check_port; /* the port to use for the health checks */
int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */
int rise, fall; /* time in iterations */
int inter; /* time in milliseconds */
unsigned int uniq_id; /* unique ID used for the traces */
};
+struct listener {
+ int fd; /* the listen socket */
+ struct sockaddr_in addr; /* the address we listen to */
+ struct listener *next; /* next address or NULL */
+};
+
+
struct proxy {
- int listen_fd; /* the listen socket */
+ struct listener *listen; /* the listen addresses and sockets */
int state; /* proxy state */
- struct sockaddr_in listen_addr; /* the address we listen to */
struct sockaddr_in dispatch_addr; /* the default address to connect to */
struct server *srv, *cursrv; /* known servers, current server */
int nbservers; /* # of servers */
static int listeners = 0; /* # of listeners */
static int stopping = 0; /* non zero means stopping in progress */
static struct timeval now = {0,0}; /* the current date at any moment */
+static struct proxy defproxy; /* fake proxy used to assign default values on all instances */
static regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */
/* this is used to drain data, and as a temporary buffer for sprintf()... */
}
+/*
+ * converts <str> to a list of listeners which are dynamically allocated.
+ * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where :
+ * - <addr> can be empty or "*" to indicate INADDR_ANY ;
+ * - <port> is a numerical port from 1 to 65535 ;
+ * - <end> indicates to use the range from <port> to <end> instead (inclusive).
+ * This can be repeated as many times as necessary, separated by a coma.
+ * The <tail> argument is a pointer to a current list which should be appended
+ * to the tail of the new list. The pointer to the new list is returned.
+ */
+struct listener *str2listener(char *str, struct listener *tail) {
+ struct listener *l;
+ char *c, *next, *range, *dupstr;
+ int port, end;
+
+ next = dupstr = strdup(str);
+
+ while (next && *next) {
+ str = next;
+ /* 1) look for the end of the first address */
+ if ((next = strrchr(str, ',')) != NULL) {
+ *next++ = 0;
+ }
+
+ /* 2) look for the addr/port delimiter */
+ if ((range = strrchr(str, ':')) != NULL) {
+ *range++ = 0;
+ }
+ else {
+ Alert("Missing port number: '%s'\n", str);
+ }
+
+ /* 3) look for the port-end delimiter */
+ if ((c = strchr(range, '-')) != NULL) {
+ *c++ = 0;
+ end = atol(c);
+ }
+ else {
+ end = atol(range);
+ }
+
+ for (port = atol(range); port <= end; port++) {
+ l = (struct listener *)calloc(1, sizeof(struct listener));
+ l->next = tail;
+ tail = l;
+
+ if (*str == '*' || *str == '\0') { /* INADDR_ANY */
+ l->addr.sin_addr.s_addr = INADDR_ANY;
+ }
+ else if (
+#ifndef SOLARIS
+ !inet_aton(str, &l->addr.sin_addr)
+#else
+ !inet_pton(AF_INET, str, &l->addr.sin_addr)
+#endif
+ ) {
+ struct hostent *he;
+
+ if ((he = gethostbyname(str)) == NULL) {
+ Alert("Invalid server name: '%s'\n", str);
+ }
+ else
+ l->addr.sin_addr = *(struct in_addr *) *(he->h_addr_list);
+ }
+ l->addr.sin_port=htons(port);
+ l->addr.sin_family=AF_INET;
+ } /* end for(port) */
+ } /* end while(next) */
+ free(dupstr);
+ return tail;
+}
+
+
/*
* This function sends a syslog message to both log servers of a proxy,
* or to global log servers if the proxy is NULL.
}
}
+ /* if this server remaps proxied ports, we'll use
+ * the port the client connected to with an offset. */
+ if (s->srv->state & SRV_MAPPORTS) {
+ struct sockaddr_in sockname;
+ int namelen;
+
+ namelen = sizeof(sockname);
+ if (get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1)
+ getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen);
+ s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port));
+ }
+
if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
qfprintf(stderr, "Cannot get a server socket.\n");
return -1;
shutdown(t->srv_fd, SHUT_RD);
t->srv_state = SV_STSHUTR;
return 1;
- }
+ }
+ /* end of client read and no more data to send */
+ else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
+ FD_CLR(t->srv_fd, StaticWriteEvent);
+ tv_eternity(&t->swexpire);
+ shutdown(t->srv_fd, SHUT_WR);
+ t->srv_state = SV_STSHUTW;
+ return 1;
+ }
/* read timeout */
else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) {
FD_CLR(t->srv_fd, StaticReadEvent);
*/
int process_chk(struct task *t) {
struct server *s = t->context;
+ struct sockaddr_in sa;
int fd = s->curfd;
int one = 1;
(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) {
//fprintf(stderr, "process_chk: 3\n");
+ /* we'll connect to the check port on the server */
+ sa = s->addr;
+ sa.sin_port = htons(s->check_port);
+
/* allow specific binding */
if (s->proxy->options & PR_O_BIND_SRC &&
bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) {
close(fd);
s->result = -1;
}
- else if ((connect(fd, (struct sockaddr *)&s->addr, sizeof(s->addr)) != -1) || (errno == EINPROGRESS)) {
+ else if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) {
/* OK, connection in progress or established */
//fprintf(stderr, "process_chk: 4\n");
*/
static int maintain_proxies(void) {
struct proxy *p;
+ struct listener *l;
int tleft; /* time left */
p = proxy;
while (p) {
if (p->nbconn < p->maxconn) {
if (p->state == PR_STIDLE) {
- FD_SET(p->listen_fd, StaticReadEvent);
+ for (l = p->listen; l != NULL; l = l->next) {
+ FD_SET(l->fd, StaticReadEvent);
+ }
p->state = PR_STRUN;
}
}
else {
if (p->state == PR_STRUN) {
- FD_CLR(p->listen_fd, StaticReadEvent);
+ for (l = p->listen; l != NULL; l = l->next) {
+ FD_CLR(l->fd, StaticReadEvent);
+ }
p->state = PR_STIDLE;
}
}
else { /* block all proxies */
while (p) {
if (p->state == PR_STRUN) {
- FD_CLR(p->listen_fd, StaticReadEvent);
+ for (l = p->listen; l != NULL; l = l->next) {
+ FD_CLR(l->fd, StaticReadEvent);
+ }
p->state = PR_STIDLE;
}
p = p->next;
Warning("Proxy %s stopped.\n", p->id);
send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id);
- fd_delete(p->listen_fd);
+ for (l = p->listen; l != NULL; l = l->next) {
+ fd_delete(l->fd);
+ listeners--;
+ }
p->state = PR_STDISABLED;
- listeners--;
}
else {
tleft = MINTIME(t, tleft);
}
+void init_default_instance() {
+ memset(&defproxy, 0, sizeof(defproxy));
+ defproxy.mode = PR_MODE_TCP;
+ defproxy.state = PR_STNEW;
+ defproxy.maxconn = cfg_maxpconn;
+ defproxy.conn_retries = CONN_RETRIES;
+ defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */
+}
+
/*
* parse a line in a <listen> section. Returns 0 if OK, -1 if error.
*/
struct server *newsrv = NULL;
if (!strcmp(args[0], "listen")) { /* new proxy */
- if (strchr(args[2], ':') == NULL) {
- Alert("parsing [%s:%d] : '%s' expects <id> and <addr:port> as arguments.\n",
+ if (!*args[1]) {
+ Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n"
+ " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n",
file, linenum, args[0]);
return -1;
}
curproxy->next = proxy;
proxy = curproxy;
curproxy->id = strdup(args[1]);
- curproxy->listen_addr = *str2sa(args[2]);
- curproxy->state = PR_STNEW;
+ if (strchr(args[2], ':') != NULL)
+ curproxy->listen = str2listener(args[2], curproxy->listen);
+
/* set default values */
- curproxy->maxconn = cfg_maxpconn;
- curproxy->conn_retries = CONN_RETRIES;
- curproxy->options = 0;
- curproxy->clitimeout = curproxy->contimeout = curproxy->srvtimeout = 0;
- curproxy->mode = PR_MODE_TCP;
- curproxy->logfac1 = curproxy->logfac2 = -1; /* log disabled */
- curproxy->to_log = 0;
+ curproxy->state = defproxy.state;
+ curproxy->maxconn = defproxy.maxconn;
+ curproxy->conn_retries = defproxy.conn_retries;
+ curproxy->options = defproxy.options;
+ curproxy->clitimeout = defproxy.clitimeout;
+ curproxy->contimeout = defproxy.contimeout;
+ curproxy->srvtimeout = defproxy.srvtimeout;
+ curproxy->mode = defproxy.mode;
+ curproxy->logfac1 = defproxy.logfac1;
+ curproxy->logsrv1 = defproxy.logsrv1;
+ curproxy->loglev1 = defproxy.loglev1;
+ curproxy->logfac2 = defproxy.logfac2;
+ curproxy->logsrv2 = defproxy.logsrv2;
+ curproxy->loglev2 = defproxy.loglev2;
+ curproxy->to_log = defproxy.to_log;
+ curproxy->grace = defproxy.grace;
+ curproxy->source_addr = defproxy.source_addr;
+ return 0;
+ }
+ else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */
+ curproxy = &defproxy;
return 0;
}
else if (curproxy == NULL) {
- Alert("parsing [%s:%d] : 'listen' expected.\n", file, linenum);
+ Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum);
return -1;
}
- if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
+ if (!strcmp(args[0], "bind")) { /* new listen addresses */
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
+ if (strchr(args[1], ':') == NULL) {
+ Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n",
+ file, linenum, args[0]);
+ return -1;
+ }
+ curproxy->listen = str2listener(args[1], curproxy->listen);
+ return 0;
+ }
+ else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */
if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP;
else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP;
else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH;
else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
curproxy->state = PR_STDISABLED;
}
+ else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
+ curproxy->state = PR_STNEW;
+ }
else if (!strcmp(args[0], "cookie")) { /* cookie name */
int cur_arg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
if (curproxy->cookie_name != NULL) {
Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n",
file, linenum);
cur_arg++;
}
if ((curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND)) == (PR_O_COOK_RW|PR_O_COOK_IND)) {
- Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' mode are incompatibles.\n",
+ Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' mode are incompatible.\n",
file, linenum);
return -1;
}
}
else if (!strcmp(args[0], "capture")) { /* name of a cookie to capture */
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
if (curproxy->capture_name != NULL) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
file, linenum, args[0]);
}
}
else if (!strcmp(args[0], "contimeout")) { /* connect timeout */
- if (curproxy->contimeout != 0) {
+ if (curproxy->contimeout != defproxy.contimeout) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
return 0;
}
curproxy->contimeout = atol(args[1]);
}
else if (!strcmp(args[0], "clitimeout")) { /* client timeout */
- if (curproxy->clitimeout != 0) {
+ if (curproxy->clitimeout != defproxy.clitimeout) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n",
file, linenum, args[0]);
return 0;
curproxy->clitimeout = atol(args[1]);
}
else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */
- if (curproxy->srvtimeout != 0) {
+ if (curproxy->srvtimeout != defproxy.srvtimeout) {
Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
return 0;
}
curproxy->grace = atol(args[1]);
}
else if (!strcmp(args[0], "dispatch")) { /* dispatch address */
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (strchr(args[1], ':') == NULL) {
Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]);
return -1;
}
else if (!strcmp(args[0], "server")) { /* server address */
int cur_arg;
+ char *rport;
+ char *raddr;
+ short realport;
+ int do_check;
+
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
- if (strchr(args[2], ':') == NULL) {
- Alert("parsing [%s:%d] : '%s' expects <name> and <addr:port> as arguments.\n",
+ if (!*args[2]) {
+ Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
file, linenum, args[0]);
return -1;
}
newsrv->next = curproxy->srv;
curproxy->srv = newsrv;
newsrv->proxy = curproxy;
- newsrv->id = strdup(args[1]);
- newsrv->addr = *str2sa(args[2]);
+
+ do_check = 0;
newsrv->state = SRV_RUNNING; /* early server setup */
+ newsrv->id = strdup(args[1]);
+
+ /* several ways to check the port component :
+ * - IP => port=+0, relative
+ * - IP: => port=+0, relative
+ * - IP:N => port=N, absolute
+ * - IP:+N => port=+N, relative
+ * - IP:-N => port=-N, relative
+ */
+ raddr = strdup(args[2]);
+ rport = strchr(raddr, ':');
+ if (rport) {
+ *rport++ = 0;
+ realport = atol(rport);
+ if (!isdigit((int)*rport))
+ newsrv->state |= SRV_MAPPORTS;
+ } else {
+ realport = 0;
+ newsrv->state |= SRV_MAPPORTS;
+ }
+
+ newsrv->addr = *str2sa(raddr);
+ newsrv->addr.sin_port = htons(realport);
+ free(raddr);
+
newsrv->curfd = -1; /* no health-check in progress */
newsrv->inter = DEF_CHKINTR;
newsrv->rise = DEF_RISETIME;
newsrv->inter = atol(args[cur_arg + 1]);
cur_arg += 2;
}
+ else if (!strcmp(args[cur_arg], "port")) {
+ newsrv->check_port = atol(args[cur_arg + 1]);
+ cur_arg += 2;
+ }
else if (!strcmp(args[cur_arg], "backup")) {
newsrv->state |= SRV_BACKUP;
cur_arg ++;
}
else if (!strcmp(args[cur_arg], "check")) {
- struct task *t;
-
- if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */
- Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
- return -1;
- }
-
- t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
- t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */
- t->state = TASK_IDLE;
- t->process = process_chk;
- t->context = newsrv;
-
- if (curproxy->state != PR_STDISABLED) {
- tv_delayfrom(&t->expire, &now, newsrv->inter); /* check this every ms */
- task_queue(t);
- task_wakeup(&rq, t);
- }
-
+ do_check = 1;
cur_arg += 1;
}
else {
- Alert("parsing [%s:%d] : server %s only supports options 'cookie', 'check', 'inter', 'rise' and 'fall'.\n",
+ Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise' and 'fall'.\n",
file, linenum, newsrv->id);
return -1;
}
}
+
+ if (do_check) {
+ struct task *t;
+
+ if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
+ newsrv->check_port = realport; /* by default */
+ if (!newsrv->check_port) {
+ Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
+ file, linenum, newsrv->id);
+ return -1;
+ }
+
+ if ((t = pool_alloc(task)) == NULL) {
+ Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
+ return -1;
+ }
+
+ t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
+ t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */
+ t->state = TASK_IDLE;
+ t->process = process_chk;
+ t->context = newsrv;
+
+ if (curproxy->state != PR_STDISABLED) {
+ tv_delayfrom(&t->expire, &now, newsrv->inter); /* check this every ms */
+ task_queue(t);
+ task_wakeup(&rq, t);
+ }
+ }
+
curproxy->nbservers++;
}
else if (!strcmp(args[0], "log")) { /* syslog server address */
}
}
else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */
- if (strchr(args[1], ':') == NULL) {
- Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n",
+ if (!*args[1]) {
+ Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n",
file, linenum, "source");
return -1;
}
}
else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0 || *(args[2]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
}
else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0 || *(args[2]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
}
else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
}
else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]);
chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL);
}
else if (!strcmp(args[0], "reqadd")) { /* add request header */
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
if (curproxy->nb_reqadd >= MAX_NEWHDR) {
Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
return 0;
}
else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
}
else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */
- regex_t *preg;
+ regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
- if (*(args[1]) == 0 || *(args[2]) == 0) {
- Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
- file, linenum, args[0]);
- return -1;
- }
+ if (*(args[1]) == 0 || *(args[2]) == 0) {
+ Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n",
+ file, linenum, args[0]);
+ return -1;
+ }
- preg = calloc(1, sizeof(regex_t));
- if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
- Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
- return -1;
- }
-
- chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
+ preg = calloc(1, sizeof(regex_t));
+ if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) {
+ Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]);
+ return -1;
}
+
+ chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2]));
+ }
else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */
regex_t *preg;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
if (*(args[1]) == 0) {
Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]);
chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2]));
}
else if (!strcmp(args[0], "rspadd")) { /* add response header */
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
if (curproxy->nb_rspadd >= MAX_NEWHDR) {
Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]);
return 0;
int errnum;
char *err;
+ if (curproxy == &defproxy) {
+ Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
+ return -1;
+ }
+
if (*(args[2]) == 0) {
Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum);
return -1;
args[arg] = line;
}
- if (!strcmp(args[0], "listen")) /* new proxy */
+ if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */
confsect = CFG_LISTEN;
else if (!strcmp(args[0], "global")) /* global config */
confsect = CFG_GLOBAL;
*/
int start_proxies() {
struct proxy *curproxy;
+ struct listener *listener;
int one = 1;
int fd;
for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
-
if (curproxy->state == PR_STDISABLED)
continue;
- if ((fd = curproxy->listen_fd =
- socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
- Alert("cannot create listening socket for proxy %s. Aborting.\n",
- curproxy->id);
- return -1;
- }
+ for (listener = curproxy->listen; listener != NULL; listener = listener->next) {
+ if ((fd = listener->fd =
+ socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
+ Alert("cannot create listening socket for proxy %s. Aborting.\n",
+ curproxy->id);
+ return -1;
+ }
- if (fd >= global.maxsock) {
- Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n",
- curproxy->id);
- close(fd);
- return -1;
- }
+ if (fd >= global.maxsock) {
+ Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n",
+ curproxy->id);
+ close(fd);
+ return -1;
+ }
- if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
- (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
- (char *) &one, sizeof(one)) == -1)) {
- Alert("cannot make socket non-blocking for proxy %s. Aborting.\n",
- curproxy->id);
- close(fd);
- return -1;
- }
+ if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) ||
+ (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
+ (char *) &one, sizeof(one)) == -1)) {
+ Alert("cannot make socket non-blocking for proxy %s. Aborting.\n",
+ curproxy->id);
+ close(fd);
+ return -1;
+ }
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
- Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n",
- curproxy->id);
- }
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) {
+ Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n",
+ curproxy->id);
+ }
- if (bind(fd,
- (struct sockaddr *)&curproxy->listen_addr,
- sizeof(curproxy->listen_addr)) == -1) {
- Alert("cannot bind socket for proxy %s. Aborting.\n",
- curproxy->id);
- close(fd);
- return -1;
- }
+ if (bind(fd,
+ (struct sockaddr *)&listener->addr,
+ sizeof(listener->addr)) == -1) {
+ Alert("cannot bind socket for proxy %s. Aborting.\n",
+ curproxy->id);
+ close(fd);
+ return -1;
+ }
- if (listen(fd, curproxy->maxconn) == -1) {
- Alert("cannot listen to socket for proxy %s. Aborting.\n",
- curproxy->id);
- close(fd);
- return -1;
- }
+ if (listen(fd, curproxy->maxconn) == -1) {
+ Alert("cannot listen to socket for proxy %s. Aborting.\n",
+ curproxy->id);
+ close(fd);
+ return -1;
+ }
- /* the function for the accept() event */
- fdtab[fd].read = &event_accept;
- fdtab[fd].write = NULL; /* never called */
- fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */
- curproxy->state = PR_STRUN;
- fdtab[fd].state = FD_STLISTEN;
- FD_SET(fd, StaticReadEvent);
- fd_insert(fd);
- listeners++;
-
+ /* the function for the accept() event */
+ fdtab[fd].read = &event_accept;
+ fdtab[fd].write = NULL; /* never called */
+ fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */
+ curproxy->state = PR_STRUN;
+ fdtab[fd].state = FD_STLISTEN;
+ FD_SET(fd, StaticReadEvent);
+ fd_insert(fd);
+ listeners++;
+ }
send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
-
}
return 0;
}