当前位置: > Linux服务器 > nginx >

nginx错误 “could not build the server_names_hash”解决方法

时间:2014-12-14 22:17来源:linux.it.net.cn 作者:IT

今天在一台nginx负载均衡服务器上批量添加了几十个域名,test的时候报如下错误(只要修改了配置,reload之前必先test -_-):

# /etc/init.d/nginx test

[emerg]: could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64
configuration file /usr/local/nginx/conf/nginx.conf test failed

经查阅资料如下:

保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

在这里我增加了hash bucket size的值,vi编辑nginx主配置,在http{}段添加如下参数:

# vi /usr/local/nginx/conf/nginx.conf
http{
...
server_names_hash_bucket_size 128; #设定值为32的倍数,由原来的64增加到128
...
}

然后再测试一下nginx的配置:

# /etc/init.d/nginx test
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

可见错误已解决,再重载配置即可:

# /etc/init.d/nginx reload
Reloading nginx daemon configuration
reloaded.

在这里也贴上批量添加域名的脚本(nginx做负载,iis后端):

#!/bin/sh

## Add IIS WEBSITE ########################
## By Qitan BLOG: http://blog.catjia.com ##

CON_PATH=/usr/local/nginx

IIS_LIST=(
ad-hqbuy 
admin-partinchina 
cart-hqbuy 
ceec-itppi 
circuit-diagram-en 
companyv2-en 
ebankapi 
en-hcsindex 
...
)

PORT_LIST=(
113 
93 
117 
102 
123 
108 
90 
98 
...
)

IIS_ALL_LEN=${#IIS_LIST[*]}
PORT_ALL_LEN=${#PORT_LIST[*]}
i=0
while [ $i -lt $IIS_ALL_LEN ]
do
	cat > $CON_PATH/vhost/iis/${IIS_LIST[$i]}.hqenet.com.conf <<EOF
server {
        listen       80;
        server_name ${IIS_LIST[$i]}.hqenet.com;
	access_log logs/access.${IIS_LIST[$i]}.hqenet.com.conf.log main;
        charset utf-8;

         location /nginx_status {
                 stub_status on;
                 allow 192.168.0.0/16;
                 deny all;
        }

        location / {
            proxy_pass http://${IIS_LIST[$i]}.hqenet.com;
            proxy_set_header    Host    $host;
        }

        location ~* /.(html)$ {
            return 403;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
EOF
	cat >> $CON_PATH/conf/iis.conf <<EOF
upstream ${IIS_LIST[$i]}.hqenet.com {
	server 192.168.100.15:${PORT_LIST[$i]} max_fails=0 fail_timeout=60s;
}
EOF
  let i++
done

然后附加可执行权限并执行:

# chmod +x add_iis.sh && ./add_iis.sh

最后还得把配置文件引入到nginx.conf主配置(记住是加在http{}段):

# vi /usr/local/nginx/conf/nginx.conf
...
http{
...
    include conf/iis.conf;
    include vhost/iis/*.conf;
}
# /etc/init.d/nginx test
# /etc/init.d/nginx reload
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容