gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:17 25 4
gpt4 key购买 nike

我正在为我的服务器配置使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com