Learn how to set up HAProxy on pfSense to allow access from WAN to LAN networks using reverse proxy and NAT rules.
HAProxy is a popular open source software that provides high availability, load balancing, and proxying for TCP and HTTP-based applications. It can be installed on pfSense, a free and open source firewall and router that also features unified threat management, load balancing, multi WAN, and more.
One of the common use cases for HAProxy on pfSense is to act as a reverse proxy for web servers on the LAN network. This allows external clients to access the web servers using a single public IP address and domain name, while also providing SSL termination, caching, and security features.
However, what if you want to access the web servers on the LAN network from another WAN network, such as a VPN or a remote office? How do you expose HAProxy to the outside world and route the traffic from WAN to LAN networks?
In this article, we will show you how to configure HAProxy on pfSense for WAN to LAN access, using reverse proxy and NAT rules. We will also explain some of the challenges and solutions for this scenario, and provide some FAQs and a summary at the end.
Table of Contents
Prerequisites
Before we begin, you will need the following:
- A pfSense instance with two network interfaces: one for WAN and one for LAN
- A HAProxy package installed on pfSense
- A web server (or multiple web servers) on the LAN network that you want to access from the WAN network
- A domain name that points to the public IP address of the pfSense WAN interface
- A valid SSL certificate for the domain name (optional, but recommended)
Step 1: Configuring HAProxy on pfSense
The first step is to configure HAProxy on pfSense to listen on both the WAN and LAN interfaces, and to create a backend pool for the web servers on the LAN network.
To do this, follow these steps:
- Log in to the pfSense web interface and go to Services > HAProxy > Backend
- Click on Add to create a new backend pool
- Give the backend pool a name, such as “WebServers”
- Under Server List, click on Add to add a new server
- Give the server a name, such as “WebServer1”
- Enter the IP address and port of the web server on the LAN network, such as “10.0.1.5:80”
- Repeat the steps to add more servers to the backend pool, if needed
- Click on Save to save the backend pool
- Go to Services > HAProxy > Frontend
- Click on Add to create a new frontend
- Give the frontend a name, such as “WebAccess”
- Under Frontend Settings, select the WAN and LAN interfaces as the Listen Addresses
- Enter the port number that you want HAProxy to listen on, such as “443” for HTTPS
- Under SSL Offloading, check the box for Enable SSL Offloading
- Select the SSL certificate that matches the domain name, or upload a new one
- Under ACLs and Actions, click on Add to add a new ACL
- Give the ACL a name, such as “WebDomain”
- Select Host Matches as the Expression
- Enter the domain name that you want to use for accessing the web servers, such as “example.com”
- Under Actions, click on Add to add a new action
- Give the action a name, such as “WebBackend”
- Select Use Backend as the Action
- Select the backend pool that you created earlier, such as “WebServers”
- Click on Save to save the frontend
Step 2: Configuring NAT on pfSense
The next step is to configure NAT on pfSense to prevent NAT from occurring on traffic going from the LAN network to the WAN network. This is necessary because pfSense by default uses NAT for outbound traffic, which would change the source IP address of the LAN clients and break the reverse proxy functionality.
To do this, follow these steps:
- Go to Firewall > NAT > Outbound
- Select Hybrid Outbound NAT rule generation as the Mode
- Click on Save
- Click on Add to add a new NAT rule
- Under Source, enter the LAN network address and mask, such as “10.0.1.0/24”
- Under Destination, enter the WAN network address and mask, such as “192.168.1.0/24”
- Check the box for Do not NAT
- Click on Save to save the NAT rule
Step 3: Testing the Configuration
The final step is to test the configuration and verify that you can access the web servers on the LAN network from the WAN network using the domain name and the port number that you configured on HAProxy.
To do this, follow these steps:
- From a client on the WAN network, open a web browser and enter the domain name and port number, such as “https://example.com:443”
- You should see the web page of the web server on the LAN network
- You can also check the HAProxy statistics page on pfSense to see the traffic and status of the frontend and backend
Frequently Asked Questions (FAQs)
Here are some frequently asked questions and answers related to the topic:
Question: Why do I need to use HAProxy for WAN to LAN access?
Answer: HAProxy provides several benefits for WAN to LAN access, such as:
- Simplifying the firewall rules and port forwarding on pfSense
- Providing SSL termination and encryption for the web traffic
- Balancing the load and improving the performance of the web servers
- Caching and compressing the web content
- Adding security features such as HTTP headers and ACLs
Question: What are the challenges and solutions for WAN to LAN access?
Answer: Some of the challenges and solutions for WAN to LAN access are:
- The web servers on the LAN network may not be aware of the original client IP address, which can affect logging and authentication. To solve this, you can enable the transparent-client-ip feature on HAProxy, which will insert the original client IP address in a custom HTTP header, such as “X-Forwarded-For”. However, this feature only works for traffic coming from the WAN interface, not the LAN interface. Alternatively, you can configure the web servers to read the custom HTTP header and use it as the client IP address.
- The web servers on the LAN network may use absolute URLs that point to the LAN IP address or hostname, which can break the links and images on the web page. To solve this, you can use the rewrite-host feature on HAProxy, which will rewrite the Host header and the Location header to match the domain name that you use for accessing the web servers. Alternatively, you can configure the web servers to use relative URLs or the domain name instead of the LAN IP address or hostname.