188

I'm in the process of setting up a new server. The web server of my choice is NGINX. I want to add the domain (e.g. example.com) as a virtual host. I already have two other domains in there and it works fine, but when I try to add the above mentioned domain and start the server it gives me:

Job failed. See system journal and 'systemctl status' for details.

I thought it was because of the dashes, so I tried just various other domains with and without hyphens, but no luck. Same error. what could be causing this? I also tried rebooting, I am really at a loss here. Any help would be greatly appreciated.

I have played around a bit and found out, that, when I only put one domain in, it works. But when I put another domain in, it stops. Here is the output in status:

[root@netzmelone nginx]# systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
      Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
      Active: failed (Result: exit-code) since Sun, 16 Dec 2012 11:38:08 +0000; 7s ago
     Process: 14239 ExecStop=/usr/sbin/nginx -g pid /run/nginx.pid; -s quit (code=exited, status=1/FAILURE)
     Process: 14232 ExecStart=/usr/sbin/nginx -g pid /run/nginx.pid; daemon on; master_process on; (code=exited, status=0/SUCCESS)
     Process: 14242 ExecStartPre=/usr/sbin/nginx -t -q -g pid /run/nginx.pid; daemon on; master_process on; (code=exited, status=1/FAILURE)
    Main PID: 14234 (code=exited, status=0/SUCCESS)
      CGroup: name=systemd:/system/nginx.service

Dec 16 11:38:08 netzmelone nginx[14242]: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
Dec 16 11:38:08 netzmelone nginx[14242]: nginx: configuration file /etc/nginx/nginx.conf test failed
Eddie C.
  • 918
  • 10
  • 16
oengelha
  • 2,057
  • 2
  • 12
  • 8

6 Answers6

394

This is most likely happening because of the long domain name. You can fix this by adding

server_names_hash_bucket_size  64;

at the top of your http block (probably located in /etc/nginx/nginx.conf). I quote from the nginx documentation what to do when this error appears:

In this case, the directive value should be increased to the next power of two.

So in your case it should become 64. If you still get the same error, try increasing to 128 and further.

Reference: https://nginx.org/en/docs/http/server_names.html#optimization

Eddie C.
  • 918
  • 10
  • 16
Jap Mul
  • 17,398
  • 5
  • 55
  • 66
  • 11
    Interestingly, I had to use 64 even though my particular Nginx error message was instructing me to use 32. – Ryan Jun 14 '16 at 14:25
  • 73
    Same as @Ryan -- NGINX was telling me to use 64, but I needed 128 to make it work. – jerzy Dec 19 '16 at 16:30
  • @jerzy Some mundane morning detail was missed, apparently. – Erutan409 Mar 08 '17 at 21:20
  • 5
    @jerzy Even though the slightly ambiguous wording of that error message kind of suggests otherwise, it was probably designed to include the **current value**, not to instruct you what value to use. – anothernode Feb 25 '19 at 10:52
  • @jerzy how would one go about increasing this in userdata when provisioning an ec2 instance. so basically in bash script? – shan Jul 22 '20 at 20:28
  • I am guessing nginx is rather saying to increase the size, and then is concatening your actual size at the end of the message ? it really looks like the `key +" : "+ get(key)` kind of error message. – vincenthavinh Nov 26 '20 at 13:00
57
  • open /etc/nginx/nginx.conf with write privileges
  • uncomment or add server_names_hash_bucket_size 64;
  • restart nginx sudo service nginx restart

If the error still persists:

  • increase server_names_hash_bucket_size in steps 128, 256, 512, and so on (increasing by a power of 2 each time). eg. server_names_hash_bucket_size 128;
  • restart the nginx each time until error is gone (error will always be the same no matter what value you already set)
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Raghavendra
  • 1,419
  • 4
  • 19
  • 28
10

This is how I solved:

cd /etc/nginx/

sudo nano nginx.conf
  • --uncomment or add server_names_hash_bucket_size 64 --
  • --increase server_names_hash_bucket_size "164" --
cd /etc/nginx/sites-available/

sudo nginx -t

  • if all it's ok
sudo service nginx restart
Flavio Oliveira
  • 381
  • 4
  • 12
  • 1
    Thanks ```server_names_hash_bucket_size "164"``` works for me, im hosting a 2 sites under the same port (just for demo only) – Ron Michael Apr 08 '22 at 12:08
9

This sounds perhaps a bit random, but it might help an nginx newb like myself.
I got this error when I left off a ; on the server_name line.

had:

server_name    www.mydomain.com  
access_log     /var/log/nginx/www.mydomain.com; 

fix:

server_name    www.mydomain.com;  
access_log     /var/log/nginx/www.mydomain.com; 

All directives in nginx config files must end with a ;
I often highlight ;s in my file before saving/uploading as a final check after editing.

(To highlight, I use Find All: ;)

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
0

May be you don't have issue with bucketsize. This happen you have previously created symlink in /etc/nginx/sites-enabled/ and you are trying to renew or reinstall ssl with certbot. delete the file from /etc/nginx/sites-enable/your-domain-conf-file and rerun the the certbot command as below

 sudo certbot --nginx -d your-domain-name.com 

It should work.

Arun
  • 3,440
  • 1
  • 11
  • 19
0

open your nginx.conf file, it's in this director /etc/nginx/nginx.conf it's the global nginx configuration.

vi into the nginx.conf and add this line to the http block "server_names_hash_bucket_size 64;"