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

发布时间:2020-03-11 04:47:16 作者:wx5d8a17c45cb5b
来源:网络 阅读:372

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

前言

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

1.更改进程数

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

[root@localhost ~]# ps aux | grep nginx
root      12848  0.0  0.0  20544   616 ?        Ss   07:47   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     12849  0.0  0.0  23072  1400 ?        S    07:47   0:00 nginx: worker process
root      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       : 0
processor       : 1
processor       : 2
processor       : 3

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

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -n '3,4p'  /usr/local/nginx/conf/nginx.conf
worker_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 nginx
root      60074  0.0  0.0  20544   616 ?        Ss   09:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     60075  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker process
nginx     60076  0.0  0.0  23072  1396 ?        S    09:29   0:00 nginx: worker process
nginx     60077  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker process
nginx     60078  0.0  0.0  23072  1400 ?        S    09:29   0:00 nginx: worker process
root      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服务器将输出内容发送到客户端之前进行压缩,可以节约网站的带宽,提升用户体验。

未压缩前如下所示:

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 named
tcp        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 nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      80863/nginx: master 

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

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

小结:

本文主要是对nginx服务进程管理以及网页压缩的优化设置。
推荐阅读:
  1. Apache网页优化—网页压缩
  2. 网站SEO之网页压缩nginx gzip压缩

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx优化 更改nginx工作进程数 配置nginx网页压缩案例

上一篇:SQL语句总结基础篇

下一篇:【PostgreSQL】数据库复制

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》