jpprojects

Proxmox Nginx

Nginx

Nginx is a reverse proxy, which I use to terminate HTTPS in my lab. Rather than create a cert for every service, I used a wildcard cert that works with any service domain name. Pi-hole is used for Local DNS Records to point my domains (like notes.parra.lan or immich.home.lan) to nginx for https termination. Nginx then redirects the traffic to the appropriate service.

Every service that uses ngninx needs it's own configuration file in the nginx container, and every client device that will connect to services through ngninx needs to trust the nginx Certificate Authority.

There are 2 ways to configure nginx: in the command line, or with a gui (Nginx Package Manager). I started on the command line, and then transitioned to NPM. If I were to do this again, I would just go straight to NPM. If you do so, you still need to do steps 1, 3, 4, and 6 from the CLI instructions first, and step 7 afterwards.

These are the instructions for creating an nginx reverse proxy server.

Nginx in the Command Line

1. Create the container

Confirm and click Finish on the last tab

Then right click and start the container, and login with username root and the password you just set.

2. Install nginx

First upgrade and update : apt update && upgrade -y.

Then install nginx (and some other utilities): apt install -y nginx libnss3-tools curl.

Make sure nginx is running with systemctl status nginx. You should see active (running).

3. Install mkcert and create the CA

Install the latest mkcert binary: curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64.

Make it exectuable : chmod +x mkcert-v*-linux-amd64. Move it to PATH so it is permanently accessible : mv mkcert-f*-linux-amd64 /usr/local/bin/mkcert

Then, create the local Certificate Athority : mkcert -install.

This will create two files (in a directory which can be found with mkcert -CAROOT): rootCA.pem is the certificate that gets installed onto all client devices rootCA-key.pem stays on this machine.

4. Generate the Wildcard Cert

This cert will be used to terminate HTTPS for every connection that goes through nginx.

First, create a dedicated directory to help keep things tidy: mkdir -p /etc/nginx/certs. Go there: cd /etc/nginx/certs.

Then, issue the wildcard cert: mkcert "*.home.lan" "home.lan".

Note: You NEED to have at least 1 subdomain. As in, browsers will freak out if you try to do service.lan, but service.home.lan is ok.

This will create two files again:

  1. The cert : /etc/nginx/certs/_wildcard.home.lan+1.pem
  2. The key : /etc/nginx/certs/_wildcard.home.lan+1-key.pem

5. Create and enable Config Files

Each service that uses nginx for HTTPS termination needs its own config file. This is a template that can be used for each service, with minor modification.

Create the file : nano /etc/nginx/sites-available/<servicename>

server {
    listen 80;
    server_name <servicename>.home.lan;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name <servicename>.home.lan;

    ssl_certificate     /etc/nginx/certs/_wildcard.home.lan+1.pem;
    ssl_certificate_key /etc/nginx/certs/_wildcard.home.lan+1-key.pem;

    location / {
        proxy_pass http://<service ip address and port>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration and reload:

6. Distribute rootCA.pem to client devices

The easiest way is to output the CA (cat /root/.local/share/mkcert/rootCA.pem) and copy-paste it to another device. Or you can use cp to move it to another device.

To install the CA:

7. Create Pi-hole redirect rule

Use Pi-hole for DNS to redirect queries to https://servicename.home.lan to nginx for port forwarding and HTTPS termination.

In Pi-hole, go to Settings > Local DNS Records in the left menu. Under Local DNS records, fill in the Domain and Associated IP, and click the green + button to add a record. (For example, set Domain to notes.lan and Associated IP to the nginx server IP address).


Nginx Package Manager (GUI)

Like I said, I built this in the same container as the nginx CLI project, and I used the same CA/certs. These first few steps are a bunch of copy/paste in the terminal in order to bring the web console up.

1. Install Docker and Dependencies

2. Pause existing nginx service

3. Create the NPM Docker Compose file

Paste this into the compose file:

services:
  npm:
    image: jc21/nginx-proxy-manager:latest
    container_name: nginx-proxy-manager
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "81:81"
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt

Save and exit with Ctrl+O, Enter and Ctrl+X. Port 81 is NPM’s web UI. Ports 80 and 443 are the actual proxy ports.

4. Start NPM

Navigate to the NPM folder : cd /opt/npm Start the container : docker compose up -d Check it’s running : docker compose ps Make docker run on start : enable docker (the service is already set to run on start).

You should see the container with status Up. Give it 30 seconds to fully initialize, then open a browser and go to:

http://:81

Create the admin credentials: nginxproxyadmin

5. Upload Wildcard Cert and Key

The cert and key are in cd /etc/nginx/certs. You need to download them to then upload them to the GUI.

In the web GUI, go to Certificates in the top bar. Click Add Certificate, and then Custom Certificate. Upload both the key and certificate files and click save.

6. Create the Proxy Hosts

Use the information from the CLI config files to create the proxy hosts in the GUI.

In the GUI, go to Hosts > Proxy Hosts and click Add Proxy Host. Fill in the details:

In the Details tab :

Click the SSL tab:

Click Save

You should be able to access https://<service>.home.lan without issue.

Proxy configurations by service:

These are the some ofconfigurations I used for different services in NPM.

OPNsense

Proxmox

Wazuh

Pi-hole

Nginx