簡體   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