ssl - 在 Nginx 中使用 SSL 避免登陆页面重定向

标签 ssl nginx centos7

我正在为我的服务器配置使用 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。这是我在服务器 block 中的完整配置:

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;
    #}
}

有什么建议吗?

最佳答案

将 :80 block 中的重定向更改为最终重定向,以避免不必要的重定向到 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 block 中。使用您的示例,

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;
}

关于ssl - 在 Nginx 中使用 SSL 避免登陆页面重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754099/

相关文章:

java - SSL 异常 : javax.net.ssl.SSLHandshakeException:收到致命警报:certificate_unknown

laravel - link_to 从容器中托管的站点中剥离端口

http - nginx 和代理 WebSockets

nginx - .net 核心 nginx 托管套接字不允许 http post

PHP-ODBC 驱动程序安装问题

apache - 如何为 easyapache 4 安装 mod_cloudflare

c# - 让 IE6、7、8 通过 ssl 从我们的站点可靠地下载 office 文档

.htaccess - 强制 WWW 使用 HTTPS

node.js - grpc 错误 SSL23_GET_SERVER_HELLO

python - 在 centOS 7 中安装 pip