Nginx深入了解-进阶(一)

Nginx配置与优化
本文介绍了Nginx作为高性能的HTTP服务器,在处理静态资源、CDN配置、浏览器缓存策略、跨域资源共享(CORS)及防盗链方面的具体配置方法。通过实例展示了如何启用sendfile、TCP优化、GZIP压缩等功能提升性能。
Nginx用来作为静态资源web服务;CDN、浏览器缓存、跨域、防盗链等。

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

类型种类
浏览器端渲染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压缩模块

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

配置实例:

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
3Last-Modified头信息校验Last-Modified

配置实例:

server {
    ...
    expires 24h;
}
- 跨域访问

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

配置语法:

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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值