server push

来源:7-6 HTTP 2的性能提升

jy_li

2021-11-02

老师您好,有几个问题想请教一下

  1. 关于server push您说是服务器提前推送给了客户端,客户端需要用的时候直接从内存里面获取,我想问下服务器是在什么阶段推送给客户端的呢,是客户端获取到第一个文件的时候吗?
  2. 如果设置了所有的图片不缓存但某一张图片提前推送,那这张图片是会从内存里面获取还是服务器返回?
  3. 如图,request sent和download中间有很大一块空白时间,这段时间是在做什么,不能直接下载吗?
    图片描述
  4. 下面是我针对我项目的尝试,但重启服务后刷新网页还是会从服务器获取,不知道问题在哪。(windows平台)

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
	keepalive_requests 20;

    gzip  on;
	
	gzip_min_length 1k;
	
	gzip_comp_level 6;
	
	gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/xml text/javascript application/json;
	
	gzip_static on;
	
	gzip_vary on;
	
	gzip_buffers 4 16k;
	
	gzip_http_version 1.1;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   C:/Users/JFJ/rebuild;
            index  index.html index.htm;
			if ($request_filename ~* .*\.(?:htm|html)$)
			{
			  add_header Cache-Control "no-cache, must-revalidate";
			  add_header "Progma" "no-cache";
			  add_header "Expires" "0";
			}
			if ($request_filename ~* .*\.(?:js|css)$)
			{
			  expires  7d;
			}
			if ($request_filename ~* .*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$)
			{
			  expires  7d;
			}
        }

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
        listen       443 ssl http2;
        server_name  localhost;

        ssl_certificate      C:/Users/JFJ/rebuild/ssl/server.crt;
        ssl_certificate_key  C:/Users/JFJ/rebuild/ssl/server.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   C:/Users/JFJ/rebuild;
            index  index.html index.htm;
			http2_push C:/Users/JFJ/rebuild/index.html;
        }
    }

}

图片描述图片描述

写回答

1回答

Mr_Max

2021-11-03

同学你好!

  1. HTTP2其实打破了传统的请求响应的流程,它允许一个请求多个响应,而且可以是服务器主动发起的资源推送。它的主要用途就是提前推送后面要用到的资源,不用等到客户端到时请求再给。像课里举的例子,访问主页(/)的请求还会返回从服务器端配置要提前推送的资源。其他请求路径也可以配置推送,同样的道理。

  2. 如果是提前推送的,那到使用时就会从缓存里读取。

  3. 空白是因为还没有到这些资源真正要被使用的时候。一旦要使用就会从缓存里读取出来。

  4. 推送的资源路径应该是它本身的请求uri,而不是文件的location,你和课里的例子对比一下。所以你看下你原来请求rebuild/index.html时的uri是什么?不过我觉得你这里理解的并不太对,通常你是不会提前推送一个html页面的。
    http2_push C:/Users/JFJ/rebuild/index.html;

0
1
jy_li
非常感谢!
2021-11-04
共1条回复

前端性能优化企业级解决方案 6大角度+大厂视野

只要项目还在线,性能优化永远是时刻要关注的问题

1109 学习 · 226 问题

查看课程