1.每日复盘与今日内容
1.1复盘
- nginx四层转发
- rewrite tag:last和break
- redirect、permanent🍟🍟🍟🍟🍟
- Nginx内置参数
- 动静分离🍟🍟🍟🍟🍟
1.2今日内容
- Nginx性能优化
- 压力测试工具
- 系统性能优化
- 代理服务优化
- 静态资源优化🍟🍟🍟🍟🍟
- 文件高效传输
- 静态资源压缩🍟🍟🍟🍟🍟
- 防止资源盗链🍟🍟🍟🍟🍟
- 跨域访问
- CPU亲和
2.Nginx性能优化
1、首先我们需要了解性能优化要考虑哪些方面。
2、然后我们需要了解性能优化必要用到的压力测试工具ab
3、最后我们需要了解系统上有哪些注意和优化的点,以及nginx配置文件
我们再做性能优化工作前,我们重点需要考虑哪些方面,和了解哪些方面
1、首先需要了解我们当前系统的结构和瓶颈,了解当前使用的是什么,运行的是什么业务,都有哪些服务,了解每个服务最大能支撑多少并发。比如nginx作为静态资源服务并发是多少,最高瓶颈在哪里,能支持多少QPS(每秒查询率)的访问请求,那我们怎么得出这组系统结构瓶颈呢,比如top查看系统的CPU负载、内存使用率、总得运行进程等,也可以通过日志去分析请求的情况,当然也可以通过我们前面介绍到的stub_statius模块查看当前的连接情况,也可以对线上的业务进行压力测试(低峰期),去了解当前这套系统能承担多少的请求和并发,已做好相应的评估。这个是我们做性能优化最先考虑的地方。
2、其次我们需要了解业务模式,虽然我们是做性能优化,但每一个性能的优化都是为业务所提供的服务的,我们需要了解每个业务接口的类型,比如:电商网站中的抢购模式,这种情况下面,平时没什么流量,但到了抢购时间流量会突增。
我们还需要了解系统层次化的结构,比如:我们使用nginx做的是代理、还是动静分离、还是后端直接服务用户,那么这个就需要我们对每一层做好相应的梳理。以便更好的服务业务。
3、最后我们需要考虑性能与安全,往往注重了性能,但是忽略了安全。往往过于注重安全,对性能又会产生影响。比如:我们在设计防火墙功能时,检测过于严密,这样就会给性能带来影响。那么如果对于性能完全追求,却不顾服务的安全,这个也会造成很大的隐患,所以需要评估好两者的关系,把握好两者的孰重孰轻。以及整体的相关性,权衡好对应的点。
- 从OSI模型考虑优化方向
名称 | 消耗资源 |
硬件 | 代理(CPU) 静态(磁盘IO) 动态(cpu、内存) |
网络 | 带宽、丢包、延迟 |
系统 | 文件描述符(文件句柄数) |
应用 | 服务与服务保持长连接 http1.1 |
服务 | 静态资源服务优化 |
3.压力测试工具
- ab命令--属于httpd-tools
在系统业务量没有增长之前,我们就要做好相应的准备工作,以防患业务量突增带来的接口压力,所以对于接口压力测试就显得非常重要了,我们首先要评估好系统压力,然后使用工具检测当前系统情况,是否能满足对应压力的需求。
选项 | 作用 |
-n | 要执行的请求数 |
-c | 请求的并发数 |
-k | 是否开启长连接 |
ab -n20000 -c200 http://ab.oldboy.com/
This is ApacheBench, Version 2.3 <$Revision: 1874286 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.oldboy.com (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requestsServer Software: nginx/1.26.1
Server Hostname: www.oldboy.com
Server Port: 80Document Path: /
Document Length: 9 bytesConcurrency Level: 200
Time taken for tests: 1.300 seconds
Complete requests: 20000
Failed requests: 0
Total transferred: 4760000 bytes
HTML transferred: 180000 bytes
Requests per second: 15383.56 [#/sec] (mean)
Time per request: 13.001 [ms] (mean)
Time per request: 0.065 [ms] (mean, across all concurrent requests)
Transfer rate: 3575.48 [Kbytes/sec] receivedConnection Times (ms)min mean[+/-sd] median max
Connect: 0 5 60.4 2 1068
Processing: 2 7 12.8 5 221
Waiting: 0 5 12.9 4 220
Total: 3 12 71.3 7 1228Percentage of the requests served within a certain time (ms)50% 766% 875% 880% 890% 1095% 1198% 1499% 26100% 1228 (longest request)以下是 Apache Benchmark (ab) 测试结果的逐行中文解释:服务器信息
Server Software: nginx/1.26.1服务器软件:Nginx,版本 1.26.1。Server Hostname: www.oldboy.com测试的目标主机名(域名)。Server Port: 80测试使用的端口(HTTP 默认端口 80)。Document Path: /请求的路径(根路径 /)。Document Length: 9 bytes服务器返回的 HTML 内容长度:9 字节。测试参数与统计
Concurrency Level: 200并发请求数:200(模拟 200 个用户同时访问)。Time taken for tests: 1.300 seconds完成所有测试请求的总耗时:1.3 秒。Complete requests: 20000成功的请求总数:2 万次(全部成功)。Failed requests: 0失败的请求数:0(无失败)。Total transferred: 4760000 bytes测试期间传输的总数据量:476 万字节(包括响应头、内容等)。HTML transferred: 180000 bytes仅 HTML 内容的总传输量:18 万字节(2 万次请求 × 9 字节)。性能指标--重要
Requests per second: 15383.56 [#/sec] (mean)每秒处理的请求数(QPS):15383.56 次/秒,反映服务器吞吐量。Time per request: 13.001 [ms] (mean)单个请求的平均耗时(并发模式下):13 毫秒(所有并发请求共同分担时间)。Time per request: 0.065 [ms] (mean, across all concurrent requests)单个请求的实际平均耗时(不考虑并发):0.065 毫秒(总时间 ÷ 总请求数)。Transfer rate: 3575.48 [Kbytes/sec] received数据传输速率:每秒接收 3575.48 KB(网络带宽占用情况)。连接时间分布(单位:毫秒)
Connection Times (ms)各项请求时间的统计(连接、处理、等待等):Connect(建立连接时间):最小值 0ms,均值 5ms,中位数 2ms,最大 1068ms(存在个别慢连接)。Processing(服务器处理时间):均值 7ms,中位数 5ms,最大 221ms。Waiting(客户端等待响应时间):均值 5ms,中位数 4ms,与 Processing 时间接近。Total(总耗时):均值 12ms,中位数 7ms,最长 1228ms。请求耗时百分比
Percentage of the requests served within a certain time (ms)请求响应时间的分布(百分比越靠后,代表长尾请求):50% 的请求在 7ms 内完成。90% 的请求在 10ms 内完成。99% 的请求在 26ms 内完成。最慢的请求耗时 1228ms(可能是网络波动或资源竞争)。总结
性能表现优秀:QPS 高达 1.5 万/秒,均值延迟 13ms,适合高并发场景。稳定性良好:99% 的请求在 26ms 内完成,仅极少数请求因网络或服务器波动较慢。优化点:检查个别慢请求(如 1228ms)是否由后端资源竞争或网络问题导致。
4.系统性能优化
- 文件句柄
Linux一切皆文件,文件句柄可以理解为就是一个索引,文件句柄会随着我们进程的调用频繁增加,系统默认文件句柄是有限制的,不能让一个进程无限的调用,所以我们需要限制每个 进程和每个服务使用多大的文件句柄,文件句柄也是必须要调整的优化参数。
[root@web02 ~]# tail -1 /etc/security/limits.conf
* - nofile 65535 #其他了解
1、系统全局性修改。
# * 代表所有用户
* soft nofile 25535
* hard nofile 255352.用户局部性修改
#针对root用户,soft仅提醒,hard限制,nofile打开最大文件数
root soft nofile 65535
root hard nofile 655353.进程局部性修改
#针对nginx进程,nginx自带配置
worker_rlimit_nofile 300004.调整内核参数:让time_wait状态重用(端口重用)[flag]
[root@web01 ROOT]# vim /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1 # 开启端口复用
net.ipv4.tcp_timestamps = 0 # 禁用时间戳
[root@web01 ROOT]# sysctl -p #可以查看我们添加的内核参数
[root@web01 ROOT]# sysctl -a #可以查看所有内核参数
在高并发短连接的TCP服务器上,当服务器处理完请求后立刻主动正常关闭连接。这个场景下会出现大量socket处于TIME_WAIT状态。如果客户端的并发量持续很高,此时部分客户端就会显示连接不上。 我来解释下这个场景。主动正常关闭TCP连接,都会出现TIMEWAIT。
- 为什么我们要关注这个高并发短连接呢?有两个方面需要注意:
1. 高并发可以让服务器在短时间范围内同时占用大量端口,而端口有个0~65535的范围,并不是很多,刨除系统和其他服务要用的,剩下的就更少了。
2. 在这个场景中,短连接表示业务处理+传输数据的时间 远远小于 TIMEWAIT超时的时间的连接。
这里有个相对长短的概念,比如取一个web页面,1秒钟的http短连接处理完业务,在关闭连接之后,这个业务用过的端口会停留在TIMEWAIT状态几分钟,而这几分钟,其他HTTP请求来临的时候是无法占用此端口的(占着茅坑不拉翔)。单用这个业务计算服务器的利用率会发现,服务器干正经事的时间和端口(资源)被挂着无法被使用的时间的比例是 1:几百,服务器资源严重浪费。(说个题外话,从这个意义出发来考虑服务器性能调优的话,长连接业务的服务就不需要考虑TIMEWAIT状态。同时,假如你对服务器业务场景非常熟悉,你会发现,在实际业务场景中,一般长连接对应的业务的并发量并不会很高。
禁止时间戳
5.代理服务优化
通常nginx作为代理服务,负责转发用户的请求,那么在转发的过程中建议开启HTTP长连接,用于减少握手次数,降低服务器损耗。
负载均衡的优化:
upstream http_backend {server 127.0.0.1:8080;keepalive 16; #负载均衡和后端长连接
}server {...location /http/ {proxy_pass http://http_backend;proxy_http_version 1.1; #对于http协议应该指定为1.1proxy_set_header Connection ""; #清除“connection”头字段proxy_next_upstream error timeout http_500 http_502 http_503 http_504; #平滑过渡proxy_set_header Host $http_host;proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for;proxy_connect_timeout 30s; # 代理连接web超时时间proxy_read_timeout 60s; # 代理等待web响应超时时间proxy_send_timeout 60s; # web回传数据至代理超时时间proxy_buffering on; # 开启代理缓冲区,web回传数据至缓冲区,代理边收边传返回给客户端proxy_buffer_size 32k; # 代理接收web响应的头信息的缓冲区大小proxy_buffers 4 128k; # 缓冲代理接收单个长连接内包含的web响应的数量和大小...}
}PHP优化
upstream fastcgi_backend {server 127.0.0.1:9000;keepalive 8;
}server {...location /fastcgi/ {fastcgi_pass fastcgi_backend;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_keep_conn on; # 开启长连接fastcgi_connect_timeout 60s; # 超时时间include fastcgi_params;...}
}
6.静态资源优化
Nginx作为静态资源Web服务器,用于静态资源处理,传输非常的高效
一个网站里面有一张图片2M,不可能完全放在公司服务器,公司根本扛不住,一般放在客户的缓存。
静态资源类型 | 种类 |
浏览器渲染 | HTML、CSS、JS |
图片文件 | JPEG、GIF、PNG |
视频文件 | FLV、Mp4、AVI |
其他文件 | TXT、DOC、PDF、… |
- 静态资源缓存
浏览器缓存设置用于提高网站性能,尤其是新闻网站,图片一旦发布,改动的可能是非常小的,所以我们希望能否用户访问一次后,图片缓存在用户的浏览器长时间缓存。 浏览器是有自己的缓存机制,他是基于HTTP协议缓存机制来实现的,在HTTP协议中有很多头信息,那么实现浏览器的缓存就需要依赖特殊的头信息来与服务器进行特殊的验证,如Expires(http/1.0);Cache-control(http/1.1)。
#配置在浏览器的缓存时间为7天
[root@web01 conf.d]# cat www.oldboy.conf
server {listen 80;server_name www.oldboy.com;root /code/test;index index.html; location ~ .*\.(jpg|gif|png)$ {expires 7d;}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
缓存到2025-9-3(半个月)
- 案例2.配置资源每次请求源站、不进行缓存
如果开发代码没有正式上线,希望静态文件不被缓存#取消js、css、html等静态文件缓存
[root@web01 conf.d]# vim www.oldboy.conf
server {listen 80;server_name www.oldboy.com;root /code/test;index index.html;location ~ .*\.(js|css|html)$ {add_header Cache-Control no-store;add_header Pragma no-cache;}location ~ .*\.(jpg|gif|png)$ {expires 7d;}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
7.文件高效存储
将多个包一次发送,用于提升网络传输效率,大文件推荐打开,需要开启sendfile才行
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
名称 | 作用 |
TCP_NODELAY | 推荐启用场景: 实时交互系统 远程桌面(SSH/VNC) 在线游戏(动作类) 金融交易系统 高延迟敏感协议 |
TCP_NOPUSH | 批量写场景 日志收集系统 数据库WAL写入 CDN边缘节点 特定协议优化 |
- 配置方式:(配置文件中写入)
sendfile on;#tcp_nopush on; 大文件业务开启、比如静态文件 CDN缓存#tcp_nodelay 小文件业务开启、比如金融平台、在线游戏类业务
8.静态资源压缩
Nginx将响应报文发送至客户端之前启用压缩功能,然后进行传输,这能够有效地节约带宽,并提高响应至客户端的速度。
级别 | CPU消耗 | 压缩率 | 适用场景 | 示例格式压缩率 |
1 | ★ | ~20% | 实时流媒体 | JS: 65% → 52% |
3 | ★★ | ~35% | 动态内容 | HTML: 70% → 45% |
6 | ★★★ | ~50% | 通用Web | CSS: 80% → 40% |
9 | ★★★★ | ~55% | 静态资源 | JSON: 75% → 35% |
注:压缩率基于典型文本内容测试,实际结果取决于内容冗余度
gzip传输压缩,传输前压缩,传输后解压
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in locationgzip压缩哪些文件
Syntax: gzip_types mime-type ...;
Default: gzip_types text/html;
Context: http, server, locationgzip压缩比率,加快传输,但压缩本身比较耗费服务器性能
Syntax: gzip_comp_level level;
Default:gzip_comp_level level 1;
Context: http, server, locationgzip压缩协议版本,压缩使用在http哪个协议,主流选择1.1版本
Syntax: gzip_http_version 1.0 | 1.1;
Default:gzip_http_version 1.1;
Context: http, server, location
- 案例: 压缩txt文件
#准备10M的文件
[root@web01 conf.d]# ll -h /code/test/b.txt
-rw-r--r-- 1 root root 10M Aug 19 11:37 /code/test/b.txt
配置压缩
[root@web01 conf.d]# vim www.oldboy.conf server_name www.oldboy.com;root /code/test;index index.html;location ~ .*\.(js|css|html)$ {add_header Cache-Control no-store;add_header Pragma no-cache;}location ~ .*\.(jpg|gif|png)$ {expires 7d;}location ~ .*\.(txt|xml|html|json|js|css)$ {gzip on;gzip_http_version 1.1;gzip_comp_level 4;gzip_types text/plain application/json application/x-javascript application/css application/xml text/javascrip
t;}
}
~
~
~
"www.oldboy.conf" 21L, 543C written
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
- 图片压缩配置方式
location ~ .*\.(jpg|png|gif) {root /code/test;gzip on;gzip_types image/jpeg image/gif image/png;gzip_comp_level 2;gzip_http_version 1.1; }
9.防止资源盗链
- 定义:
指的是防止资源被其他网站恶意盗用。
- 基础防盗链设置思路:
主要是针对客户端请求过程中所携带的一些Header信息来验证请求的合法性,比如客户端在请求的过程中都会携带referer信息。优点是规则简单,配置和使用都很方便,缺点是防盗链所依赖的Referer验证信息是可以伪造的,所以通过referer信息防盗链并非100%可靠,但是他能够限制大部分的盗链情况。
Syntax: valid_referers none | blocked | server_name | string ...;
Default: -;
Context: server, location
#none: referer来源头部为空的情况
#blocked: referer来源头部不为空,这些都不以http://或者https://开头
#server_name: 来源头部信息包含当前域名,可以正则匹配
- web01配置盗链、偷取WEB02的图片 类似A网站
[root@web01 conf.d]# cat www.oldboy.conf
server {listen 80;server_name www.oldboy.com;root /code/test;index index.html;
}[root@web01 conf.d]# cat /code/test/index.html
<html><head><meta charset="utf-8"><title>oldboy.com</title></head><body style="background-color:pink;"><center><img src="http://www.test.com/31.png"/></center></body>
</html>
- WEB02配置 B网站server
[root@web02 conf.d]# cat test.conf
server {listen 80;server_name www.test.com;root /code/test;index index.html;}
[root@web02 conf.d]# nginx -t
snginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl restart nginx#最后 windows的hosts解析
10.0.0.8 www.test.com
10.0.0.7 www.oldboy.com
- 准备一张禁止盗图的图片
[root@web02 conf.d]# vim test.conf
server {listen 80;server_name www.test.com;root /code/test;index index.html;location ~ .*\.(jpg|png|gif) {valid_referers none blocked *.baidu.com;if ( $invalid_referer ) {#return 403;rewrite ^(.*)$ /d.png break; }}
}
~
[root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl restart nginx
[root@web02 conf.d]# ll /code/test/d.png
-rw-r--r-- 1 root root 149180 Aug 19 12:03 /code/test/d.png
10.跨域访问
- 定义:
当我们通过浏览器访问a网站时,同时会利用到ajax或其他方式,同时也请求b网站,这样的话就出现了请求一个页面,使用了两个域名,这种方式对浏览器来说默认是禁止的。
- WEB02准备跨域
[root@web02 conf.d]# cat test.conf
server {listen 80;server_name www.test.com;root /code/test;index index.html;
}[root@web02 conf.d]# cat /code/test/index.html
<html lang="en">
<head><meta charset="UTF-8" /><title>测试ajax和跨域访问</title><script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){$.ajax({type: "GET",url: "http://www.oldboy.com", # 此域名是WEB01的域名success: function(data) {alert("sucess 成功了!!!");},error: function() {alert("fail!!,跨不过去啊,不让进去啊,只能...!");}});
});
</script><body><h1>跨域访问测试</h1></body>
</html>
- WEB01准备静态页面
[root@web01 conf.d]# cat www.oldboy.conf
server {listen 80;server_name www.oldboy.com;root /code/test;index index.html;
}[root@web01 conf.d]# echo web01... > /code/test/index.htmlwindows的hosts解析
10.0.0.8 www.test.com
10.0.0.7 www.oldboy.com
- 配置WEB02允许跨域请求
[root@web01 conf.d]# cat www.oldboy.conf
server {listen 80;server_name www.oldboy.com;root /code/test;index index.html;location ~ .*\.(html|htm)$ {add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;}
}
11.CPU亲和
- 定义:
少进程之间不断频繁切换,减少性能损耗,其实现原理是将CPU核心和Nginx工作进程绑定方式,把每个worker进程固定到对应的cpu上执行,减少切换CPU的cache miss,获得更好的性能。
- 查看默认的Nginx进程绑定的核心
[root@web01 ~]# ps -eo pid,args,psr|grep [n]ginx1394 nginx: master process /usr/ 31395 nginx: worker process 01396 nginx: worker process 31397 nginx: worker process 21398 nginx: worker process 0
- 配置CPU亲和方式
# 第一种绑定组合方式(不推荐)
worker_processes 12;
worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000;# 第二种方式(使用较少)
worker_processes 2;
worker_cpu_affinity 101010101010 010101010101;# 第三种最佳绑定方式,修改nginx启动的work进程为自动。
worker_processes auto;
worker_cpu_affinity auto;#在Nginx主配置文件中修改
[root@web01 ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto; # 配置CPU自动亲和error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
....#查看cpu亲和
[root@web01 ~]# systemctl restart nginx php-fpm
[root@web01 ~]# ps -eo pid,args,psr|grep [n]ginx1512 nginx: master process /usr/ 31513 nginx: worker process 01514 nginx: worker process 11515 nginx: worker process 21516 nginx: worker process 3
12.今日总结
- Nginx性能优化
- 压力测试工具
- 系统性能优化
- 代理服务优化
- 静态资源优化
- 文件高效传输
- 静态资源压缩
- 防止资源盗链
- 跨域访问
- CPU亲和
[root@nginx ~]# cat nginx.conf
user www; # nginx进程启动用户
worker_processes auto; #与cpu核心一致即可
worker_cpu_affinity auto; # cpu亲和error_log /var/log/nginx/error.log warn; # 错误日志
pid /run/nginx.pid;
worker_rlimit_nofile 35535; #每个work能打开的文件描述符,调整至1w以上,负荷较高建议2-3w 不用events {use epoll; # 使用epoll高效网络模型 默认的worker_connections 65535; # 限制每个进程能处理多少个连接,10240x[cpu核心]
}http {include mime.types;default_type application/octet-stream;charset utf-8,gbk; # 统一使用utf-8字符集# 定义日志格式log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';#定义json日志格式 log_format json_access '{"@timestamp":"$time_iso8601",''"host":"$server_addr",''"clientip":"$remote_addr",''"size":$body_bytes_sent,''"responsetime":$request_time,''"upstreamtime":"$upstream_response_time",''"upstreamhost":"$upstream_addr",''"http_host":"$host",''"url":"$uri",''"domain":"$host",''"xff":"$http_x_forwarded_for",''"referer":"$http_referer",''"status":"$status"}';access_log /var/log/nginx/access.log main; # 访问日志server_tokens off; # 禁止浏览器显示nginx版本号client_max_body_size 20m; # 文件上传大小限制调整 默认1M# 文件高效传输,静态资源服务器建议打开sendfile on;tcp_nopush on;# 文件实时传输,动态资源服务建议打开,需要打开keepalivetcp_nodelay on;keepalive_timeout 65;# Gzip 压缩gzip on;gzip_disable "MSIE [1-6]\."; #针对IE浏览器不进行压缩gzip_http_version 1.1;gzip_comp_level 2; #压缩级别gzip_buffers 16 8k; #压缩的缓冲区gzip_min_length 1024; #文件大于1024字节才进行压缩,默认值20gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;# 虚拟主机include /etc/nginx/conf.d/*.conf;
}
面试题:Nginx优化过哪些东西?
1、CPU亲和
2、调整每个worker进程的最大连接数、默认1024
3、文件的高效传输sendfile
4、开启tcp长连接,以及长连接超时时间keepalived
5、开启文件传输压缩gzip
6、开启静态文件expires缓存
7、隐藏nginx版本号
8、配置防盗链、以及跨域访问
9、优雅显示nginx错误页面
10、nginx加密传输https优化
11、防DDOS、cc攻击,限制单IP并发连接,以及http请求