{ "id": "nginx-config", "title": "nginx Configuration", "category": "web", "tags": ["nginx", "config", "syntax", "reload", "vhost"], "updated": "2025-09-18", "summary": "nginx config structure, common syntax errors, and safe reload procedure.", "sections": [ { "heading": "Config File Layout", "body": "
nginx uses a block-based config syntax. The main file is /etc/nginx/nginx.conf. Site configs live in /etc/nginx/sites-available/ and are symlinked into /etc/nginx/sites-enabled/ to activate them.
Every block opens with { and closes with }. Every directive ends with ;. Missing either one will fail the syntax check.
Always test before reloading. A bad config will prevent nginx from reloading, but it will not take down the running process—the old config stays live.
", "code": "nginx -t\n# or\nnginx -T # prints the full parsed config" }, { "heading": "Reloading vs Restarting", "body": "Use reload, not restart. Reload applies the new config without dropping existing connections.
", "code": "systemctl reload nginx\n\n# Only use restart if you have to—it drops active connections.\nsystemctl restart nginx" }, { "heading": "Common Syntax Errors", "body": "} on a blocklisten directives on the same port across multiple vhosts without default_server resolutionThe error message from nginx -t includes the file name and line number. Read it.
Default paths on Debian/Ubuntu:
", "code": "/var/log/nginx/error.log\n/var/log/nginx/access.log\n\n# Per-vhost logs are usually defined in the server block:\naccess_log /var/log/nginx/mysite.access.log;\nerror_log /var/log/nginx/mysite.error.log;" }, { "heading": "Quick Vhost Template", "body": "Minimal working vhost for a static site:
", "code": "server {\n listen 80;\n server_name example.internal;\n\n root /var/www/example;\n index index.html;\n\n location / {\n try_files $uri $uri/ =404;\n }\n\n access_log /var/log/nginx/example.access.log;\n error_log /var/log/nginx/example.error.log;\n}" } ] }