Nginx非www重定向到www似乎没有工作

时间:2018-01-15 09:18:59

标签: redirect nginx digital-ocean http-status-code-301 no-www

我在sites-available / default

中有以下配置

我添加了服务器块,将我的非www请求重定向到www,如数字海洋guide中所述。但是,当我从浏览器请求页面时,www流量似乎重定向到非www。但是从终端做一个卷曲-I请求导致www和非www的200 OK。为什么是这样?

 # HTTP — redirect all traffic to HTTPS
    server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
        return 301 https://$host$request_uri;
    }

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

    # HTTPS — proxy all requests to the Node app
    server {
        # Enable HTTP/2
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name www.example.com;
            ssl_dhparam /etc/ssl/certs/dhparam.pem;
    # Include the SSL configuration from cipherli.st
        #include snippets/ssl-params.conf;

        location / {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:3000/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
        }
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot



    }

卷曲请求:

curl -I https://example.com

    HTTP/1.1 200 OK
    Server: nginx/1.10.3 (Ubuntu)
    Date: Mon, 15 Jan 2018 09:14:10 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 23690
    Connection: keep-alive
    X-Powered-By: Express
    ETag: W/"5c8a-deTNNXHp2f1a8yOhbxbzVmtCHo4"
    Vary: Accept-Encoding



 curl -I https://www.example.com

    HTTP/1.1 200 OK
    Server: nginx/1.10.3 (Ubuntu)
    Date: Mon, 15 Jan 2018 09:15:07 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 23690
    Connection: keep-alive
    X-Powered-By: Express
    ETag: W/"5c8a-deTNNXHp2f1a8yOhbxbzVmtCHo4"
    Vary: Accept-Encoding

0 个答案:

没有答案