千家信息网

Nginx深入优化之更改进程数及配置网页压缩

发表于:2025-02-13 作者:千家信息网编辑
千家信息网最后更新 2025年02月13日,Nginx深入优化之更改进程数及配置网页压缩前言​ 本文将介绍Nginx更改进程数以及配置网页压缩。1.更改进程数​ 在高并发环境中,需要启动更多的nginx进程以保证快速响应,避免造成阻塞。可以使用
千家信息网最后更新 2025年02月13日Nginx深入优化之更改进程数及配置网页压缩

Nginx深入优化之更改进程数及配置网页压缩

前言

​ 本文将介绍Nginx更改进程数以及配置网页压缩。

1.更改进程数

​ 在高并发环境中,需要启动更多的nginx进程以保证快速响应,避免造成阻塞。可以使用ps aux命令查看nginx运行的个数。

[root@localhost ~]# ps aux | grep nginxroot      12848  0.0  0.0  20544   616 ?        Ss   07:47   0:00 nginx: master process /usr/local/nginx/sbin/nginxnginx     12849  0.0  0.0  23072  1400 ?        S    07:47   0:00 nginx: worker processroot      67138  0.0  0.0 112732   968 pts/1    R+   08:37   0:00 grep --color=auto nginx

那么如果需要更改进程数我们首先要将原本的cpu参数适当增加,当然在真正的工程中是自动扩展的。目前我的CPU 处理器个数为4,所以做该实验就无需关机添加了,我们可以在/proc目录下grep出必要信息

[root@localhost ~]# grep 'processor' /proc/cpuinfo processor       : 0processor       : 1processor       : 2processor       : 3

修改配置文件(nginx)然后重启服务

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf[root@localhost ~]# sed -n '3,4p'  /usr/local/nginx/conf/nginx.confworker_processes  4;    #修改数量worker_cpu_affinity 0001 0010 0100 1000;    #分配不同的进程给不同的CPU[root@localhost ~]# service nginx stop[root@localhost ~]# service nginx start[root@localhost ~]# ps aux | grep nginxroot      60074  0.0  0.0  20544   616 ?        Ss   09:29   0:00 nginx: master process /usr/local/nginx/sbin/nginxnginx     60075  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker processnginx     60076  0.0  0.0  23072  1396 ?        S    09:29   0:00 nginx: worker processnginx     60077  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker processnginx     60078  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker processroot      60169  0.0  0.0 112732   972 pts/1    R+   09:29   0:00 grep --color=auto nginx

2.网页压缩

Nginx 的ngx_http_gzip_module压缩模块提供了对文件内容压缩的功能,允许Nginx服务器将输出内容发送到客户端之前进行压缩,可以节约网站的带宽,提升用户体验。

未压缩前如下所示:

开启压缩并如下设置参数

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf[root@localhost ~]# sed -n '37,44p' /usr/local/nginx/conf/nginx.conf    gzip  on; #开启压缩功能    gzip_min_length 1k;  #超过1kb开始压缩    gzip_buffers 4 16k;  #大小为4个16k缓冲区大小    gzip_http_version 1.1;     gzip_comp_level 6; #压缩比率,1-9 1压缩最快 9压缩比最高    gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif;    gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,表示ie6以下浏览器不支持    gzip_vary on; #选择支持vary header 可以让前端的缓存服务器缓存经过gzip的压缩的页面[root@localhost ~]# service nginx stop[root@localhost ~]# service nginx start[root@localhost named]# systemctl stop firewalld.service [root@localhost named]# setenforce 0[root@localhost named]# cd -/root[root@localhost ~]# netstat -antp | grep namedtcp        0      0 192.168.68.145:53       0.0.0.0:*               LISTEN      78380/named         tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      78380/named         tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      78380/named         tcp        0      0 192.168.68.145:45416    198.97.190.53:53        ESTABLISHED 78380/named         tcp6       0      0 ::1:53                  :::*                    LISTEN      78380/named         tcp6       0      0 ::1:953                 :::*                    LISTEN      78380/named         [root@localhost ~]# netstat -antp | grep nginxtcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      80863/nginx: master 

然后在win10测试机上清空缓存之后再次抓包,结果如下所示

小结:

本文主要是对nginx服务进程管理以及网页压缩的优化设置。
0