Nginx website service optimization (connection timeout, changing the number of processes, web page compression, fpm parameter optimization)

1. Connection timeout

HTTP has a KeeepAlive mode, which tells the web server to keep the TCP connection open after processing a request.
If it accepts other requests from the client, the server will use this unclosed connection without establishing another connection.
KeepAlive stays open for a period of time, they will occupy resources during this period of time, occupying too much will affect performance.

vim /usr/local/nginx/conf/nginx.conf
http {
......
     keepalive_timeot 65 180;
	 client_header_timeout 80;
	 client_body_timeout 80;
......
}
systemctl restart nginx

Insert picture description here
Insert picture description here

  • keepalive_timeout——Specify the timeout of KeepAlive. Specify how long each TCP connection can last, and the server will close the connection after this time. The default value of Nginx is 65 seconds. Some browsers only hold 60 seconds at most, so it can be set to 60 seconds. If it is set to 0, keepalive connection is prohibited.
  • The second parameter (optional)-specifies the time value in the response header Keep-Alive:timeout=time. This header allows some browsers to actively close the connection, so that the server does not have to close the connection. Without this parameter, Nginx will not send Keep-Alive response headers.
  • client_header_timeout——The timeout period for the client to send a complete request header to the server. If the client does not send a complete request header within the specified time, Nginx returns HTTP 408 (Request Timed out).
  • client_body_timeout——Specify the timeout period for sending the request body after the client establishes a connection with the server. If the client does not send any content within the specified time, Nginx returns HTTP 408 (Request Timed out).

Two, change the number of processes

cat /proc/cupinfo | grep -c "physical id"       #查看cpu核数
ps aux | grep nginx                             #查看nginx主进程中包含几个子进程

vim /usr/local/nginx/conf/nginx.conf 
worker_processes 2;                 #修改为核数相同或者2倍
worker_cpu_affinity 01 10;          #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000

Insert picture description here

Three, configure web page compression

vim /usr/local/nginx/conf/nginx.conf
http {
......
   gzip on;                #取消注释,开启gzip压缩功能
   gzip_min_length 1k;     #最小压缩文件的大小
   gzip_buffers 4 64k;     #压缩缓冲区,大小为4个64k缓冲区
   gzip_http_version 1.1;  #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
   gzip_comp_level 6;      #压缩比率
   gzip_vary on;           #支持前端缓存服务器存储压缩页面
   gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;  #压缩类型,表示哪些网页文档启用压缩功能
 ......
}  

cd /usr/local/nginx/html
先将game.jpg文件传到/usr/local/nginx/html目录下
vim index.html
......
<img src="game.jpg"/>           #网页中插入图片
</body>
</html>

systemctl restart nginx

在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络 ---> 选择 HTML、WS、其他
访问 http://196.168.153.10 ,双击200响应消息查看头包含 Content-Encoding:gzip

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Four, fpm parameter optimization

vim /usr/local/php/etc/php-fpm.conf 
pid = run/php-fpm.pid

vim /usr/local/php/etc/php-fpm.d/www.conf
pm = dynamic                    # 96行,fpm进程启动方式,动态的
pm.max_children=20              #107行,fpm进程启动的最大进程数
pm.start_servers = 5            #112行,动态方式下启动时默认开启的进程数,在最小和最大之间
pm.min_spare_severs = 2         #117行,动态方式下最小空闲进程数
pm.max_spare_severs = 8         #122行,动态方式下最大空闲进程数

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`   #重启php-fpm 
netstat -anpt | grep 9000

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112596033