目录
一.更改进程数
- 安装Nginx服务
systemctl start vsftpd //开启ftp服务
#使用wget工具从官网上下载nginx压缩包
wget http://nginx.org/download/nginx-1.17.6.tar.gz
#解压
tar zxvf nginx-1.12.2.tar.gz -C /opt
#安装环境包
yum install gcc gcc-c++ pcre pcre-devel zlib-devel make -y
#创建用户,不建立宿主文件,且不能再shell上登录
useradd -M -s /sbin/nologin nginx
#配置,安装且编译
cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
#nigix运行的用户和组都为nginx
#启用http_stub_status_module模块以支持状态统计,便于查看服务器的连接信息
make && make install
#为主程序nginx创建软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
#优化服务控制,service工具
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: -99 20
# description: Nginx Service Control Script
#主程序
PROG="/usr/local/nginx/sbin/nginx"
#PID号
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
#添加执行权限
chmod +x /etc/init.d/nginx
#添加为系统服务
chkconfig --add /etc/init.d/nginx
#开启服务#关闭防火墙
service nginx start
systemctl stop firewalld
setenforce 0
#验证服务是否开启
netstat -natp | grep 80
- 验证Nginx服务
- 在高并发的坏境中,需要启动更多的Nginx进程以保证快速响应,用以处理用户的请求,避免造成阻塞,使用ps aux查看Nginx运行的进程个数
[root@localhost nginx-1.12.2]# ps aux | grep nginx
root 7765 0.0 0.0 20544 604 ? Ss 09:01 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 7769 0.0 0.1 23072 1640 ? S 09:01 0:00 nginx: worker process
root 42127 0.0 0.0 112728 972 pts/1 R+ 09:12 0:00 grep --color=auto nginx
[root@localhost nginx-1.12.2]#
注:其中master process是Nginx的主进程,worker process是Nginx的子进程
- 修改Nginx的配置文件中的worker_process参数,一般设置为CPU的个数或者核数,在高并发的情况下可以设置为Cpu的个数或者核数的2倍,可以先查看CPU的核数确定参数
[root@localhost nginx-1.12.2]# cat /proc/cpuinfo | grep -c "physical"
2
[root@localhost nginx-1.12.2]#
-
在nginx的主配置文件,设置参数为2,与cpu的核数相同,运行进程数设置多一些,响应客户端访问请求时,Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
...
worker_processes 2;
...
#重启服务
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# service nginx start
-
查看运行进程的变化
[root@localhost nginx-1.12.2]# ps aux | grep nginx
root 42392 0.0 0.0 20544 604 ? Ss 09:33 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 42393 0.0 0.1 23072 1392 ? S 09:33 0:00 nginx: worker process
nginx 42394 0.0 0.1 23072 1392 ? S 09:33 0:00 nginx: worker process
root 42406 0.0 0.0 112728 972 pts/1 R+ 09:34 0:00 grep --color=auto nginx
[root@localhost nginx-1.12.2]#
注:开启了一个主进程和2两个子进程,由此可见参数起到作用,当有多核cpu时,设置参数 worker_cpu_affiniity
二.网页压缩
- Nginx的ngx_http_gizp_module压缩模块提供了对文件内容压缩的功能,允许Nginx服务器将输出内容发送到客户端之前进行压缩,以节省网站的带宽。
- 默认情况下,Nginx安装的该模块,只需要在配文件中加入相应的压缩功能
#开启压缩功能
gzip on;
#表示申请4个单位为16KB的内存作为压缩结果流缓存,默认值时申请与原始数据大小相同的内存空间来存储gzip压缩结果
gzip_buffers 4 64k;
#用于设置识别http协议版本,默认时1.1,目前大部分浏览器以及支持解压,但是处理很慢,消耗资源多
gzip_http_version 1.1;
用来指定压缩比,1压缩最小,处理速度块,9压缩最多,处理速度慢
gzip_comp_level 2;
#用于设置允许压缩的页面最小字节数
gzip_min_length 1k;
#该选项可以让前端的缓存服务器缓存经过gzip压缩的页面
gzip_vary on;
#压缩类型,对那些网页文档启动压缩功能
gzip_types test/plain text/javascript application/x-javascript text/css text/html text/xml application/xml;
- 网页压缩操作
#在nginx的主页面中加入图片
[root@localhost html]# ls
1.jpg 50x.html index.html
[root@localhost html]# vim index.html
...
<img height=200px src="1.jpg"/>
...
[root@localhost html]#
- 设置压缩参数
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
gzip on;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 1k;
gzip_vary on;
gzip_types test/plain text/javascript application/x-javascript text/css text/xml image/jpg image/jpeg image/png image/gif application/xml application/xml+rss;
...
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
[root@localhost ~]#
- 测试压缩模块
三.防盗链
- 在企业网站服务中一般要配置防盗链功能,以避免网站被非法盗用,造成经济损失,也避免不必要的带宽浪费
- Nginx的防盗链功能非常强大,在默认情况之下,只需要进行简单的配置,即可实现防盗链功能
- 防盗链具体操作
- 准备两台主机模拟盗链与防盗链
类别 | IP地址 | 域名 |
源主机 | 192.168.43.227 | www.kgc.com |
盗链主机 | 192.168.43.221 | www.yun.com |
- 设置源主机
[root@localhost html]# vim /etc/hosts
...
192.168.43.221 www.yun.com
192.168.43.227 www.kgc.com
...
[root@localhost named]# cd /usr/local/nginx/html
[root@localhost html]# ls
1.jpg 50x.html index.html
[root@localhost html]# vim index.html
...
<img height=200px src="1.jpg"/>
...
- 查看源主机的页面
- 设置盗链主机进行盗链
[root@localhost named]# vim /etc/hosts
...
192.168.43.227 www.kgc.com
192.168.43.221 www.yun.com
...
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim index.html
...
<img height=300px src="http://www.kgc.com/1.jpg"/>
...
- 验证盗链结果
- 在源主机上设置防盗链模块
vim /usr/local/nginx/conf/nginx.conf
...
location ~* \.(jpg|gif|swf)$ {
valid_referers none blocked www.kgc.com; //满足此条规则,不跳转
if ($invalid_referer) { //如果为真
rewrite ^/ http://www.kgc.com/error.png; //跳转错误页面
}
}
...
service nginx restart
//valid_referers,设置信任网站
- 验证防盗链
四.FPM参数优化
- 在LNMP架构中php-fpm模块处理动态请求,为了提高PHP的处理速度,可以对FPM模块进行参数的调整
- FPM进程有两种启动方式,由pm参数指定,分别是static和dynamic
- static将产生固定数据的fpm进程,可以使用pm.max_children指定的启动的进程数量
- dynamic将以动态的方式产生fpm进程,这种方式产生的参数则要根据服务器的内存与服务器负载进行调整
- dynamic方式下的参数
选项 | 描述 |
pm.max_children | 指定启动的进程的最大的数量 |
pm.start.servcie | 动态方式下初始的ftpm的进程数量 |
pm_mim_space_servers | 动态方式下最小的fpm空闲进程数 |
pm_max_space_servers | 动态方式下最大的fpm空闲进程数 |