繁体   English   中英

Nginx永久重定向(非WWW到WWW)不起作用

[英]Nginx Permanent Redirect (NON-WWW to WWW) not working

嗨,大家好,我希望所有的非WW网址请求都永久移动并重写为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

我终于解决了我的问题。 我检查了我的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打电话了,我得到了以下正确的答复。

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM