# Build a FreeBSD nginx Web Server with Jails, ZFS, pkg, and Ports

One of the best ways to understand FreeBSD is to build something using the technologies that make FreeBSD distinctive.

In this tutorial, we'll create a small nginx web server while touching four important parts of the FreeBSD ecosystem:

```text
FreeBSD
   |
   +-- Jails  → isolate the web server
   |
   +-- pkg    → install nginx quickly
   |
   +-- ZFS    → snapshot a known-good server
   |
   `-- Ports  → rebuild nginx with custom features
```

We'll first install nginx from FreeBSD's binary package repository and get a working web server.

Once it works, we'll take a ZFS snapshot.

Then we'll replace the standard nginx package with our own build from the FreeBSD Ports Collection, enabling **Brotli compression**, which is not enabled in the standard nginx port configuration.

This demonstrates an important FreeBSD idea:

> Start with binary packages when they do what you need. Drop down to the source-based Ports Collection when you need more control.

## What You'll Need

This tutorial assumes:

- a current supported FreeBSD release
- FreeBSD installed on ZFS
- root access
- Internet access
- a spare IP address on your LAN
- the host has a network interface such as `em0`

Our examples will use:

```text
ZFS pool:        zroot
Host interface:  em0
Jail name:       web
Jail hostname:   web.example
Jail IP:         192.168.1.50
```

Change these values to match your network.

You can determine your ZFS pool with:

```sh
zpool list
```

And your network interfaces with:

```sh
ifconfig
```

For example, your interface might actually be:

```text
igb0
em0
re0
ix0
```

instead of `em0`.

---

# 1. Create a ZFS Dataset for the Jail

We could simply create a directory for our jail, but putting the jail in its own ZFS dataset gives us snapshots, clones, compression, quotas, and other ZFS features.

We'll organize our jails under:

```text
zroot/jails/containers
```

Create the hierarchy if it doesn't already exist:

```sh
zfs create -o mountpoint=/usr/local/jails zroot/jails
zfs create zroot/jails/containers
```

Now create a dataset specifically for nginx:

```sh
zfs create zroot/jails/containers/web
```

Check it:

```sh
zfs list zroot/jails/containers/web
```

It should be mounted at approximately:

```text
/usr/local/jails/containers/web
```

You can confirm:

```sh
zfs get mountpoint zroot/jails/containers/web
```

Our jail now has its own filesystem:

```text
zroot
 |
 `-- jails
      |
      `-- containers
           |
           `-- web
```

That becomes important when we take snapshots later.

---

# 2. Install a FreeBSD Userland Into the Jail

A jail shares the host's kernel, but it needs its own FreeBSD userland.

FreeBSD's `bsdinstall` utility can populate a jail for us:

```sh
bsdinstall jail /usr/local/jails/containers/web
```

The jail userland cannot be newer than the FreeBSD version running on the host.

After installation, take a look:

```sh
ls /usr/local/jails/containers/web
```

It should look very much like a normal FreeBSD installation:

```text
bin
boot
dev
etc
lib
libexec
root
sbin
tmp
usr
var
```

That's because a traditional FreeBSD jail is essentially an isolated FreeBSD userland sharing the host's kernel.

Copy the host's DNS configuration into it:

```sh
cp /etc/resolv.conf /usr/local/jails/containers/web/etc/resolv.conf
```

If you want the jail to use the same timezone as the host:

```sh
cp /etc/localtime /usr/local/jails/containers/web/etc/localtime
```

---

# 3. Configure the Jail

FreeBSD's native jail configuration lives in:

```text
/etc/jail.conf
```

or individual files under:

```text
/etc/jail.conf.d/
```

We'll use the latter.

Create the directory if necessary:

```sh
mkdir -p /etc/jail.conf.d
```

Create:

```text
/etc/jail.conf.d/web.conf
```

with:

```text
web {
    host.hostname = "web.example";
    path = "/usr/local/jails/containers/web";

    interface = "em0";
    ip4.addr = "192.168.1.50";

    exec.start = "/bin/sh /etc/rc";
    exec.stop = "/bin/sh /etc/rc.shutdown";
    exec.clean;

    mount.devfs;
}
```

Remember to change:

```text
em0
192.168.1.50
```

to values appropriate for your network.

For this introductory tutorial we're using a simple jail attached directly to an IP on the host's network interface.

Later we can build a more advanced **VNET jail**, where the jail receives its own virtual network stack and virtual network interface.

---

# 4. Enable and Start Jails

Tell FreeBSD to start configured jails during boot:

```sh
sysrc jail_enable="YES"
```

You can also allow multiple jails to start concurrently:

```sh
sysrc jail_parallel_start="YES"
```

Start our jail:

```sh
service jail start web
```

Check it:

```sh
jls
```

You should see something similar to:

```text
JID  IP Address      Hostname      Path
  1  192.168.1.50    web.example   /usr/local/jails/containers/web
```

We now have a running FreeBSD jail.

---

# 5. Enter the Jail

Use `jexec` to start a shell inside it:

```sh
jexec web /bin/sh
```

Your prompt is now inside the jail.

Try:

```sh
hostname
```

You should get:

```text
web.example
```

Look around:

```sh
uname -a
df -h
ps aux
```

Notice something interesting about:

```sh
uname -a
```

The jail is using the **host's FreeBSD kernel**.

It has its own userland and isolated environment, but it isn't booting another kernel like a virtual machine would.

---

# 6. Install nginx Using pkg

Now we'll do things the easy way.

Inside the jail:

```sh
pkg update
```

The first run may bootstrap the `pkg` package manager.

Search for nginx:

```sh
pkg search nginx
```

Install it:

```sh
pkg install nginx
```

Check what was installed:

```sh
pkg info nginx
```

The important files are under `/usr/local`, because nginx is **third-party software**, not part of the FreeBSD base system.

For example:

```text
/usr/local/sbin/nginx
/usr/local/etc/nginx/
/usr/local/www/nginx/
```

Compare that with something from FreeBSD itself:

```text
/etc/rc.conf
/etc/ssh/sshd_config
/bin/sh
/usr/bin/grep
```

This is the base-system-versus-third-party-software distinction we discussed in the introductory article.

---

# 7. Enable nginx

Enable nginx at boot:

```sh
sysrc nginx_enable="YES"
```

Check the result:

```sh
grep nginx /etc/rc.conf
```

You should see:

```text
nginx_enable="YES"
```

Before starting nginx, test its configuration:

```sh
nginx -t
```

If everything is correct, start it:

```sh
service nginx start
```

Check it:

```sh
service nginx status
```

You should now have nginx listening on port 80.

---

# 8. Test the Web Server

From another machine on your network, browse to:

```text
http://192.168.1.50/
```

Or use:

```sh
curl http://192.168.1.50/
```

You should see nginx's default page.

You can also confirm the listening socket inside the jail:

```sh
sockstat -4 -l
```

Look for something similar to:

```text
www   nginx   ... tcp4   *:80
```

At this point we have:

```text
FreeBSD host
     |
     `-- web jail
            |
            `-- nginx installed with pkg
```

Before changing anything else, let's preserve this working state.

---

# 9. Exit the Jail

Return to the FreeBSD host:

```sh
exit
```

Check that our jail dataset exists:

```sh
zfs list zroot/jails/containers/web
```

Now comes one of the reasons we deliberately created the jail as its own ZFS dataset.

---

# 10. Snapshot the Working nginx Server

Create a snapshot:

```sh
zfs snapshot zroot/jails/containers/web@nginx-pkg-working
```

List snapshots:

```sh
zfs list -t snapshot
```

You should see:

```text
zroot/jails/containers/web@nginx-pkg-working
```

Our setup now conceptually looks like:

```text
FreeBSD host
    |
    `-- ZFS: zroot/jails/containers/web
                 |
                 `-- nginx jail
                       |
                       `-- snapshot
                           @nginx-pkg-working
```

That snapshot represents the jail exactly as it existed when nginx was working with the standard FreeBSD package.

Now we can experiment.

---

# 11. Why the Snapshot Matters

Suppose we're about to rebuild nginx, change dependencies, modify configuration, and generally make a mess.

If everything goes wrong, we can stop the jail:

```sh
service jail stop web
```

and restore the snapshot:

```sh
zfs rollback zroot/jails/containers/web@nginx-pkg-working
```

Then start the jail again:

```sh
service jail start web
```

Everything in that dataset returns to its snapshot state.

That includes:

```text
nginx
configuration files
installed packages
Ports tree
logs
website files
```

Anything created **after** that snapshot is discarded by the rollback.

So don't actually run the rollback unless you want to throw away those changes.

And remember:

> A ZFS snapshot is not a backup.

If the pool itself is lost, snapshots stored on that same pool are lost too.

---

# 12. Now Let's Build nginx From Source

Our normal nginx installation works.

So why compile it?

Because the standard binary package has to choose a configuration that works well for most FreeBSD users.

But nginx supports many optional modules.

FreeBSD's nginx Port currently exposes optional features including:

```text
Brotli compression
GeoIP2
ModSecurity
Lua
NJS
OpenTelemetry
RTMP
Headers More
VTS
Zstandard compression
```

Many of these are disabled by default.

For our example we'll enable:

```text
BROTLI
```

Brotli is a compression algorithm commonly used for web content such as HTML, CSS, and JavaScript.

The default nginx Port has Brotli disabled.

This gives us a practical reason to compile nginx ourselves.

---

# 13. Install the Ports Collection

Enter the jail again:

```sh
jexec web /bin/sh
```

We'll use Git to retrieve the Ports Collection.

Install Git:

```sh
pkg install git
```

At the time this tutorial was written, the current quarterly Ports branch was:

```text
2026Q3
```

Clone it:

```sh
git clone --depth 1 --branch 2026Q3 \
    https://git.FreeBSD.org/ports.git /usr/ports
```

If you're following this tutorial later, use the current quarterly branch instead.

Why quarterly?

Because we're already using binary packages from FreeBSD's normal package repository. Keeping our Ports tree reasonably aligned with the package branch reduces the chance of dependency-version mismatches.

Look at the nginx Port:

```sh
cd /usr/ports/www/nginx
ls
```

You'll see files such as:

```text
Makefile
distinfo
files
pkg-descr
pkg-plist
```

Notice that the nginx source itself isn't sitting here.

The Port is the **recipe** for building nginx.

---

# 14. Look at nginx's Build Options

Run:

```sh
make showconfig
```

You'll see a large list of features.

Among them should be:

```text
BROTLI=off
```

You'll also see many other optional modules such as:

```text
HEADERS_MORE=off
HTTP_GEOIP2=off
LUA=off
MODSECURITY3=off
NJS=off
OTEL=off
RTMP=off
VTS=off
ZSTD=off
```

The exact list evolves as the Port changes.

This is the difference between:

```sh
pkg install nginx
```

and Ports.

With `pkg`, someone has already made these build-time decisions for you.

With Ports, you can make them yourself.

---

# 15. Enable Brotli

Run:

```sh
make config
```

FreeBSD will display an interactive options dialog.

Find:

```text
BROTLI
```

and enable it.

Leave:

```text
DSO
```

enabled as well.

DSO tells nginx to support dynamically loaded modules.

Save your choices and exit.

You can verify them:

```sh
make showconfig | grep BROTLI
```

You should now see:

```text
BROTLI=on
```

FreeBSD stores your selected Port options under:

```text
/var/db/ports/
```

For nginx:

```sh
cat /var/db/ports/www_nginx/options
```

This is another nice feature of Ports: your build choices are explicit and reproducible.

---

# 16. Build nginx

Before removing the currently running package, build the replacement.

From:

```text
/usr/ports/www/nginx
```

run:

```sh
make
```

Ports will automatically:

```text
fetch nginx source
       |
       v
verify checksums
       |
       v
fetch optional module sources
       |
       v
install/build dependencies
       |
       v
apply FreeBSD patches
       |
       v
configure nginx
       |
       v
compile
```

Depending on your machine and selected dependencies, this may take a while.

This is the source code becoming operationally useful.

You aren't manually visiting nginx.org, downloading a tarball, figuring out compiler flags, locating every dependency, and trying to remember where FreeBSD expects files to be installed.

The Port encodes that knowledge.

---

# 17. Replace the Binary nginx Package

Before replacing nginx, keep a copy of the configuration:

```sh
cp /usr/local/etc/nginx/nginx.conf /root/nginx.conf.pkg
```

Now stop nginx:

```sh
service nginx stop
```

From `/usr/ports/www/nginx` remove the currently installed nginx package:

```sh
make deinstall
```

Then install the version we just compiled:

```sh
make install clean
```

Check it:

```sh
pkg info nginx
```

This is an important point:

**nginx built through Ports is still registered with FreeBSD's package database.**

Ports and packages aren't completely separate universes.

The Ports Collection is the source-based machinery from which packages can be produced.

---

# 18. Verify That Brotli Was Built

The current nginx Port builds optional modules dynamically when DSO support is enabled.

Check for the Brotli modules:

```sh
ls /usr/local/libexec/nginx/*brotli*
```

You should see files similar to:

```text
/usr/local/libexec/nginx/ngx_http_brotli_filter_module.so
/usr/local/libexec/nginx/ngx_http_brotli_static_module.so
```

Those didn't exist in our standard build.

We now have nginx functionality that we specifically requested.

---

# 19. Load the Brotli Modules

Edit:

```text
/usr/local/etc/nginx/nginx.conf
```

Near the top of the file, before the `events` block, add:

```nginx
load_module /usr/local/libexec/nginx/ngx_http_brotli_filter_module.so;
load_module /usr/local/libexec/nginx/ngx_http_brotli_static_module.so;
```

Then, inside the existing:

```nginx
http {
}
```

block, add:

```nginx
brotli on;
brotli_comp_level 5;

brotli_types
    text/plain
    text/css
    application/javascript
    application/json
    application/xml
    image/svg+xml;
```

Save the file.

Always test nginx configuration before restarting:

```sh
nginx -t
```

If nginx reports that the configuration is valid:

```sh
service nginx start
```

Check:

```sh
service nginx status
```

---

# 20. Test Brotli

If you don't already have `curl`:

```sh
pkg install curl
```

Then request the site while telling nginx that the client accepts Brotli:

```sh
curl -s \
    -H 'Accept-Encoding: br' \
    -D - \
    -o /dev/null \
    http://192.168.1.50/
```

Look for:

```text
Content-Encoding: br
```

If the test page is extremely small, create a larger HTML page so nginx considers compression worthwhile.

We have now gone from:

```text
pkg install nginx
```

to:

```text
nginx
   +
our own build configuration
   +
Brotli module
```

without manually constructing nginx's build system ourselves.

---

# 21. Compare With the Original Snapshot

Return to the host:

```sh
exit
```

You can ask ZFS what changed since our original working snapshot:

```sh
zfs diff zroot/jails/containers/web@nginx-pkg-working
```

You'll see filesystem changes caused by things such as:

```text
Git
Ports tree
build dependencies
nginx replacement
Brotli modules
configuration changes
```

The snapshot gives us both a recovery point and a useful way to understand what our experiment changed.

---

# 22. Protect Your Custom nginx Build

There is one important issue with installing one customized Port while continuing to use the official package repository.

A future:

```sh
pkg upgrade
```

may eventually find a newer official nginx package.

That package was built with **FreeBSD's options**, not yours.

To prevent `pkg` from replacing your customized nginx accidentally:

```sh
pkg lock nginx
```

Check locks:

```sh
pkg lock -l
```

This is reasonable for a small experimental server.

But it isn't the ideal long-term solution.

---

# 23. The Better Production Approach: Poudriere

If you want custom software across several FreeBSD servers, you generally don't want this:

```text
Server 1 → compile nginx
Server 2 → compile nginx
Server 3 → compile nginx
Server 4 → compile nginx
```

Instead, FreeBSD provides **poudriere**.

The architecture becomes:

```text
               FreeBSD Ports
                     +
               your options
                     |
                     v
                poudriere
                     |
                     v
          custom package repository
                     |
          +----------+----------+
          |          |          |
          v          v          v
       server1    server2    server3

             pkg install nginx
```

You still control the source build.

But production machines simply install binary packages.

This combines two advantages:

```text
Ports
    → source-level customization

pkg
    → simple binary deployment
```

Poudriere also builds packages inside clean FreeBSD jails, making builds considerably more reproducible than compiling arbitrary software directly on each production server.

That is a natural next step after understanding this tutorial.

---

# 24. Updating the Jail

Remember that the jail has two different categories of software.

Third-party applications are updated with:

```sh
pkg upgrade
```

But the FreeBSD userland inside a traditional jail is part of the base system.

For a jail populated from FreeBSD distribution sets, update the jail from the **host** rather than trying to update the base system from inside it.

For example:

```sh
freebsd-update -j web fetch install
```

Then restart the jail if necessary:

```sh
service jail restart web
```

Before doing an important jail upgrade, take another snapshot:

```sh
zfs snapshot zroot/jails/containers/web@before-update
```

That should become a habit:

```text
snapshot
   |
upgrade
   |
test
   |
keep or recover
```

---

# 25. A Few Important Next Steps

Our nginx server works, but it is deliberately simple.

Before exposing a real web server to the public Internet, you would normally also consider:

### HTTPS

Use TLS certificates and serve the site over HTTPS rather than plain HTTP.

### Firewall rules

If your host runs PF or IPFW, explicitly control what traffic can reach the jail.

For a web server, that normally means deciding whether to expose:

```text
TCP 80   HTTP
TCP 443  HTTPS
```

### Logs

nginx logs normally live under:

```text
/var/log/nginx/
```

Learn where your logs are before you need them.

### Backups

ZFS snapshots provide quick recovery, but important data should also be replicated or backed up somewhere outside the pool.

### VNET

Our jail borrowed networking from the host in a relatively simple way.

A future tutorial can give the jail a full virtual network stack:

```text
FreeBSD host
     |
   bridge
     |
   epair
     |
     v
 VNET jail
     |
   nginx
```

That makes networking more flexible and is useful for more sophisticated jail hosts.

---

# What We Just Built

Our final system looks roughly like this:

```text
FreeBSD Host
|
+-- ZFS
|    |
|    `-- zroot/jails/containers/web
|           |
|           +-- @nginx-pkg-working
|           |
|           `-- FreeBSD jail
|                  |
|                  +-- nginx
|                  |
|                  +-- custom Brotli module
|                  |
|                  `-- Ports tree
|
`-- jail(8)
     |
     `-- web
          192.168.1.50
```

More importantly, we've seen several FreeBSD ideas working together.

We started with the simplest approach:

```sh
pkg install nginx
```

Then protected the working system with:

```sh
zfs snapshot
```

Then moved from a generic binary package to a source-based customized build:

```sh
cd /usr/ports/www/nginx
make config
make
make install
```

And all of it happened inside a native FreeBSD jail.

That's the part worth remembering.

**Jails, ZFS, pkg, and Ports are useful individually. FreeBSD becomes much more interesting when you start combining them.**