【问题标题】:Avoiding Landing Page Redirects with SSL in Nginx在 Nginx 中使用 SSL 避免登陆页面重定向
【发布时间】:2016-08-05 02:10:30
【问题描述】:

我正在为我的服务器配置使用 SSL。之后,当用户访问我们的网站时,它会有点困难,几乎需要大量时间才能访问。并尝试 PageSpeed Insight,结果建议我避免登录页面重定向。

Avoid landing page redirects for the following chain of redirected URLs.

http://example.com/
https://example.com/
https://www.example.com/

这对我来说是个新问题。

我们使用最新的 Nginx。这是我在服务器块中的完整配置:

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

server {
        listen 443 ssl;
        server_name example.com;
        ssl_certificate /etc/nginx/ssl/cert_chain.crt;
        ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
        return 301 $scheme://www.example.com$request_uri;
}

server {
       listen 443 ssl spdy;
       add_header Strict-Transport-Security "max-age=31536000; includeSubdomain$

       server_name www.example.com
       root   /home/domain;
       index  index.html index.php index.htm;

       ssl on;
       ssl_certificate /etc/nginx/ssl/cert_chain.crt;
       ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
       ssl_prefer_server_ciphers on;
       ssl_dhparam /etc/ssl/certs/dhparam.pem;
       ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GC$
       ssl_session_timeout 1d;
       ssl_session_cache shared:SSL:50m;
       ssl_stapling on;
       ssl_stapling_verify on;

       location / {
                 try_files $uri $uri/ /index.php?q=$request_uri;
        }



  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /home/domain;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME/home/domain$fastcgi_script_name;
        include        fastcgi_params;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

有什么建议吗?

【问题讨论】:

  • 你能发布完整的 conf 文件吗?
  • 我已经更新了我的配置文件,谢谢

标签: ssl nginx centos7


【解决方案1】:

将 :80 块中的重定向更改为最终重定向,以避免不必要的重定向到 https://example.com

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

另外,我建议将相同的 SSL 相关指令集添加到 server_name example.com 块中。用你的例子,

server {
        listen 443 ssl;
        server_name example.com;
        ssl_certificate /etc/nginx/ssl/cert_chain.crt;
        ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
        return 301 $scheme://www.example.com$request_uri;
}

应该是

server {
        --listen 443 ssl;
        ++listen 443 ssl spdy;
        server_name example.com;
        ssl_certificate /etc/nginx/ssl/cert_chain.crt;
        ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
        ++add_header Strict-Transport-Security "max-age=31536000; includeSubdomain$
        ++ssl_prefer_server_ciphers on;
        ++ssl_dhparam /etc/ssl/certs/dhparam.pem;
        ++ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GC$
        ++ssl_session_timeout 1d;
        ++ssl_session_cache shared:SSL:50m;
        ++ssl_stapling on;
        ++ssl_stapling_verify on;
        return 301 $scheme://www.example.com$request_uri;
}

【讨论】:

    【解决方案2】:

    试试这个,我不知道你为什么添加两个不同的连接到服务器以通过 www 和没有 www 连接 SSL。它可以写在一个语句中。

    为什么你在 SSL 中使用 301 重定向,不知何故你已经强制使用或 www 重定向到 https。

    我还添加了重写规则,以在有或没有 www 连接的情况下重定向到 https 并删除了 301 返回规则。

    这是最终代码,如果不起作用,请尝试一下,让我知道我为您准备了另一个代码。

    server {
       listen           80;
       server_name      example.com www.example.com;
       rewrite        ^ https://$server_name$request_uri? permanent;       
    }
    
    server {
       listen 443 ssl spdy;
       add_header Strict-Transport-Security "max-age=31536000; includeSubdomain$
       server_name example.com www.example.com;
       root   /home/domain;
       index  index.html index.php index.htm;
       ssl on;
       ssl_certificate /etc/nginx/ssl/cert_chain.crt;
       ssl_certificate_key /etc/nginx/ssl/*.example.com.key;
       ssl_prefer_server_ciphers on;
       ssl_dhparam /etc/ssl/certs/dhparam.pem;
       ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GC$
       ssl_session_timeout 1d;
       ssl_session_cache shared:SSL:50m;
       ssl_stapling on;
       ssl_stapling_verify on;
    
       location / {
                 try_files $uri $uri/ /index.php?q=$request_uri;
        }
    
       location ~ \.php$ {
        root           /home/domain;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME/home/domain$fastcgi_script_name;
        include        fastcgi_params;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
      }
    }
    

    【讨论】:

    • 嘿,感谢您的代码,但如果我使用您的建议,我似乎有老问题,因为它直接指向非 www 域,在这种情况下,我们需要在 www 中定位我们的域。
    猜你喜欢
    • 2014-12-02
    • 2019-03-05
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多