Ffile2fix
Sign in Get started

How to fix nginx: [emerg] unknown directive

nginx: [emerg] unknown directive "more_set_headers" in /etc/nginx/sites-enabled/example.com:14

While parsing its configuration, Nginx found a word in a directive position that it does not know, so it refuses to start or reload. The quoted name and file:line in the message tell you exactly where to look.

Also appears as: nginx: [emerg] unknown directive "brotli" in /etc/nginx/nginx.conf:30 · nginx: [emerg] unknown directive "server" in /etc/nginx/conf.d/site.conf:1 · nginx: [emerg] unknown directive "RewriteEngine" in /etc/nginx/sites-enabled/default:3 · nginx: configuration file /etc/nginx/nginx.conf test failed

Common causes

  • The directive belongs to a module that is not compiled in or loaded, such as more_set_headers (headers-more), brotli or geoip2
  • A typo or a missing semicolon on the previous line, so two directives run together
  • Apache .htaccess syntax (RewriteEngine, RewriteRule) pasted into an Nginx config
  • An invisible UTF-8 BOM or non-breaking space at the start of the file or line, shown as odd characters in the quoted name
  • A directive from a newer Nginx version than the one installed (for example http2 on; requires 1.25.1+)
  • Commercial NGINX Plus-only directives used on open-source Nginx

How to fix it

  1. Run the config test. Run sudo nginx -t. It prints the unknown directive with its file and line number.
  2. Check spelling and the line above. Open the file at that line and check spelling, then make sure the previous directive ends with a semicolon and braces are balanced.
  3. Load the missing module. List modules with nginx -V 2>&1 | tr ' ' '\n' | grep module and ls /etc/nginx/modules-enabled/. Install the package (for example libnginx-mod-http-headers-more-filter on Debian/Ubuntu) or add load_module at the top of nginx.conf.
  4. Translate Apache rules. Replace RewriteRule and similar lines with Nginx equivalents such as try_files, return 301 or rewrite. Nginx never reads .htaccess syntax.
  5. Remove hidden characters. If the quoted name looks garbled, run file yourfile.conf and strip the BOM with sed -i '1s/^\xEF\xBB\xBF//' yourfile.conf, then retest.
  6. Check your Nginx version. Run nginx -v and compare it to the directive's documentation. Use the older equivalent or upgrade Nginx from the official repository.

Shell (diagnose and load a dynamic module)

sudo nginx -t
nginx -v
nginx -V 2>&1 | tr ' ' '\n' | grep -i module
ls /etc/nginx/modules-enabled/
# Debian/Ubuntu: install headers-more
sudo apt install libnginx-mod-http-headers-more-filter
sudo nginx -t && sudo systemctl reload nginx

How to stop it happening again

  • Always run nginx -t before reloading
  • Keep configs in version control and lint them in CI
  • Document required dynamic modules alongside the config

Frequently asked questions

Can I use .htaccess rules in Nginx?

No. Nginx has its own syntax and does not read .htaccess files. Rules must be rewritten as Nginx directives in the server block.

Why does the error show strange characters before the directive name?

That is usually a UTF-8 byte order mark or a non-breaking space from a copy-paste. Save the file as UTF-8 without BOM and retype the line.

Will the running site go down because of this error?

A failed reload keeps the old configuration running. A full restart with a broken config stops Nginx, so always test before restarting.