Static Routing

HAProxy

HAProxy routes the incoming requests to the backend services after finding the correct routing rule. On the other hand, when it cannot find any routing rule, the request must be ended. An ordinary method for this is using static content. The static content can be used against the search engines to reduce server costs.

devops@haproxy-01:~$ sudo vi /etc/haproxy/haproxy.conf
...
##################################################
#                    Frontend                    #
##################################################
frontend http_in
    mode http
    bind *:80

    #logging
    option httplog

    #http options
    option http-server-close
    option forwardfor

    #redirection rules

    default_backend be_default
##################################################


##################################################
#                    Backend                     #
##################################################
backend be_default
    mode http

    errorfile 503 /etc/haproxy/errors/503.http
##################################################
...

After this, the static content is created.

devops@haproxy-01:~$ sudo vi /etc/haproxy/errors/503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html>
    <head>
        <title>Fatih Tatoğlu</title>
    </head>
    <body>
        <h1>SERVICE UNAVAILABLE</h1>
        <p>The server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay.</p>
    </body>
</html>

The static response is ready. The HAProxy will return the above HTML content with a 503 HTTP status code if a request cannot be matched by routing.

Before applying this change, the config file must be validated.

devops@haproxy-01:~$ sudo /opt/haproxy/sbin/haproxy -c -V -f /etc/haproxy/haproxy.conf
devops@haproxy-01:~$ sudo systemctl reload haproxy

If there is a cluster environment, the configuration must be applied to all servers.