Nginx静态资源
Nginx静态资源Web服务
1.静态资源:浏览器渲染,HTML,CSS,JS 图片:JPEG,GIF,PNG 视频:FLV,MPEG, 文件:TXT,其他任意文件
2.静态资源服务场景-CDN
3.配置语法-文件读取 sendfile, 需要在sendfile开启的环境下开启tcp_nopush(提高网络的传输效率)
tcp_nodelay 提高网络的传输实时性,gzip 压缩 , gzip_comp_level level;压缩比例, http_gzip_static_module 预读gzip功能 http_gunzip_module 应用支持gunzip的压缩方式
4.配置参数语法:
server {
listen 80;
server_name 16.14.28.106 test.tt.com;
sendfile on;
#charset koi8-r;
access_log /var/log/nginx/log/static.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/codes/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/apps/codes/doc;
}
location ~ ^/download {
gzip_static on;
tcp_nopush on;
root /opt/apps/codes;
}
5.浏览器缓存:HTTP协议定义的缓存机制(Expires; Cache-control)
ETag, Last-Modified 跟服务端响应查询缓存,看服务端缓存是否有更新,有缓存服务端直接响应304状态码
6.add_header name value [always]; Access-Control-Allow-origin 配置可以跨域访问
location ~ .*\.(htm|html)$ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
#expires 24h;
root /opt/apps/codes;
}