1

嗨,伙计们,我希望我的所有 NON-WWW url 请求永久移动并重写为 WWW,我已经尝试在这些以及 Nginx no-www 到 www 和 www 到 no-www上遵循现有的解决方案, 但它仍然没有工作我。

例如,我希望 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
4

1 回答 1

1

我终于解决了我的问题。我检查了我的 nginx.conf,它指向 /etc/nginx/sites-enabled 和 /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 进行了 curl 调用,得到了以下正确响应。

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/
于 2015-08-27T08:30:43.930 回答