在Nginx中避免使用SSL重定向着陆页

时间:2016-08-03 21:41:01

标签: 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。这是我在服务器块中的完整配置:

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

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

将您的: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;
}

答案 1 :(得分:0)

尝试这个,我不知道为什么你添加了两个不同的服务器连接,通过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;
  }
}