1. Home
  2. Developer
  3. Nginx Config Generator

Nginx Config Generator

Pick a purpose and options to get an Nginx server block config file.

For every format and option in one place, use Server Config Generators →

Runs in your browser · No upload
The 443 server block only passes nginx -t once the certificate files exist. Get the certificate first, then apply.
Nginx config
# nginx config for example.com (review before use) server { listen 80; listen [::]:80; server_name example.com www.example.com; location / { return 301 https://$host$request_uri; } } server { listen 443 ssl; listen [::]:443 ssl; http2 on; server_name example.com 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_session_cache shared:SSL:10m; ssl_session_timeout 1d; ssl_session_tickets off; server_tokens off; client_max_body_size 10m; # note: add_header in a location block replaces these, so repeat them there if you add any add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always; gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 5; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml; # hide .git, .env and other dotfiles (keep .well-known) location ~ /\.(?!well-known) { deny all; } 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; } }
How to apply
# 1) save as /etc/nginx/conf.d/example.com.conf # 2) test the syntax, then reload sudo nginx -t sudo systemctl reload nginx # certificate with Let's Encrypt (run before enabling the 443 block) sudo certbot certonly --nginx -d example.com -d www.example.com
Review the generated config and check the syntax with nginx -t before using it on a production server. Never put secrets such as tokens or passwords into the config.

Your input is processed in this browser and never sent to a server.

How to use

  1. Pick the purpose: a reverse proxy in front of an app server, static files, an SPA (React or Vue build output) or PHP-FPM.
  2. Enter your domains separated by spaces. The first is the canonical one and is used for the default certificate path and the file name.
  3. For a proxy, enter the app server address (for example http://127.0.0.1:3000); for the others, enter the web root path.
  4. Turn options such as HTTPS, gzip and security headers on or off. Copy the result or download it as domain.conf, place it in /etc/nginx/conf.d/, check it with sudo nginx -t and apply it with sudo systemctl reload nginx.

What each option generates

Option Generated config
HTTPS listen 443 ssl;, certificate and key paths, ssl_protocols TLSv1.2 TLSv1.3;, session cache
HTTP redirect a port 80 server with return 301 https://$host$request_uri;
HTTP/2 http2 on; (syntax for nginx 1.25.1 and newer)
HSTS Strict-Transport-Security "max-age=31536000" (one year)
gzip gzip on; plus MIME types for text, JSON, JS, SVG and more
Security headers X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy
WebSocket Upgrade and Connection headers with map $http_upgrade $connection_upgrade
Static cache expires 30d; for css, js, image and font extensions
Dotfile blocking rejects requests for .git, .env and other dotfiles but allows .well-known
www handling the non-canonical server block returns a 301 to the canonical domain

Example structure

With the defaults (reverse proxy, HTTPS, domains example.com www.example.com) you get two server blocks. The first listens on port 80 and sends every request to https://$host$request_uri. The second listens on 443, loads the certificate, and passes proxy_pass http://127.0.0.1:3000; along with the Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto headers. If you leave the certificate paths empty, the Let’s Encrypt default /etc/letsencrypt/live/example.com/fullchain.pem is used.

Things to know

  • The certificate comes first. The 443 block makes nginx -t fail while the certificate files are missing. The commands below include a certbot certonly example. For the webroot method, turn on “Open the webroot path” and apply the port 80 block first.
  • add_header inheritance: if a child location has even one add_header, every add_header from the parent server block is ignored there. When you add headers in a location, repeat the security headers inside it.
  • Path in proxy_pass: if the address ends with a path such as /api/, the matched part of the request path is replaced by it. Without a path, the original request path is passed through.
  • HSTS: once on, browsers use HTTPS only for a year. Turn it on after confirming that HTTPS and certificate renewal work reliably.
  • WebSocket map: if another config file already defines $connection_upgrade, nginx reports a duplicate. In that case delete the generated map block.
  • Domains and paths accept only safe characters such as letters, digits, ., - and /. A semicolon or brace is not used and is reported instead.
  • Review the generated config before using it on a production server. Rate limiting, proxy caching and log formats are not included.
  • If you run containers, use the Docker Compose Generator and the Dockerfile Generator; for port policy see the Firewall Rule Generator. Status code meanings are in HTTP Status Codes, crawler rules in the robots.txt Generator, and subnet math in the IP Subnet Calculator.

Frequently asked questions

Where do I put the config file?

It depends on the distribution. Debian, Ubuntu and most Docker images read /etc/nginx/conf.d/*.conf. If you use the Debian-style sites-available and sites-enabled, put the same content in sites-available and create a link.

What should I do about 502 Bad Gateway?

nginx could not connect to the app at the proxy_pass address. Check that the app is running, that the address and port are right, and in Docker whether you need the service name instead of 127.0.0.1. The error log is /var/log/nginx/error.log.

How do I get a certificate?

certbot from Let’s Encrypt is the usual way. This tool shows a certbot certonly example. Check issuance and automatic renewal for your own server setup.

Why do static files return 404?

Check the root path and that the nginx user (usually www-data) can read the folder. For an SPA, choose the SPA purpose so refreshes fall back to index.html.

Is my input sent to a server?

No. The config is generated in your browser only.

Your input is processed in this browser and never sent to a server.

Can't find the tool you need?

Tell us what you want to do. We build the most requested tools first.