Nginx永久重定向(NON-WWW到WWW)无法正常工作

时间:2015-08-21 19:18:18

标签: php redirect drupal nginx permanent

嗨,大家好,我希望我的所有非WWW网址请求永久移动,并重写为WWW,我也试图按照现有的解决方案 Nginx no-www to www and www to no-www 但它仍然不适合我。

e.g。我希望example.com或example.com/*重写为www.example.com或www.example.com / *

我正在运行带有nginx和memcache的PHP-FPM

下面是我的配置

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

1 个答案:

答案 0 :(得分:1)

我终于解决了我的问题。我检查了我的nginx.conf,它指向/ etc / nginx / sites-enabled和/etc/nginx/conf.d

我的nginx.conf-&gt;

 ##
        # 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/