2015-08-21 56 views
1

嗨,大家好我想我所有的非www URL請求永久地住,也改寫爲WWW和我曾試過把這些遵循現有的解決方案,以及 Nginx no-www to www and www to no-www 但仍然不適合我。Nginx的永久重定向(NON-WWW到WWW)不工作

例如我想example.com或example.com/*改寫到www.example.com或www.example.com/*

我運行PHP-FPM nginx的和memcache

下面

是我的配置

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

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

     root /srv/www/abc; 
     index index.php index.html index.htm; 
....... 
} 

下面是我的捲曲響應

neel:~ nilesh$ curl -I http://example.com 
HTTP/1.1 200 OK 
Server: nginx/1.4.6 (Ubuntu) 
Date: Fri, 21 Aug 2015 19:00:54 GMT 
Content-Type: text/html; charset=utf-8 
Connection: keep-alive 
Vary: Accept-Encoding 
X-Powered-By: PHP/5.5.9-1ubuntu4.11 
X-Drupal-Cache: HIT 
Etag: "1440178291-0" 
Content-Language: en 
X-Generator: Drupal 7 (http://drupal.org) 
Link: <http://example.com/>; rel="canonical",<http://example.com/>; rel="shortlink" 
Cache-Control: public, max-age=1800 
Last-Modified: Fri, 21 Aug 2015 17:31:31 +0000 
Expires: Sun, 19 Nov 1978 05:00:00 GMT 
Vary: Cookie 
Vary: Accept-Encoding 
+0

您是否嘗試給'-L'或'--location'捲曲?如果你不這樣做,curl不會跟着重定向。 –

+0

我試過,但-L只打印出html。那麼我-I返回標題,如果配置可以工作,它將永久移動。 – Neel

+0

那麼這是你的本地環境還是生活?只有在我看來,如果它是現場DNS可能會導致類似這樣的事情。 –

回答

1

我終於解決了我的問題。我檢查了我的nginx.conf並且它指向到/ etc/nginx的/啓用站點和/etc/nginx/conf.d

我nginx.conf->

## 
     # Virtual Host Configs 
     ## 
     include /etc/nginx/conf.d/*.conf; 
     include /etc/nginx/sites-enabled/*; 

我終於複製我的文件從站點 - 可用文件夾站點 - 啓用。事實上我保持同步。

下面

是我在我的服務器標籤

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

server { 
     listen 80; 
     server_name www.example.com; 
     #listen [::]:80 default_server ipv6only=on; 
     root /srv/www/example; 
#rest config goes below 

下使用的代碼.......

現在我所有的非www流量301永久移動和重寫到www上述碼。

我對非www進行了捲曲調用,並且得到了以下正確的響應。

neel:~ nilesh$ curl -I http://example.com 
HTTP/1.1 301 Moved Permanently 
Server: nginx/1.4.6 (Ubuntu) 
Date: Thu, 27 Aug 2015 08:39:38 GMT 
Content-Type: text/html 
Content-Length: 193 
Connection: keep-alive 
Location: http://www.example.com/