本文原作者阮一峰,作者博客:ruanyifeng.com。
01 02 03 04 05 06 07 08 09 10 | <!DOCTYPE html> < html > < head > < link rel = "stylesheet" href = "style.css" > </ head > < body > < h1 >hello world</ h1 > < img src = "example.png" > </ body > </ html > |
GET /index.html HTTP/1.1
GET /style.css HTTP/1.1
GET /example.png HTTP/1.1
1 2 | < link rel = "preload" href = "/styles.css" as = "style" > < link rel = "preload" href = "/example.png" as = "image" > |
$ cd nginx-docker-demo
$ rm html/index.html
1 2 3 | h 1 { color : red ; } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | server { listen 443 ssl http2; server_name localhost; ssl on; ssl_certificate /etc/nginx/certs/example .crt; ssl_certificate_key /etc/nginx/certs/example .key; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/share/nginx/html ; index index.html index.htm; http2_push /style .css; http2_push /example .png; } } |
1 2 3 4 5 6 7 8 9 | $ docker container run \ -- rm \ --name mynginx \ --volume "$PWD/html" : /usr/share/nginx/html \ --volume "$PWD/conf" : /etc/nginx \ -p 127.0.0.2:8080:80 \ -p 127.0.0.2:8081:443 \ -d \ nginx |
$ docker container stop mynginx
1 2 3 4 | <FilesMatch "\index.html$" > Header add Link "</styles.css>; rel=preload; as=style" Header add Link "</example.png>; rel=preload; as=image" < /FilesMatch > |
Link: </styles.css>; rel=preload; as=style
Link: </styles.css>; rel=preload; as=style, </example.png>; rel=preload; as=image
01 02 03 04 05 06 07 08 09 10 11 12 | server { listen 443 ssl http2; # ... root /var/www/html ; location = / { proxy_pass http: //upstream ; http2_push_preload on; } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | server { listen 443 ssl http2 default_server; ssl_certificate ssl /certificate .pem; ssl_certificate_key ssl /key .pem; root /var/www/html ; http2_push_preload on; location = /demo .html { add_header Set-Cookie "session=1" ; add_header Link $resources; } } map $http_cookie $resources { "~*session=1" "" ; default "</style.css>; as=style; rel=preload" ; } |
联系客服