Category: Nginx

nginx: [emerg] unexpected end of file, expecting “;” or “}” in filename:linenumber

I created a new vhost for a domain to serve static files. Here’s the config…


server {

    root "/path/to/;

    index index.html;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

    location / {
        try_files $uri $uri/ /index.html;

It can’t get simpler than this. However, when I checked the configuration with `nginx -t`, it threw the following error…

nginx: [emerg] unexpected end of file, expecting ";" or "}" in /path/to/

It had exactly 15 lines and the error happened exactly at the end of the file. I was too confused about this. Finally, I decided to check each character of this config. Turned out that I missed the matching quote around the `root` path.

Time to stop using quotes. Nginx can work without quotes too. However, quotes are necessary, if there is a space in the path.

It is easy to catch these kind of errors, if we turned on the syntax highlighting in my editor. While I did turn it on, the configuration was located in a specific path that the editor failed to apply the syntax highlighting on this particular file. Time to fine-tune it!


Correct Nginx try_files syntax with query params

With WordPress sites and Nginx servers, you may have used one of the following try_files directives…

try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php?$args;

Both are incomplete. With the first method, arguments are not passed. With the second method, question mark is present, even when there are no query parameters.

A complete solution is to use the following…

try_files $uri $uri/ /index.php$is_args$args;

$is_args checks, if $args is empty. If empty, the value of $is_args becomes empty. Otherwise “?”. Isn’t that genius!

However, if you have large number of vhosts, then it may be time-consuming to go through every vhost to update this change. Here’s a little one-liner to do the same…

sed -i '/try_files/ s/index.php;/index.php$is_args$args;/' *
sed -i '/try_files/ s/index.php?$args;/index.php$is_args$args;/' *