nginx 静态资源WEB服务


9303390

1、静态资源类型

非服务器动态运行生成的文件

类型种类

浏览器端渲染     HTML、CSS、JS

图片            JPEG、GIF、PNG

视频            FLV、MPEG

文件            TXT、等任意下载文件


2、静态资源服务场景-CDN

image


配置语法

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、配置语法-压缩

10255015

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调试时,注意关闭缓存

ca746241-0681-4a50-aef8-59fcfcc529ef

压缩后

dbb664bb-06a5-48ec-957d-942fe732695d

对文本的压缩更大

71689936-587d-4897-b16c-240bce0f52aa

[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;
    }
posted @ 2019-06-23 15:00  前海渔文乐  阅读(699)  评论(0编辑  收藏  举报