---
# System prepended metadata

title: 'Haproxy :palm_tree:'
tags: [haproxy, Linux, linux, nginx]

---

# Haproxy :palm_tree: 
###### tags: `haproxy` `Linux` `linux` `nginx`

[youtube](https://www.youtube.com/watch?v=qYnA2DFEELw&list=PLQnljOFTspQUhgfvpgfxc-uFlWElKIBr-&ab_channel=HusseinNasser)

[ACL](https://www.haproxy.com/documentation/hapee/latest/configuration/acls/overview/)

[Haproxy](https://www.haproxy.com/documentation/hapee/latest/configuration/acls/overview/)

`haproxy.conf`

```
global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http

frontend  www
    option httplog
    option forwardfor
    option http-server-close
    option httpclose
   
    http-response add-header Strict-Transport-Security max-age=31536000;\ includeSubDomains;\ preload
    http-response add-header X-Frame-Options DENY
   

    bind 172.16.11.71:80
    bind 172.16.11.71:443 ssl crt /etc/haproxy/certs/haproxy01.hack.me.pem alpn h2,http/1.1
    http-request redirect scheme https code 301 unless { ssl_fc }
    mode http

    timeout client 10s
    # http -> https redirect

    acl app1 path_beg -i /app1
    acl app2 path_beg -i /app2

    use_backend app1srv if app1    
    use_backend app2srv if app2

    default_backend nginx_pool

backend app1srv
    mode        http
    server      web01 172.16.11.81:443 check ssl verify none

backend app2srv
    mode        http
    server      web02 172.16.11.82:443 check ssl verify none

backend nginx_pool
    balance     roundrobin
    mode        http
    server      web01 172.16.11.81:443 check ssl verify none
    server      web02 172.16.11.82:443 check ssl verify none

# monotoring
listen stats
    bind :1001
    stats enable
    stats uri /haproxy_stats
    stats auth admin:admin
```

## prepare NGINX
`nano /etc/nginx/sites-available/web01(or your name)`

```
server {
        listen 80;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name web02.hack.me;
        location / {
                try_files $uri $uri/ =404;
        }
}

server {
        listen 443 ssl;
        # if you need ipv6 delete #
        #listen [::]:443 ssl; default_server;
        ssl_certificate /etc/nginx/certificate/web02.hack.me.cert.pem;
        ssl_certificate_key /etc/nginx/certificate/web02.hack.me.key.pem;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name web02.hack.me;
        location / {
                try_files $uri $uri/ =404;
        }
}
```