nginx 静态资源WEB服务
1、静态资源类型
非服务器动态运行生成的文件
类型种类
浏览器端渲染 HTML、CSS、JS
图片 JPEG、GIF、PNG
视频 FLV、MPEG
文件 TXT、等任意下载文件
2、静态资源服务场景-CDN
配置语法
1、文件读取 Syntax:sendfile on l off; Default:sendfile off; Context:http,server,location,if in location 引读:一with-file-aid异步文件读取 2、tcp_nopush Syntax: tcp_nopush on | off; Default: tcp_nopush off; Context: http,server,location 作用:sendfile开启的情况下,提高网络包的传输效率 3、tcp_nodelay Syntax: tcp_nodelay on|off; Default: tcp_nodelay on; Context: http,server,location 作用:keepalive连接下,提高网络包的传输实时性
3、配置语法-压缩
Syntax:gzip on|off; Default:gzip off; Context:http,server,location,if in location 作用:压缩传输 Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http,server,location Syntax: gzip_http_version 1.0|1.1; Default: gzip_http_version 1.1; Context: http, server, location 扩展Nginx压缩模块 http_gzip_static_module -预读gzip功能 http_gunzip_module -应用支持gunzip的压缩方式
测试
server_static.conf
server { listen 80; server_name 10.0.0.6 www.web01.com; sendfile on; access_log /var/log/nginx/log/static_access.log main; location ~ .*\.(jpg|gif|png)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/images; } location ~ .*\.(txt|xml)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 1; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /opt/app/code/doc; } location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin *; #访问限制 add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; #允许访问的方法 #expires 24h; root /opt/app/code; } location ~ ^/download { gzip_static on; tcp_nopush on; root /opt/app/code; } }
压缩前
F12调试时,注意关闭缓存
压缩后
对文本的压缩更大
[root@web-01 doc]# ls -lh
total 176K
-rw-r--r--. 1 root root 175K Aug 8 2017 access.txt
设置缓存
location ~ .*\.(htm|html)$ { expires 24h; root /opt/app/code; }
权限控制
location ~ .*\.(jpg|gif|png)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; valid_referers none blocked 116.62.103.228 jeson.imoocc.com ~wei\.png; if ($invalid_referer) { return 403; } root /opt/app/code/images; }