在Nginx中使用SSL避免登录页重定向
我正在使用SSL进行服务器配置。之后,当用户访问我们的网站时会有点困难,几乎需要大量的时间来访问。并尝试PageSpeed Insight,结果建议我避免登录页面重定向在Nginx中使用SSL避免登录页重定向,ssl,nginx,centos7,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。
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;
#}
}
有什么建议吗?将:80块中的重定向更改为最终重定向,以避免不必要的重定向 另外,我建议将同一组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;
}
将:80块中的重定向更改为最终重定向,以避免不必要的重定向 另外,我建议将同一组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;
}
试试这一个,我不知道为什么您添加了两个不同的服务器连接,通过www和不使用www连接SSL。它可以在一个语句中写入 为什么要在SSL中使用301重定向,不知何故,您已经强制使用或将www重定向到https 我还添加了重写规则以重定向到https,无论是否有www连接&删除了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连接SSL。它可以在一个语句中写入 为什么要在SSL中使用301重定向,不知何故,您已经强制使用或将www重定向到https 我还添加了重写规则以重定向到https,无论是否有www连接&删除了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中定位我们的域。嘿,谢谢你的代码,但如果我使用你的建议,我有一个老问题,因为它直接指向非www域,在这种情况下,我们需要在www中定位我们的域。