NGINX url在digitalocean上重写

时间:2013-12-16 17:19:51

标签: nginx

我在nginx web服务器上从example.com重写到www.example.com时遇到问题。我使用新的主机digitalocean.com并仍然在努力...

我会对每一个意见感到满意。

有我的代码:

server {
  server_name example.com;
  return 301 http://www.example.com$request_uri;
}

server {
  server_name www.example.com;

  root /usr/share/nginx/www;
  index index.php index.html index.htm;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/www;
  }

  location / {
    try_files $uri $uri/ /index.html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;        
  }
}

我尝试重启nginx服务器时遇到错误:

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

2 个答案:

答案 0 :(得分:2)

服务器名称可能对于默认值来说太长了。

通过在http:

下面添加以下内容来修改/etc/nginx/nginx.conf文件
increase server_names_hash_bucket_size: 64

保存此值并使用-t

进行测试
nginx -t

另外,请注意sites-available文件夹中可能导致问题的任何已保存的默认配置。

答案 1 :(得分:1)

尝试以下方法:

server {
  server_name example.com;
  return 301 http://www.example.com$request_uri;
}

server {
  server_name www.example.com;

  root /usr/share/nginx/www;
  index index.php index.html index.htm;

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/www;
  }

  location / {
    try_files $uri $uri/ /index.html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;        
  }
}