You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dev-protocol/http/nginx/Nginx静态资源-优化配置语法.md

2.3 KiB

  • Nginx对静态资源如何进行优化配置。这里从三个属性配置进行优化

  • sendfile ;

  • tcp_nopush ;

  • tcp_nodeplay ;

  • 1sendfile用来开启高效的文件传输模式。

语法 sendfile on \off;
默认值 sendfile off;
位置 http、server、location...
  • 请求静态资源的过程:客户端通过网络接口向服务端发送请求,操作系统将这些客户端的请求传递给服务器端应用程序,服务器端应用程序会处理这些请求, 请求处理完成以后,操作系统还需要将处理得到的结果通过网络适配器传递回去。
server {
	listen 80;
	server_name localhost
	location / {
		root html;
		index index.html;
	}
}
在html目录下有一个welcome.html页面访问地址
http://192.168.200.133/welcome.html
  • sendfile优化

  • 2tcp_nopush该指令必须在sendfile打开的状态下才会生效主要是用来提升网络包的传输'效率'

| 语法 | tcp_nopush on\off; | |-----|----------------------|。 | 默认值 | tcp_nopush off; | | 位置 | http、server、location |

  • 3tcp_nodelay该指令必须在keep-alive连接开启的情况下才生效来提高网络包传输的'实时性'
语法 tcp_nodelay on\off;
默认值 tcp_nodelay on;
位置 http、server、location
  • 经过刚才的分析,"tcp_nopush"和”tcp_nodelay“看起来是"互斥的",那么为什么要将这两个值都打开,
  • 这个大家需要知道的是在linux2.5.9以后的版本中两者是可以兼容的,三个指令都开启的好处是,
  • sendfile可以开启高效的文件传输模式
  • tcp_nopush开启可以确保在发送到客户端之前数据包已经充分“填满” 这大大减少了网络开销,并加快了文件发送的速度
  • 然后当它到达最后一个可能因为没有“填满”而暂停的数据包时Nginx会忽略tcp_nopush参数 然后tcp_nodelay强制套接字发送数据。
  • 由此可知TCP_NOPUSH可以与TCP_NODELAY一起设置它比单独配置TCP_NODELAY具有更强的性能。
  • 所以我们可以使用如下配置来优化Nginx静态资源的处理

  • sendfile on;
  • tcp_nopush on;
  • tcp_nodelay on;