配置緩存nginx.conf
#user nobody;
worker_processes 1;
#error_log /opt/proj/nginx/logs/error.log;
#error_log /opt/proj/nginx/logs/error.log notice;
#error_log /opt/proj/nginx/logs/error.log info;
pid /opt/nginx/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;
#gzip on;
#以下是開啟gzip壓縮緩存start
gzip on;
#該指令用于開啟或關(guān)閉gzip模塊(on/off)
gzip_buffers 16 8k;
#設(shè)置系統(tǒng)獲取幾個單位的緩存用于存儲gzip的壓縮結(jié)果數(shù)據(jù)流。16 8k代表以8k為單位住闯,安裝原始數(shù)據(jù)大小以8k為單位的16倍申請內(nèi)存
gzip_comp_level 6;
#gzip壓縮比赋除,數(shù)值范圍是1-9,1壓縮比最小但處理速度最快,9壓縮比最大但處理速度最慢
gzip_http_version 1.1;
#識別http的協(xié)議版本
gzip_min_length 256;
#設(shè)置允許壓縮的頁面最小字節(jié)數(shù)好芭,頁面字節(jié)數(shù)從header頭得content-length中進行獲取。默認值是0昏苏,不管頁面多大都壓縮弦叶。這里我設(shè)置了為256
gzip_proxied any;
#這里設(shè)置無論header頭是怎么樣俊犯,都是無條件啟用壓縮
gzip_vary on;
#在http header中添加Vary: Accept-Encoding ,給代理服務(wù)器用的
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
#進行壓縮的文件類型,這里特別添加了對字體的文件類型
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#禁用IE 6 gzip
#開啟gzip壓縮緩存end
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
}
location /distc/ {
#所有靜態(tài)文件直接讀取硬盤
root /opt/proj/;
autoindex on;
#緩存30天
expires 30d;
}
#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;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
驗證:
測試 移動網(wǎng)絡(luò)熱點測試
-
沒加入nginx
8s以上的文件
-
加入nginx并且開啟nginx的gzip緩存
pdfmake.min.js時間:18.03s-->10.83,少了8s左右
vfs_fonts.js時間: 24.53s--> 11.82s ,少 13s左右
daterangepicker.js時間: 21.66s—> 3.77s ,少了17s