Fix nginx "invalid number of arguments in directive" error
nginx "invalid number of arguments" means a directive got too many or too few values. Look for a missing semicolon, an unquoted space or a missing argument.
nginx: [emerg] invalid number of arguments in "root" directive in /etc/nginx/nginx.conf:6nginx: configuration file /etc/nginx/nginx.conf test failedThe directive named in quotes (root here) received too many or too few arguments. Candidate causes are a missing semicolon, a value containing a space that is not quoted, and a required argument that was left out or one too many. The missing semicolon is easy to miss because it sits above the reported line, so scan line endings first.
In an nginx config, a simple directive is a name plus parameters separated by spaces and ends with a semicolon (nginx beginner's guide). Without the ;, the next line is read as part of the same statement. Each directive has its own accepted argument count in the source (exactly one, one or more, a range and so on), and anything outside it stops with invalid number of arguments in "%s" directive (src/core/ngx_conf_file.c).
[1] Read the file and line number at the end of the error. Run the check again after each edit to see whether the same line comes back.
# nginx -tThe name inside the quotes after in is the directive, and the :6 at the end is the line number. If the error is in an included file, the path points to that file. When a line in conf.d/headers.conf was wrong, the output was:
nginx: [emerg] invalid number of arguments in "proxy_set_header" directive in /etc/nginx/conf.d/headers.conf:1[2] Sort out the cause. These all produced the same error:
| Config | Error |
|---|---|
root /srv/app followed by index index.html; (no ;) | "root" argument count, next line number |
root /srv/my app; (path with a space) | "root" argument count |
proxy_set_header Host; (no value) | "proxy_set_header" argument count |
proxy_pass http://127.0.0.1:9000 http://127.0.0.1:9001; (two targets) | "proxy_pass" argument count |
server_name; (no name) | "server_name" argument count |
return 301 /a /b; (three arguments) | "return" argument count |
[3] For a missing semicolon, in the example below the **directive name is the one missing the ; and the line number is the line after it**, where nginx decided the statement ended. Blank lines, comments or a value spanning lines can push the number further down, so find the named directive in the config and check from there that the statement ends with ;.
location / { proxy_pass http://127.0.0.1:9000 proxy_set_header Host $host;}nginx: [emerg] invalid number of arguments in "proxy_pass" directive in /etc/nginx/nginx.conf:7proxy_pass is on line 6 but the error says 7. Get into the habit of checking one line above the reported number. Add the ; at the end of line 6.
[4] Quote values that contain a space. A path or string with a space is counted as two arguments.
root /srv/my app;nginx: [emerg] invalid number of arguments in "root" directive in /etc/nginx/nginx.conf:5With root "/srv/my app"; the config passed nginx -t. A path without spaces is easier to live with in later edits.
[5] add_header words the error differently. It takes a name, a value and only an optional always. In the hello world example below the error named the third argument as invalid rather than a count problem (more words can give a count error, and a third word always is read as that option without any error).
add_header X-Msg hello world;nginx: [emerg] invalid parameter "world" in /etc/nginx/nginx.conf:5The cause is the same; use add_header X-Msg "hello world";. The documented form is add_header name value [always];.
[6] If an argument is missing or extra, count against the Syntax: line in the docs. The forms checked here:
| Directive | Documented syntax |
|---|---|
root | root path; |
proxy_pass | proxy_pass URL; |
proxy_set_header | proxy_set_header field value; |
return | return code [text]; · return code URL; · return URL; |
return 301 /a /b; has three arguments and matches none of the forms, so it gave the same error. A variable ($host) or a quoted string counts as one.
[7] After fixing, test and then apply. Do not reload until the test passes.
# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulOnce it passes, apply it: nginx -s reload if you run nginx directly (or in a container), systemctl reload nginx if it runs as a systemd service. If the same test reports a brace problem instead, see unexpected end of file, expecting "}".
Analyze your Nginx log in the browser ↗ — nothing is uploaded.