Skip to content

nginx Config Generator

Build a server block for a reverse proxy, static site, single-page app or PHP, with HTTPS, HTTP/2 and security headers, and get the install steps in the order that works on a fresh server.

Free · No signupBuilt in your browser

Start from
Site

Forward requests to an app such as Node.js, Python or Go.

Where your app listens. A Unix socket works too: http://unix:/run/app.sock:

HTTPS
Server
nginx version

Run nginx -v. Ubuntu 24.04 ships 1.24 and Debian 12 ships 1.22; nginx.org packages are newer.

System
Options
81 lines
# example.com, generated by the CtrlOps nginx config generator

# Pass WebSocket upgrades through; send every other request as an ordinary one
map $http_upgrade $connection_upgrade_example_com {
    default upgrade;
    ''      close;
}

# Send every HTTP request to HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    server_tokens off;
    return 301 https://example.com$request_uri;
}

# Send www.example.com to example.com
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/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;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    server_tokens off;
    return 301 https://example.com$request_uri;
}

# example.com
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/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;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    add_header Strict-Transport-Security "max-age=63072000" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    server_tokens off;
    client_max_body_size 20m;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_types text/css text/plain text/xml application/javascript application/json application/xml application/rss+xml image/svg+xml font/ttf font/otf;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade_example_com;

        # An idle WebSocket closes after 60 seconds otherwise
        proxy_read_timeout 1h;
    }
}

Save as /etc/nginx/sites-available/example.com, then link it into sites-enabled. The Install steps tab has the commands in the order that works.

  • HTTP/2 is turned on in the listen line, which every nginx accepts. nginx 1.25.1 and later still work but warn that it is deprecated; pick 1.25.1 or newer if nginx -v says so.

  • HSTS makes browsers use HTTPS for this host for two years, even if the certificate later breaks. Add it once HTTPS works.

Built in this tab as you type. Nothing you enter leaves this page.

From config to live

Deploy your app without the hand-written config.

CtrlOps deploys Node.js, Next.js and React apps from a desktop app: it sets up the server, nginx and SSL, then ships your code, with credentials encrypted on your own machine.

Explore DeploymentDownload for MacDownload for Windows

Keep it handy

nginx directives,
in one line each.

The directives the generated config uses, and what each one does.

Common nginx directives
DirectiveWhat it does
listenThe port and protocol the server block accepts, e.g. 80 or 443 ssl.
server_nameThe domain(s) this block matches, e.g. example.com www.example.com.
http2 onTurns on HTTP/2 in nginx 1.25.1 and later; older versions write listen 443 ssl http2.
rootThe filesystem directory files are served from.
locationRules that apply to matching request paths.
proxy_passForwards a request to a backend (reverse proxy).
try_filesTries each path in turn, falling back to the last - the basis of SPA routing.
return 301Issues a permanent redirect, e.g. HTTP to HTTPS.
ssl_certificatePath to the TLS certificate (with ssl_certificate_key for the key).
add_headerAdds a response header (security, caching, HSTS).
client_max_body_sizeLargest request body nginx accepts (raise to fix 413 errors).

How the config is built

One server block.
No silent surprises.

What is an nginx server block?

nginx matches each request to a server block by its listen port and server_name, then to a location inside it by path. The generator writes the main block for your site, plus a port 80 block that redirects to HTTPS and one that redirects www, when you turn those on.

Get the certificate before you test

nginx -t fails while the certificate files a config names do not exist, which is the usual first-day snag with Let's Encrypt. So the install steps run certbot certonly --nginx first, then save the config, link it into sites-enabled on Debian and Ubuntu, test, and reload with no downtime. Renewal then runs on its own timer.

HTTP/2 and your nginx version

nginx 1.25.1 replaced listen 443 ssl http2 with a separate http2 on;. The new directive is unknown to older versions, and Ubuntu 24.04 ships 1.24 and Debian 12 ships 1.22, so a config copied from a newer guide stops nginx -t there. Pick your version and the generator writes the form it accepts.

Headers that survive every location

An add_header inside a location drops every add_header from the server block for those requests, so a cache rule with its own header quietly removes HSTS and the security headers from your images and scripts. The cache rule here uses expires instead, and keeps them all. Dotfiles such as .git and .env are refused before any other rule can serve them.

Reverse proxy and WebSockets

The proxy forwards Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto, so your app sees the real visitor and knows the page was HTTPS. With WebSockets on, a map passes the upgrade through only when the browser asks for one, and the read timeout is raised so an idle socket is not cut after 60 seconds. Point it at 127.0.0.1 rather than localhost, which nginx tries as IPv6 first.

Password-protect the site with the htpasswd generator

nginx FAQ

nginx config,
explained.

Keep working

Related tools for your next task.

Your privacy preferences. We use product analytics to improve CtrlOps. Google measurement runs without analytics cookies. Live chat stays off until you allow it. Cookie policy