Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

haproxy: restrict specific URLs to specific IP addresses

Published: 04-03-2018 | Author: Remy van Elst | Text only version of this article


Table of Contents


This snippet shows you how to use haproxy to restrict certain URLs to certain IPaddresses. For example, to make sure your admin interface can only be accessedfrom your company IP address.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

This example restricts access to the /admin/ and /helpdesk URL's. It onlyallows access from the IP addresses 20.30.40.50 and 20.30.40.40. Any otherIP addresses will get the standard haproxy 403 forbidden error.

ACL for URL's

It uses the acl option. If the requested path begins with either /admin or/helpdesk haproxy sets the restricted_page acl. haproxy also looks at therequesting source IP address. If that matches any of the two IP addresses, itsets the network_allowed acl. If the allowed_network acl is set and therestricted_page is also set, it allows a visitor to go to the page. If therestricted_page acl is set but the allowed_network is not, haproxy willserve a 403 error, thus, disallowing access to that specific URL.

Note that you can use IP addresses but also networks in the src acl. Both192.168.20.0/24 and 192.168.10.3 work.

frontend example-frontend  [...]  acl network_allowed src 20.30.40.50 20.30.40.40  acl restricted_page path_beg /admin  acl restricted_page path_beg /helpdesk  block if restricted_page !network_allowed  [...]

To use a specific file as error page, use the following config in the defaultssection:

defaults  [...]  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

The http files are regular HTML files with a HTTP response on top, like so:

  HTTP/1.0 403 Forbidden  Cache-Control: no-cache  Connection: close  Content-Type: text/html  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">  <html><head>  <title>403 Forbidden</title>  </head><body>  <h1>Forbidden</h1>  <p>You don't have permission to access this area  on this server.</p>  <hr>  <address>Apache/2.4.12 (Ubuntu) Server at example.org Port 443</address>  </body></html>

This is the default apache error page.

ACL for TCP backends

** update 2017-01-09 **

If you have a non-http service you want to restrict to a few IP's you can use anACL together with the tcp-request connection reject optio. Here below is asimple example for a MySQL service. Do note that this also works in a frontendblock:

listen mysql      bind            1.2.3.4:3306      mode            tcp      acl             network_allowed src 20.30.40.50 8.9.9.0/27      tcp-request     connection reject if !network_allowed      server          mysqlvip 10.0.0.30:3306

More info on acl: http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#aclMore info on errorfile:http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#errorfile

Password if not from whitelisted network

If you want access to the resource, but are not on the whitelisted network, therecommended way is to setup a VPN. But, if that is not feasable, the below trickallows you to prompt for a password if you're from a different network.

First setup a userlist:

userlist UsersFor_Example  user user1 insecure-password password1  user user2 insecure-password password2

This is comparable with your .htpasswd file.

In your frontend, where you have the ACL:

frontend example-frontend  [...]  acl network_allowed src 20.30.40.50 20.30.40.40  acl restricted_page path_beg /admin  acl restricted_page path_beg /helpdesk  block if restricted_page !network_allowed  [...]

Remove the last line (block if) and make the section look like below:

frontend example-frontend  [...]  acl network_allowed src 20.30.40.50 20.30.40.40  acl restricted_page path_beg /admin  acl restricted_page path_beg /helpdesk  acl auth_ok http_auth(UsersFor_Example)  http-request auth if restricted_page !network_allowed !auth_ok  [...]

If you're on the whitelisted network, you are given access. If you're on adifferent network, you will be prompted for a password. Make sure you're onlyusing this over an HTTPS protected frontend.

Tags: acl, apache, errors, haproxy, loadbalancer, restrict, snippets, ssl