How to Fix error "nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/

Endrit Qerreti

Endrit Qerreti

Sometimes when you try to reload or restart nginx you get the error

nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/owlhowto.com.save:51

This error means the nginx configuration file has not been configured correctly, or it was configured correctly but you forgot to close the directives with curly braces or ;

And because of this error nginx won't be able to start or restart, solving this issue is easy, simply follow the steps below

Step 1 - Open Nginx Configuration File

The error is being caused on the virtual block that we had created which is located /etc/nginx/sites-enabled/mysite.com, so you need to open the config file with your favorite text editor

sudo nano /etc/nginx/sites-enabled/mysite.com

Now once you have opened the virtual block with the text editor you need to make sure there's no { or ; missing on any parameters on the config file.

See the example config below, this is how a normal virtual block configuration looks like, this config has two open "{" and two "}" to close the block.

server {
        listen 80;
        listen [::]:80;

        root /var/www/owlhowto.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name owlhowto.com www.owlhowto.com;

        location / {
                try_files $uri $uri/ =404;
}


}

Step 2 - Restart Nginx

Once you have inspected the configuration file and added the missing curly braces or ; now you should run the command to make sure the nginx syntax has been configured correctly.

sudo nginx -t 

Once you enter the command above, it should tell you if the test was successful or not, proceed with restart if test was successful

sudo systemctl restart nginx

Step 3 - Delete old .save config files

if nginx won't start even after configuring it correctly, make sure to check there's no files like : mysite.com.save / mysite.com.save.1 located on  

/etc/nginx/sites-enabled/

To see if those files are created there, navigate to the path and type ls command to list all files inside enabled sites directory

#Go to the path
cd /etc/nginx/sites-enabled

#List all files inside the directory
ls

If you see these files then you need to delete them, to delete them simply type the command below

sudo rm /etc/nginx/sites-enabled/*.save

The wildcard command will remove all files that end with .save extension

You can also delete them manually without using the wildcard by using the command below

sudo rm /etc/nginx/sites-enabled/mysite.com.save

Another way of deleting them is to go  to the directory where the .save files are and just delete them

For example : To go to the directory /etc/nginx/sites-enabled type

cd /etc/nginx/sites-enabled

and then simply delete the file with the .save extension

sudo rm mysite.com.save

Once you have deleted the .save files, try to run this command again to check nginx syntax

sudo nginx -t

If not errors came up, then simply restart nginx and your site should be running correctly

sudo systemctl restart nginx

Conclusion

In this tutorial you learned how to fix the error "nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled.