Skip to content
subuilds.dev

← logbuild #4 passedc9d8c69

Expose GitLab Registry via OPNsense Port Forwarding

Set up OPNsense so the GitLab Container Registry is reached directly, while the GitLab UI stays behind Cloudflare Tunnel. A full guide for self-hosted setups.

· 15 min read

The Problem#

You host GitLab yourself, behind Cloudflare Tunnel. With a tunnel, visitors reach your server through Cloudflare, and you open no ports. In this setup, Docker image pushes fail. Login works:

bash
docker login registry.example.com:5443
Authenticating with existing credentials... [Username: your-username]

i Info → To login with a different account, run 'docker logout' followed by 'docker login'


Login Succeeded

The layers (the parts of an image) start to upload:

bash
docker push registry.example.com:5443/myproject/myimage:latest
The push refers to repository [registry.example.com:5443/myproject/myimage]
5f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7f18b442972b: Preparing
5f70bf18a086: Pushing [==============>                    ]  15.3MB/50.1MB
a3b5c80a4eba: Pushing [========================>          ]  28.1MB/45.2MB

Then the push stops with this error:

bash
unauthorized: authentication required

Cloudflare Tunnel cannot handle the way Docker Registry uploads. The registry sends a large file in many pieces (chunked uploads). It also keeps connections open for a long time.

In Simple Words#

The problem
You run GitLab behind Cloudflare Tunnel. docker login works, but docker push stops in the middle with “unauthorized: authentication required”.
The root cause
Docker Registry sends large files in many pieces and keeps connections open for a long time. Cloudflare Tunnel has upload limits and timeouts, so the push breaks.
The solution
Split the traffic. The GitLab web page stays behind Cloudflare Tunnel. The registry gets its own domain, and OPNsense forwards port 5443 straight to the GitLab VM.
Think of it like this
Your house has a small front door with a guard. Visitors pass through it without trouble, but a large sofa does not fit. So you open a side gate that is only for deliveries. The guard still watches the front door.

The Solution: Hybrid Approach#

Split the traffic into two paths:

  • GitLab UI → Cloudflare Tunnel (DDoS protection, no open ports)
  • GitLab Registry → Direct port forwarding through OPNsense (handles large uploads)

Port forwarding means the firewall takes traffic that arrives on one port and sends it to a machine inside your network.

Architecture Overview#

The registry gets its own path, step by step:

  1. Everything goes through Cloudflare Tunnel. The web page works, but docker push fails in the middle of the upload.
  2. Give the registry its own name, registry.example.com. Its DNS record points to your public IP, and the Cloudflare proxy is off.
  3. On OPNsense, add a port forward and a firewall rule. WAN port 5443 now goes to 192.168.100.22:5443.
  4. docker push now goes straight to the registry, and the large upload finishes. The web page still uses Cloudflare Tunnel.
4 / 4

Prerequisites#

You need:

  • OPNsense firewall (any recent version)
  • GitLab running on a VM (for example 192.168.100.22)
  • A domain where you can change the DNS records
  • SSL certificate (Let’s Encrypt or other)
  • Basic knowledge of NAT and port forwarding

Part 1: GitLab Configuration#

Step 1: Configure GitLab for Separate Registry URL#

Edit /etc/gitlab/gitlab.rb:

bash
# Main GitLab URL (via Cloudflare Tunnel)
external_url 'https://gitlab.example.com'

# Registry with separate URL (direct access)
registry_external_url 'https://registry.example.com:5443'

# Registry configuration
gitlab_rails['registry_enabled'] = true
gitlab_rails['registry_host'] = "registry.example.com"
gitlab_rails['registry_port'] = "5443"
gitlab_rails['registry_api_url'] = "http://127.0.0.1:5000"

# Registry nginx configuration
registry_nginx['enable'] = true
registry_nginx['listen_port'] = 5050
registry_nginx['listen_https'] = true

# SSL certificates for registry
registry_nginx['ssl_certificate'] = "/etc/gitlab/ssl/registry.example.com.crt"
registry_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/registry.example.com.key"

# Important: Allow registry to be accessed on different port
registry_nginx['proxy_set_headers'] = {
  "Host" => "$http_host",
  "X-Real-IP" => "$remote_addr",
  "X-Forwarded-For" => "$proxy_add_x_forwarded_for",
  "X-Forwarded-Proto" => "https",
  "X-Forwarded-Ssl" => "on"
}

Step 2: Generate or Install SSL Certificate#

Get a certificate for the registry domain:

bash
sudo apt install certbot

sudo certbot certonly --standalone -d registry.example.com

Troubleshooting: Port 80 Already in Use#

You may see this error:

bash
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Could not bind TCP port 80 because it is already in use by another process on
this system (such as a web server). Please stop the program in question and then
try again.

GitLab is using port 80. Stop GitLab first:

bash
sudo gitlab-ctl stop

Wait a minute, then try again:

bash
sudo certbot certonly --standalone -d registry.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for registry.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/registry.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/registry.example.com/privkey.pem
This certificate expires on 2026-03-16.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Then copy the certificate and the key to GitLab:

bash
# Copy to GitLab SSL directory
sudo mkdir -p /etc/gitlab/ssl
sudo cp /etc/letsencrypt/live/registry.example.com/fullchain.pem \
        /etc/gitlab/ssl/registry.example.com.crt
sudo cp /etc/letsencrypt/live/registry.example.com/privkey.pem \
        /etc/gitlab/ssl/registry.example.com.key
sudo chmod 600 /etc/gitlab/ssl/*

Step 3: Reconfigure GitLab#

bash
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

# Expected results
ok: run: alertmanager: (pid 69369) 1s
ok: run: gitaly: (pid 69385) 0s
ok: run: gitlab-exporter: (pid 69410) 0s
ok: run: gitlab-kas: (pid 69424) 1s
ok: run: gitlab-workhorse: (pid 69439) 0s
ok: run: logrotate: (pid 69457) 1s
ok: run: nginx: (pid 69466) 0s
ok: run: node-exporter: (pid 69479) 0s
ok: run: postgres-exporter: (pid 69485) 0s
ok: run: postgresql: (pid 69494) 0s
ok: run: prometheus: (pid 69496) 0s
ok: run: puma: (pid 69518) 1s
ok: run: redis: (pid 69523) 0s
ok: run: redis-exporter: (pid 69531) 1s
ok: run: registry: (pid 69540) 0s
ok: run: sidekiq: (pid 69567) 0s

Step 4: Verify Registry is Running#

bash
# Check registry status
sudo gitlab-ctl status registry

# Check registry nginx status
sudo gitlab-ctl status nginx

# Verify registry is listening on port 5443
sudo ss -tlnp | grep 5443
# Should show: tcp   LISTEN 0  511  0.0.0.0:5443  0.0.0.0:*

# Test registry locally
curl -k https://127.0.0.1:5443/v2/

# Should return: {"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
# This is GOOD - registry is working, just requires authentication

Step 5: Configure GitLab VM Firewall (CRITICAL)#

The firewall on the VM itself may block port 5443, even when GitLab listens on it. Open the port.

Using UFW (Ubuntu/Debian)#

bash
# Check if UFW is active
sudo ufw status

# Allow port 5443 from anywhere
sudo ufw allow 5443/tcp

# Or restrict to your LAN only (more secure)
sudo ufw allow from 192.168.100.0/24 to any port 5443 proto tcp

# Reload firewall
sudo ufw reload

# Verify the rule was added
sudo ufw status numbered

Step 6: Test from LAN#

Run these from another machine on your network, not from the GitLab VM:

bash
# Test port connectivity (from your local machine, NOT the GitLab VM)
nc -zv 192.168.100.22 5443
# Should return: Connection to 192.168.100.22 port 5443 [tcp/*] succeeded!

# Test HTTPS (from your local machine)
curl -k https://192.168.100.22:5443/v2/
# Should return: {"errors":[{"code":"UNAUTHORIZED",...}]}

Part 2: OPNsense Configuration#

You set up three things: a port forward, a firewall rule, and NAT reflection.

Step 1: Create Port Forward Rule#

This rule sends traffic that arrives on WAN port 5443 to your GitLab VM. WAN is the side of the firewall that faces the internet.

  1. Go to Firewall → NAT → Port Forward
  2. Click + Add
Setting Value Explanation
Interface WAN Traffic coming from the internet
Protocol TCP Docker Registry uses TCP protocol
Destination WAN address Your router’s public IP
Destination port range From: 5443, To: 5443 External port clients connect to
Redirect target IP 192.168.100.22 Your GitLab VM’s internal IP
Redirect target port 5443 GitLab registry_nginx listening port
Description GitLab Container Registry Helps identify this rule later
NAT reflection Enable Allows LAN clients to use public DNS name
Filter rule association Add associated filter rule Auto-creates firewall rule to allow traffic

Click Save then Apply Changes.

Flow: Internet → Public IP:5443 → OPNsense → 192.168.100.22:5443 → GitLab Registry

OPNsense forward nat port

Step 2: Create/Verify Firewall Rule#

A port forward alone does not let traffic through. You also need a firewall rule.

If you selected “Add associated filter rule” in Step 1, the rule already exists. Check Firewall → Rules → WAN.

If it is not there, create it:

  1. Go to Firewall → Rules → WAN
  2. Click + Add
Setting Value Explanation
Action Pass Allow this traffic through the firewall
Interface WAN Apply rule to traffic coming from the internet
Direction in Only incoming traffic (not outgoing)
Protocol TCP Docker Registry uses TCP protocol
Source Any Accept connections from any IP (or restrict for security)
Destination WAN address Traffic destined for your router’s public IP
Destination port 5443 Only allow traffic to port 5443 (the registry port)
Description Allow GitLab Registry Helps identify this rule later

Click Save and Apply Changes.

Without this rule, the firewall drops the traffic and shows no error.

OPNsense WAN Firewall Rule

NAT reflection (also called hairpin NAT) lets devices on your LAN reach the registry by its public DNS name. They do not need the internal IP.

Without it, a LAN client that opens registry.example.com:5443 tries to reach your public IP from inside the network. That fails.

A DNS override (Step 4) solves the same problem. If you turn on both, one still works when the other fails.

  1. Go to Firewall → Settings → Advanced
  2. Find Reflection for port forwards under Network Address Translation
  3. Set to Enable (or Pure NAT for better performance)
  4. Click Save

OPNsense NAT Reflection Settings

You can also turn on reflection for a single rule, and not for all rules.

Step 4: (Optional) Configure Local DNS Override#

A DNS override gives LAN clients the address 192.168.100.22 directly. Their traffic does not go through the WAN.

This is faster, works when the WAN is down, and puts less load on the router.

  1. Go to Services → Unbound DNS → Overrides
  2. Click + Add
Setting Value Explanation
Host registry The subdomain part (before the domain)
Domain example.com Your actual domain name
Type A (IPv4 address) IPv4 address record
IP address 192.168.100.22 Your GitLab VM’s internal IP
  1. Click Save and Apply

OPNsense Unbound DNS Host Override Configuration

LAN devices now get 192.168.100.22 when they look up registry.example.com. Clients outside your network still reach your public IP.

Check it with nslookup registry.example.com from a device inside your network. You should see 192.168.100.22.

Part 3: DNS Configuration#

Step 1: Create DNS Record#

An A record connects a name to an IP address. Add an A record for registry that points to your public IP:

Type Name Value TTL
A registry YOUR_PUBLIC_IP 300

On Cloudflare, set proxy status to DNS only (grey cloud). Docker Registry traffic cannot go through Cloudflare’s proxy.

Cloudflare DNS

Step 3: Verify DNS Resolution#

bash
# Check DNS resolution
nslookup registry.example.com

# Should return your public IP (or internal IP if using DNS override)
dig registry.example.com +short

Part 4: Testing the Setup#

Step 1: Test from External Network#

Run this from outside your network (a mobile hotspot works):

bash
# Test HTTPS connectivity
curl -k https://registry.example.com:5443/v2/

# Should return: {"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
# This is expected - it means the registry is responding correctly

Step 2: Test Docker Login#

bash
# Login to registry
docker login registry.example.com:5443

# Enter your GitLab username and Personal Access Token

Step 3: Test Push/Pull#

Push a real image:

bash
# Pull Node.js image from Docker Hub
docker pull node:24.12.0-trixie-slim
bash
24.12.0-trixie-slim: Pulling from library/node
f626fba1463b: Pull complete
e669be0904f2: Pull complete
5918935f1407: Pull complete
2ee9d466d7a4: Pull complete
cb778f43df8b: Pull complete
Digest: sha256:9ad7e7db423b2ca7ddcc01568da872701ef6171505bd823978736247885c7eb4
Status: Downloaded newer image for node:24.12.0-trixie-slim
docker.io/library/node:24.12.0-trixie-slim
bash
# Tag for your GitLab registry
docker tag node:24.12.0-trixie-slim \
  registry.example.com:5443/base/docker-images/node:24.12.0-trixie-slim
bash
# Push to your GitLab registry
docker push registry.example.com:5443/base/docker-images/node:24.12.0-trixie-slim
bash
The push refers to repository [registry.example.com:5443/base/docker-images/node]
65de0fe7aaa8: Pushed
300ab2bb9bfa: Pushed
5f1c02153132: Pushed
813c6273ce81: Pushed
742b5304df6e: Pushed
24.12.0-trixie-slim: digest: sha256:507fa69d79feec3c18afedfc4b7ab67d3fdd6f750631066ef34d4d5ea4595c04 size: 1367

Step 4: Verify in GitLab UI#

  1. Go to Deploy → Container Registry
  2. Your node image with tag 24.12.0-trixie-slim should appear

GitLab Container Registry after successful push

Step 5: Test Pulling from Registry#

bash
# Remove local image to test pull
docker rmi registry.example.com:5443/base/docker-images/node:24.12.0-trixie-slim

# Pull from your registry
docker pull registry.example.com:5443/base/docker-images/node:24.12.0-trixie-slim

# Use the image
docker run --rm registry.example.com:5443/base/docker-images/node:24.12.0-trixie-slim node --version
# Should output: v24.12.0

Part 5: Security Considerations#

  1. Create an Alias under Firewall → Aliases named registry_allowed_ips. Add your office, home, and CI/CD runner IPs.
  2. Update the WAN firewall rule: change Source from “Any” to the alias.

Enable Rate Limiting#

Rate limiting sets how many requests one client may send in a period of time. Use OPNsense’s os-nginx plugin, or the rate limiting built into GitLab in /etc/gitlab/gitlab.rb:

bash
# Rate limiting for registry
registry['rate_limiting_enabled'] = true
registry['rate_limiting_threshold'] = 100
registry['rate_limiting_expiry'] = 1.hour

Use Fail2Ban#

Block clients that fail to log in again and again:

bash
# Install fail2ban
sudo apt install fail2ban

# Create GitLab registry jail
sudo cat > /etc/fail2ban/jail.d/gitlab-registry.conf << 'EOF'
[gitlab-registry]
enabled = true
port = 5050
filter = gitlab-registry
logpath = /var/log/gitlab/registry/current
maxretry = 5
bantime = 3600
findtime = 600
EOF

# Create filter
sudo cat > /etc/fail2ban/filter.d/gitlab-registry.conf << 'EOF'
[Definition]
failregex = ^.*unauthorized.*client_addr=<HOST>.*$
ignoreregex =
EOF

# Restart fail2ban
sudo systemctl restart fail2ban

Part 6: Complete Configuration Summary#

Final Architecture#

Component URL Path
GitLab UI https://gitlab.example.com Cloudflare Tunnel → GitLab (192.168.100.22)
GitLab Registry https://registry.example.com:5443 Internet → OPNsense (Port 5443) → GitLab (192.168.100.22:5443)

GitLab Configuration in gitlab.rb#

bash
# External URL (via Cloudflare Tunnel)
external_url 'https://gitlab.example.com'

# Registry configuration (direct access)
registry_external_url 'https://registry.example.com:5443'
gitlab_rails['registry_enabled'] = true
gitlab_rails['registry_host'] = "registry.example.com"
gitlab_rails['registry_port'] = "5443"

# Registry nginx
registry_nginx['enable'] = true
registry_nginx['listen_port'] = 5443
registry_nginx['listen_https'] = true
registry_nginx['ssl_certificate'] = "/etc/gitlab/ssl/registry.example.com.crt"
registry_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/registry.example.com.key"

OPNsense Port Forward#

Setting Value
Interface WAN
Protocol TCP
Destination WAN address
Destination port 5443
Redirect target IP 192.168.100.22
Redirect target port 5443

DNS Configuration#

Type Name Value Proxy
A gitlab (Cloudflare Tunnel IP) Proxied (orange)
A registry YOUR_PUBLIC_IP DNS only (grey)

Conclusion#

This hybrid setup keeps Cloudflare’s DDoS/WAF protection on the GitLab UI. Docker Registry gets the direct connection it needs for large uploads.

It fixes the “unauthorized: authentication required” error that you get when you push through Cloudflare Tunnel.

Key Takeaways#

  1. Cloudflare Tunnel cannot handle Docker Registry — upload limits and timeouts break pushes
  2. OPNsense port forwarding gives registry traffic direct access that you can rely on
  3. Use separate domains for GitLab UI and Registry
  4. SSL certificates are required for both endpoints
  5. Restrict registry access to known IPs when possible

References#