【问题标题】:Adding domain to NGINX config, need to increase server_names_hash_bucket_size, but HTTP directive is failing将域添加到 NGINX 配置,需要增加 server_names_hash_bucket_size,但 HTTP 指令失败
【发布时间】:2014-08-26 18:42:26
【问题描述】:

我需要将现有站点移动到另一台服务器。我有问题。这是原始(工作)nginx 配置:

# Define a default server, to catch requests directly to the servers IP or other non-valid sources/domains/etc.
server {
    listen                          80 default_server;
    return                          444;
}


# main.domain1 (everything defaults to this subdomain, it's a domain hack)
server {
    listen                          80;
    listen                          443 ssl;
    server_name                     main.domain1.com;
    ssl_certificate                 /etc/nginx/ssl/main.domain1.com.bundle.crt;
    ssl_certificate_key             /etc/nginx/ssl/main.domain1.com.key;
    root                            /www/main.domain1.com;
    index                           index.php index.html index.htm;

    location /blog {
        try_files                   $uri /?p=blog&$args;
    }

    location /contact {
        try_files                   $uri /?p=contact&$args;
    }

    location /pics {
        autoindex                   on;
    }

    location ~ /\. {
        return                      444;
    }

    location ~ \.php$ {
        #try_files                  $uri =404;
        fastcgi_split_path_info     ^(.+\.php)(/.+)$;
        #fastcgi_pass               unix:/var/run/php5-fpm.sock;
        fastcgi_pass                127.0.0.1:9000;
        fastcgi_index               index.php;
        include                     fastcgi_params;
    }
}
# no sub-domain or one of the other TLD's should direct to main.domain1.com
server {
    listen                          80;
    listen                          443 ssl;
    server_name                     domain1.com .domain1.net .domain1.org;
    return                          301 $scheme://main.domain1.com$request_uri;
}


# domain2.com (note: this is just a testing domain for main.domain1 without the subdomain)
server {
    listen                          80;
    server_name                     domain2.com;
    root                            /www/domain2.com;
    index                           index.php index.html index.htm;

    location /blog {
        try_files                   $uri /?p=blog&$args;
    }

    location /contact {
        try_files                   $uri /?p=contact&$args;
    }

    location /pics {
        autoindex                   on;
    }

    location ~ /\. {
        return                      444;
    }

    location ~ \.php$ {
        #try_files                  $uri =404;
        fastcgi_split_path_info     ^(.+?\.php)(/.+)$;
        #fastcgi_pass               unix:/var/run/php5-fpm.sock;
        fastcgi_pass                127.0.0.1:9000;
        include                     fastcgi_params;
        auth_basic                  "Restricted";
        auth_basic_user_file        /www/domain2.com/.htpasswd;
    }
}


# domain3.com
server {
    listen                          80;
    server_name                     domain3.com;
    root                            /www/domain3.com;
    index                           index.php index.html index.htm;

    location ~ /\. {
        return                      444;
    }

    location ~ \.php$ {
        #try_files                  $uri =404;
        fastcgi_split_path_info     ^(.+\.php)(/.+)$;
        #fastcgi_pass               unix:/var/run/php5-fpm.sock;
        fastcgi_pass                127.0.0.1:9000;
        fastcgi_index               index.php;
        include                     fastcgi_params;
    }
}


# catches https traffic to these other domains and sends them to non-https
# user will see ssl warning from a.ntivir.us before being redirected however
# this is not ideal, but about all we can do I think
server {
    listen                          443 ssl;
    server_name                     domain2.com domain3.com;
    return                          301 http://$host;
}

我的第一次尝试是简单地复制 domain3 的服务器块,并用 domain4.com 替换它。尝试重新启动/重新加载 nginx 时出现此错误:

nginx: [emerg] 无法构建 server_names_hash,你应该增加 server_names_hash_bucket_size: 32

所以,我将所有服务器块包装在 http {} 指令中,并在 http 级别定义 server_names_hash_bucket_size 64 并且 nginx 仍然无法加载,说这里不允许使用 http 指令。从 nginx 文档中,它说它需要成为所有服务器块的父级,所以我不明白我需要在哪里增加大小。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    我想您已经在 /etc/nginx/nginx.conf (用于 debian/ubuntu)中有 http {} 部分,不需要创建另一个(包装器)。

    尝试在那里放置散列命令(server_names_hash_max_size 或 server_names_hash_bucket_size)。

    【讨论】:

    • 我的原始帖子包含我的默认配置(我是 nginx 新手,所以我还没有分离出任何东西)。
    • 这个conf文件的完整路径是什么,操作系统是什么?
    • Debian 7, /etc/nginx/sites-avaialable/default... 这就是我的问题。我知道现在发生了什么。
    • 这就是我所说的,阅读我的第一篇文章 :) 你有基本的 nginx conf /etc/nginx/nginx.conf 并且你有 /etc/nginx/sites-avaialable/default 用于服务器定义,从那里删除 http {} 包装器并将哈希命令放在基本配置的 http{} 部分中。
    • 是的,你是 100% 正确的,我今天学到了一些东西。谢谢!已接受答案。
    猜你喜欢
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2020-10-17
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多