Nginx服务深入优化(2)——更改Nginx运行进程数、Nginx实现网页压缩、配置Nginx实现防盗链、FPM模块进行参数优化------超详细的

一、更改Nginx运行进程数

在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
更改进程数的配置方法
■修改配置文件,修改进程配置参数
修改配置文件的worker_processes参数
■一般设为CPU的个数或者核数
■在高并发情况下可设置为CPU个数或者核数的2倍
增加进程数,可减少了系统的开销,提升了服务速度
使用ps aux查看运行进程数的变化情况
默认情况,Nginx的多个进程可能跑在一个CPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU

可以先查看自己的进程数=(CPU核数 + CPU个数) x 2 ,这里我自己时一核一线程,配置有点低
[root@www nginx-1.12.2]# cat /proc/cpuinfo |grep -c "physical"  
4
[root@www nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
worker_processes  4;  ## 数字修改为4
[root@www nginx-1.12.2]# nginx -t   ##语法没错
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www nginx-1.12.2]# killall -s HUP nginx   ###重新加载配置
[root@www nginx-1.12.2]# ps aux | grep nginx    ###可以看到4个进程数
root       4324  0.0  0.0  20704  1520 ?        Ss   10:21   0:00 nginx: master process nginx
nginx      4937  0.0  0.0  23016  1480 ?        S    11:02   0:00 nginx: worker process
nginx      4938  0.0  0.0  23016  1480 ?        S    11:02   0:00 nginx: worker process
nginx      4939  0.0  0.0  23016  1480 ?        S    11:02   0:00 nginx: worker process
nginx      4940  0.0  0.0  23016  1480 ?        S    11:02   0:00 nginx: worker process
root       4952  0.0  0.0 112680   984 pts/0    S+   11:03   0:00 grep --color=auto nginx


二、配置Nginx实现网页压缩功能

[root@www nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
## 找到 gzip on
gzip  on;   ##   #号去掉,开启网页压缩功能
gzip_buffers 4 64k;    ##表示申请4个单位为16k的内存作为压缩结果流缓存
gzip_http_version 1.1;   ###http的版本时1.1
gzip_comp_level 6;       ##用来指定gzip压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,处理速度慢
gzip_min_length 1k;     ##用于设置允许压缩的最小字节数;
gzip_vary on;           ##选择支持vary header可以让前端的缓存服务器缓存经过gzip压缩的页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

[root@www ~]# vim /usr/local/nginx/html/index.html
...省略内容
<h1>Welcome to nginx!</h1>
<img src="image.jpg"/>         ##加入<img/>标签

三、Nginx防盗链设置

C:\Windows\System32\drivers\etc  ## 进入这个目录
用记事本打开 hosts ,在最下面添加如下两行。(如果直接更改不生效,建议复制一份去桌面,在桌面改完后,在覆盖源文件)
### 在最下面添加如下两行
20.0.0.13 www.bt.com   ### 源主机
20.0.0.11 www.test.com   ### 盗链主机
[root@localhost ~]# cd /usr/local/nginx/html/   ##把图片放到index.html目录下
[root@localhost html]# ll
总用量 60
-rw-r--r--. 1 root root   537 10月 25 12:52 50x.html
-rw-r--r--  1 root root 52848 11月 19 12:16 a.jpg
-rw-r--r--. 1 root root   612 10月 25 12:52 index.html
[root@www ~]# vi /usr/local/nginx/html/index.html 
<html>
<head></head>
<body>
<img src=a.jpg / >
</body>
</html>

盗链主机访问,可以正访问
在这里插入图片描述
配置防盗链(源主机)

需要把错误的图片导入目录下
[root@www html]# ll
总用量 68
-rw-r--r--. 1 root root   537 10月 25 12:52 50x.html
-rw-r--r--  1 root root 52848 11月 19 12:16 a.jpg
-rw-r--r--  1 root root  7298 11月 19 19:13 error.png
-rw-r--r--  1 root root    63 11月 19 19:06 index.html

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 location ~*\.(gif|jpg|swf)$ {
             valid_referers none blocked *.test.com test.com;
             if ($invalid_referer) {
             rewrite ^/ http://www.test.com/error.png;
             }
    }

最够盗链主机再次访问,就是我错我的图片了
在这里插入图片描述

四、对FPM模块进行参数优化

1、Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对
FPM 模块进行参数跳转。
2、FPM 优化参数:
pm 使用哪种方式启动 fpm 进程,可以说 static 和 dynamic,前者将产生固定数量的 fpm 进程,后者将以动态的方式产生 fpm 进程
pm.max_children :static 方式下开启的 fpm 进程数
pm.start_servers :动态方式下初始的 fpm 进程数量
pm.min_spare_servers :动态方式下最大的 fpm 空闲进程数
pm.max_spare_servers :动态方式下最大的 fpm 空闲进程数
3、优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
4、优化参数调整
FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多
可以有20个进程存在

#####FPM参数优化###
[root@www php-7.1.10]# cd /usr/local/php/etc/php-fpm.d
[root@www etc]# vi www.conf
pm=dynamic
pm.max_children=20
pm.start_servers=5
pm.min_spare_servers=2
pm.max_spare_servers=8

[root@www ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php-fpm.d/www.conf
[root@www ~]# netstat -ntap | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2094/php-fpm: master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值