Nginx is failing to start because it encountered a configuration directive it doesn’t understand, halting the entire process.

Common Causes and Fixes:

  1. Typo in a Directive Name:

    • Diagnosis: Carefully review your nginx.conf and any included configuration files for misspellings. Common typos include listen as lisen, server_name as server_nam, root as rout, or index as indx.
    • Fix: Correct the misspelled directive. For example, change lisen 80; to listen 80;.
    • Why it works: Nginx parses directives sequentially. An unrecognized word breaks its parsing logic, leading to the error.
  2. Using a Directive from a Newer Nginx Version:

    • Diagnosis: You might be using a directive that was introduced in a version of Nginx you haven’t installed. Check the Nginx version (nginx -v) and compare it against the documentation for the directive in question. For instance, proxy_cache_use_stale error timeout invalid_header updating http_404; might be available in Nginx 1.11.10+, but not older versions.
    • Fix: Either upgrade Nginx to a version that supports the directive or remove/comment out the directive. To upgrade on Ubuntu/Debian:
      sudo apt update
      sudo apt install nginx
      
      On CentOS/RHEL:
      sudo yum update nginx
      
    • Why it works: Newer features require the corresponding code in the Nginx binary. If the binary doesn’t have the code for that directive, it won’t recognize it.
  3. Using a Directive from a Third-Party Nginx Module:

    • Diagnosis: Many custom modules (like ngx_http_vhost_traffic_status_module or ngx_pagespeed) introduce new directives. If you’ve compiled Nginx from source or installed a custom package, ensure the module was correctly compiled into your Nginx binary. Check your Nginx build configuration: nginx -V. Look for --add-module=/path/to/module or similar entries. If the module isn’t listed, it’s not compiled in.
    • Fix: Recompile Nginx from source, including the desired module, or install a package that bundles it. For example, to include ngx_http_vhost_traffic_status_module during compilation:
      ./configure --add-module=/path/to/ngx_http_vhost_traffic_status_module
      make
      sudo make install
      
    • Why it works: Nginx’s core only understands its built-in directives. External modules extend this functionality, and their directives are only recognized if the module’s code is part of the running Nginx process.
  4. Incorrectly Placed Directive:

    • Diagnosis: Some directives are context-specific. For example, root and index belong inside a server or location block, while listen and server_name belong in a server block. Placing root /var/www/html; directly in the http block would cause an error.
    • Fix: Move the directive to its correct context. The root directive should be within a server or location block:
      http {
          server {
              listen 80;
              server_name example.com;
              root /var/www/html; # Correct placement
              index index.html index.htm;
      
              location / {
                  try_files $uri $uri/ =404;
              }
          }
      }
      
    • Why it works: Nginx processes configuration based on hierarchical contexts (main, http, server, location). Directives are designed to operate within the scope of specific contexts where their parameters are meaningful.
  5. Syntax Error within a Directive’s Value:

    • Diagnosis: While less common for "unknown directive," a syntax error within a directive’s parameters can sometimes manifest as one, especially if the parser gets confused. For example, missing a semicolon, or using invalid characters in a port number.
    • Fix: Ensure all directives end with a semicolon ; and that parameters are correctly formatted. For listen, it should be listen 8080; or listen [::]:8080;, not listen 8080.
    • Why it works: The parser expects specific formats for directive arguments. Deviations can lead it to interpret parts of the argument as a new, unknown directive.
  6. Conflicting or Redundant Configuration Files:

    • Diagnosis: If you have multiple include statements pointing to directories that contain overlapping or duplicate configurations, Nginx might load a directive multiple times or in an unexpected order, leading to conflicts. Check your nginx.conf for include directives.
    • Fix: Consolidate or remove redundant configuration files. Ensure your include patterns are specific enough to avoid loading unintended files. For example, if you have include /etc/nginx/sites-enabled/*; and accidentally create a symlink for default in sites-enabled that duplicates configuration from another file, remove one.
    • Why it works: Nginx loads configurations in the order specified by include statements. Duplicate or conflicting definitions can confuse the parser or lead to runtime issues, sometimes manifesting as configuration errors during startup.

After fixing this, the next error you’ll likely encounter is a port already in use error if you’ve tried to bind to a port that another process is already listening on.

Want structured learning?

Take the full Nginx course →