Further optimization of the process of change Nginx configuration and the number of page compression
Foreword
This article describes the process and change the number of Nginx configuration web page compression.
1. Change the number of processes
In high concurrency environments, we need to start more nginx process to ensure rapid response to avoid congestion. You can use ps aux command to view the number of nginx run.
[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
So if you need to change the number of processes we first need to appropriately increase the original cpu parameters, of course, in the real project it is automatically extended. Currently my CPU processor number is 4, so do not need to shut down the experiment added, we can grep the necessary information in / proc directory
[root@localhost ~]# grep 'processor' /proc/cpuinfo
processor : 0
processor : 1
processor : 2
processor : 3
Modify the configuration file (nginx) and then restart the service
[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. Web page compression
Nginx's ngx_http_gzip_module compression module provides for the contents of the file compression feature that allows the output Nginx server to compress content before sending the client, the site of the bandwidth savings, improve the user experience.
Uncompressed before follows:
Open compressed and set the parameters as follows
[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
Ethereal then again after clearing the cache on win10 tester, the results shown below
summary:
本文主要是对nginx服务进程管理以及网页压缩的优化设置。