为什么nginx重定向所有子域到非www,我只是设置重定向www到非www


Why nginx redirect all subdomain to non-www, I just setting redirect www to non-www

操作系统:Ubuntu 14.04 LTS

Nginx version: 1.6.0

我想重定向www到非www,但下面的配置重定向所有子域到非www。

例如,我访问test.mydomain.com/index.php也重定向到mydomain.com/index.php

访问ip_address/index.php也重定向到mydomain.com/index.php,测试FF 26和Chrome 34,但IE11不重定向。

http {
  gzip                on;
  include             mime.types;
  sendfile            on;
  default_type        application/octet-stream;
  keepalive_timeout   65;
  server {
    listen       80 default_server;
    server_name  mydomain.com;
    root         /var/www;
    location ~* '.php$ {
      fastcgi_index   index.php;
      fastcgi_pass    unix:/var/run/php-fpm.sock;
      include         fastcgi_params;
      fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
      fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
    location / {
      index        index.html index.htm index.php;
    }
    error_page  404              /404.html;
    error_page  500 502 503 504  /50x.html;
  }
  server {
    server_name  www.mydomain.com;
    listen 80;
    return 301 http://mydomain.com$request_uri;
  }
}

您的重定向在此子句中定义

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

删除回车301 http://mydomain.com$request_uri;这应该会删除重定向。

如果这是服务器上唯一的站点,你可以删除整个服务器块,因为PHP处理的根和位置是在上面的server{}块中定义的。