Nginx深刻了解-进阶(一)

Nginx用来做为静态资源web服务;CDN、浏览器缓存、跨域、防盗链等。

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

类型 种类
浏览器端渲染 HTML、CSS、JS
图片 JPG、GIF、JPEG、PNG
视频 FLV、MPEG
文件 TXT等

- 静态资源服务CDN.

  • 配置语法-文件读取
Syntax:sendfile on|off
Default:sendfile off
Context:http、server、location、if on location
  • 配置语法-tcp_nopush(在sendfile开启的状况下,提升网络包的传输效率)
Syntax:tcp_nopush on|off
Default:tcp_nopush off
Context:http、server、location
  • 配置语法-tcp_nodelay(在keepalive链接下,提升网络包的传输实时性)
Syntax:tcp_nodelay on|off
Default:tcp_nodelay off
Context:http、server、location
  • 配置语法-压缩(压缩传输文件)
Syntax:gzip on|off
Default:gzip off
Context:http、server、location、if on location

Syntax:gzip_comp_level
Default:gzip_comp_lvel 1;
Context:http、server、location

syntax:gzip_http_version 1.0|1.1
Default:gzip_http_version 1.1
Context:http、server、location

扩展Nginx压缩模块php

http_gzip_static_module-预读gzip功能
http_gunzip_module-应用支持gunzip压缩方式css

配置实例:html

server {
    ...
    sendfile on;
    
    location ~ .*\.(jpg|png|gif)$ {
        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;
    }
    
    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;
    }
    
    location ~ ^/download {
        gzip on;
        tcp_nopush on;
        gzip_static on;
        ...
    }
}

- 浏览器缓存

优先级 机制 参数
1 校验是否过时 Expires、Cache-Control(max-age)
2 协议中的Etag头信息校验 Etag
3 Last-Modified头信息校验 Last-Modified

配置实例:java

server {
    ...
    expires 24h;
}

- 跨域访问

通俗地讲,跨域访问就是在访问某一个网址(www.mantis.me) 的同时访问另外一个网址(www.xxx.com) ,对于浏览器来讲这是不容许的,由于容易出现CRSF攻击,浏览器会阻止这种行为。node

配置语法:web

Syntax:add_header name value [always]
Default:--
Context:http、server、location、if in location

浏览器会检查Access-Control-Allow-Origin对应的值,提示错误:跨域

XMLHttpRequest cannot load http://www.mantis.me/. The 'Access-Control-Allow-Origin' header has a value 'http://www.xxx.com' that is not equal to the supplied origin. Origin 'http://www.mantis.me/' is therefore not allowed access.

配置实例:浏览器

server {
    ...
    location ~ .*\.(html|htm)$ {
        ...
        add_header Access-Control-Allow-Origin www.xxx.com; // 容许全部能够设置为*(不建议,容易被跨域攻击)
        add_header Access-Control-Allow-Methods POST,GET,OPTIONS,PUT,DELETE;
    }
}

- 防盗链

防止资源被盗用,其余网站盗用咱们网站的资源连接信息致使资源信息被盗用同时还有可能致使咱们服务器的压力增大。缓存

  • http_refer防盗链配置
Syntax:valid_referers none|blocked|server_names|string...;
Default:---
Context:server、location

配置实例:

server {
    location ~ .*\.(jpg|jpeg|png|gif)$ {
        ...
        valid_referers blocked www.mantis.me ~/baidu\./;// 这里只容许refer头为www.mantis.me的地址和baidu搜索过来的,能够便于seo优化
        if ($invalid_referer) {
            return 403;
        }
    }
}
相关文章
相关标签/搜索