2014-05-16 19 views
0

操作系统:Ubuntu的LTS 14.04为什么nginx的重定向所有子域名不带www的,我只是设置重定向到WWW非www

Nginx的版本: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; 
    } 
} 
+0

对不起我的英文。我只想将www重定向到非www。其他子域不重定向。 – consatan

+0

尝试将'return 301 ...'放入'if($ http_host〜*^www \ .mydomain \ .com $){'[here]'}'。我在配置中看不到任何错误。 – Deadooshka

+0

@Deadooshka我很抱歉!我测试了我的windows 8.1笔记本电脑,在%windir%\ system32 \ drivers \ etc \ hosts文件中设置了本地dns,但失败了。我尝试在Ubuntu虚拟机上使用/ etc/hosts本地dns使用curl,这是工作。我在我的Asus RT-N16路由器(使用AsusWRT Merlin固件)上设置本地DNS,现在都可以使用。我想添加评论,但我不能昨天登录,thx中国GWF! – consatan

回答

0

您重定向在本节

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

删除行返回301 http://mydomain.com $ REQUEST_URI的定义;并应该删除重定向。

如果这是服务器上唯一的站点,您可以删除整个服务器块,因为php处理的根目录和位置是在此目录之上的服务器块中定义的。