【问题标题】:How to redirect www to non-www and http to https using nginx and cloudflare?如何使用 nginx 和 cloudflare 将 www 重定向到非 www 和 http 到 https?
【发布时间】:2015-08-04 20:55:56
【问题描述】:

我正在使用以下内容将所有用户重定向到httpsnon-www

server {
    listen 80;
    listen [::]:80;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    access_log /var/log/nginx/example.com.access.log rt_cache;
    error_log /var/log/nginx/example.com.error.log;

    root "/usr/share/nginx/app/public";
    index index.php index.htm index.html;

    charset utf-8;

    ssl on;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    client_max_body_size 100m;

    include hhvm.conf;

    location ~ /\.ht {
        deny all;
    }
}

注意:我也在使用 CloudFlare。

当我访问example.com 时,我被重定向到https://example.com。太好了。

但是www.example.com 重定向到https://www.example.com 并且网站无法加载。

访问https://example.com 工作正常。

这是服务器配置问题还是 CloudFlare 问题?怎么解决?

【问题讨论】:

标签: ssl nginx seo server cloudflare


【解决方案1】:

问题是我需要添加一个 DNS A 记录以允许 www 指向我的服务器的 IP 地址。然后,nginx 可以毫无问题地重定向。

【讨论】:

    猜你喜欢
    • 2017-09-03
    • 2018-09-26
    • 2018-02-02
    • 2017-03-10
    • 2014-05-08
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多