Error Setting up Server Blocks on Nginx

Posted August 6, 2014 19.7k views

I am going through How To Set Up nginx Virtual Hosts (Server Blocks) on CentOS 6.

I’ve previously set up a LEMP stack and moved my web root to /var/www. That works correctly.

On step 5 of the tutorial, I get an error when restarting nginx. I run sudo systemctl restart nginx, and when I do so I get the following error: Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

Running systemctl status nginx.service produces this message:

I’ve double-checked that nginx has access to read and write the files referenced in the tutorial. When I remove virtual.conf, the server starts normally. Here’s my virtual.conf:

Any thoughts on what I might be doing wrong here? Thanks in advance for your help!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Looking at the output of systemctl status nginx.service shows that the command:

nginx -t -c /etc/nginx/nginx.conf

is exiting with a non-0 status which indicates a problem. Run that command on it’s own and it should tell you where the problem in your configuration is located. For instance, I just purposely put a typo in my /etc/nginx/sites-enabled/default file. This is the output of the command:

# nginx -t -c /etc/nginx/nginx.conf
nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:113
nginx: configuration file /etc/nginx/nginx.conf test failed

It should tell you exactly what line the problem is at. In this case, it is on line 113.

  • Ah, that makes sense!

    Running that command produces the following output:

    nginx: [emerg] could not build the servernameshash, you should increase servernameshashbucketsize: 32
    nginx: configuration file /etc/nginx/nginx.conf test failed

    I edited nginx.conf, added the servernameshashbucketsize, and set the value to 64. The documentation says that this error appears if you have “a large number of server names are defined, or unusually long server names are defined”, but I only have two server names, and they’re not especially long.

    Does this change sound like a reasonable solution to the problem?

  • Yes, that’s fine, it’s actually the only way to fix it. You don’t have to worry too much about it, 64 is okay and should last you a while before you would have to increase it again.