尝试www到非www时,nginx失败

时间:2013-09-27 06:23:43

标签: nginx

我正在部署一个带有jar文件的网站。这是我设置的第一台服务器,所以我在这里做的事情有点迷失。我在我的nginx.config中有这个:

server{
     listen          80;
     server_name     www.mySite.com;
     return          301 $scheme://mySite.com$request_uri;
}

server {
     listen       80;
     server_name  mySite.com;

      location / {
           proxy_pass http://localhost:3000 ;
      }

      .....

我根据文档设置了CNAME。

我已经尝试了几种资源来理顺这一点:

http://nginx.org/en/docs/http/converting_rewrite_rules.html

Nginx no-www to www and www to no-www

http://rtcamp.com/wordpress-nginx/tutorials/nginx/www-non-www-redirection/

并继续得到同样的错误:

$ systemctl status nginx.service
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 Fri 2013-09-27 06:22:30 UTC; 11s ago
  Process: 4492 ExecStop=/usr/bin/nginx -g pid /run/nginx.pid; -s quit (code=exited, status=1/FAILURE)
  Process: 3705 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 4495 ExecStartPre=/usr/bin/nginx -t -q -g pid /run/nginx.pid; daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 3706 (code=exited, status=0/SUCCESS)
   CGroup: name=systemd:/system/nginx.service

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。事实证明我不需要添加第二台服务器:

server {
     listen       80;
     server_name  mySite.com www.mySite.com;

      location / {
           proxy_pass http://localhost:3000 ;
      }

我不确定这是否是绝对正确的答案,但我确实从nginx.com尝试了这个例子并且它不起作用:

server {
    listen       80;
    server_name  example.com www.example.com;
    ...
}

server {
    listen       80 default_server;
    server_name  _;
    return       301 http://example.com$request_uri;
}