Minimalist nginx vhost template example

Minimal Nginx template for HTTPS with HTTP → HTTPS and www → non-www redirects. No need for separate 80 and 443 vhosts if you only use 443 for traffic.


{% set domains = [domain] + alias|default([]) -%}

server {
  listen 80;
  listen 443 ssl http2;
  server_name {{ domains|join(' ') }};

  if ($scheme = http) {
    return 301 https://$host$request_uri;
  }

  if ($host ~* ^www\.(.*)$) {
    return 301 $scheme://$1$request_uri;
  }

  ssl_certificate /etc/nginx/crt/{{ domain }}.crt;
  ssl_certificate_key /etc/nginx/crt/{{ domain }}.key;

  root /var/www/{{ domain }}/;

  index index.html index.htm;

  access_log /var/log/nginx/{{ domain }}_access.log everything;
  error_log /var/log/nginx/{{ domain }}_error.log;

  location / {
...
  }
}
Human Logic, AI Syntax... Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.

Comments

Popular posts from this blog

FreeRadius with Google Workspace LDAP

Fixing pssh (parallel-ssh) Problems on Debian 10 with Python 3.7