PortGate

Blueprints · Reverse Proxy & SSL

Production 20 min · Nginx

Hardened Nginx reverse proxy, TLS 1.3, and grade-A headers

Hides the Nginx version, allows TLS 1.2 and 1.3, sets a header baseline, rate-limits login and API paths, and answers bare-IP requests with an empty close.

Live config

Baked-in protections

Step 1 · nginx

Site, headers, and limits

/etc/nginx/sites-available/app.example.com.conf

Point the certificate paths at an existing certificate. client_max_body_size is 20m.

limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    http2 on;
    server_name _;
    ssl_certificate /etc/nginx/ssl/default.crt;
    ssl_certificate_key /etc/nginx/ssl/default.key;
    return 444;
}

server {
    listen 443 ssl;
    http2 on;
    server_name app.example.com;
    server_tokens off;
    client_max_body_size 20m;
    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options "DENY" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; base-uri 'self'" always;

    location /login {
        limit_req zone=login burst=5 nodelay;
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
    }

    location /api/ {
        limit_req zone=api burst=20 nodelay;
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}

Step 2 · bash

Reload Nginx

/root/portgate-blueprints/nginx-reload.sh

Creates a throwaway default certificate so the catch-all TLS listener can parse. Replace it if you already have one.

#!/bin/sh
set -eu
mkdir -p /etc/nginx/ssl
if [ ! -f /etc/nginx/ssl/default.crt ]; then
  openssl req -x509 -nodes -newkey rsa:2048 -days 30 -keyout /etc/nginx/ssl/default.key -out /etc/nginx/ssl/default.crt -subj "/CN=default"
fi
ln -sfn "/etc/nginx/sites-available/app.example.com.conf" /etc/nginx/sites-enabled/app.example.com.conf
nginx -t
systemctl reload nginx