本文共 9674 字,大约阅读时间需要 32 分钟。
前言
最近整理了一下Nginx的优化,详细介绍,更改用户与组、配置网页缓存、日志切割、设置连接超时等;
直接修改
[root@localhost ~]# iptables -F[root@localhost ~]# setenforce 0[root@localhost ~]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
解压缩
[root@localhost ~]# cd /opt[root@localhost opt]# lsrh[root@localhost opt]# rz -Erz waiting to receive.[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
创建管理用户
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
编译及安装
[root@localhost nginx-1.12.2]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module
编译
[root@localhost nginx-1.12.2]# make[root@localhost nginx-1.12.2]# make install路径优化,便于系统管理[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
使用service控制
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx#!/bin/bash# chkconfig: - 99 20# description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx"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 1esacexit 0#增加权限[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx #重启服务[root@localhost nginx-1.12.2]# service nginx start#查看端口[root@localhost nginx-1.12.2]# netstat -ntap | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22235/nginx: master
[root@server3 nginx-1.12.2]# curl -I http://192.168.158.30HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 11 Nov 2020 03:46:42 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Wed, 11 Nov 2020 03:21:54 GMTConnection: keep-aliveETag: "5fab58d2-264"Accept-Ranges: bytes
[root@server3 conf]# curl -I http://192.168.158.30HTTP/1.1 200 OKServer: nginx #已经隐藏版本号Date: Wed, 11 Nov 2020 03:56:19 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Wed, 11 Nov 2020 03:21:54 GMTConnection: keep-aliveETag: "5fab58d2-264"Accept-Ranges: bytes
[root@localhost ~]# iptables -F[root@localhost ~]# setenforce 0[root@localhost ~]# cd /opt[root@localhost opt]# lsrh[root@localhost opt]# rz -Erz waiting to receive.[root@localhost opt]# lsnginx-1.12.2.tar.gz rh[root@localhost opt]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost nginx-1.12.2]# cd src/[root@localhost src]# lscore event http mail misc os stream[root@localhost src]# cd core/[root@localhost core]# vim nginx.h[root@localhost core]# vim nginx.h
[root@localhost core]# cd ../../[root@localhost nginx-1.12.2]# lsauto CHANGES.ru configure html man srcCHANGES conf contrib LICENSE README[root@localhost nginx-1.12.2]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module#编译[root@localhost nginx-1.12.2]# make[root@localhost nginx-1.12.2]# make install[root@server3 nginx-1.12.2]# curl -I 192.168.158.30HTTP/1.1 200 OKServer: nginx/1.2.2 Date: Wed, 11 Nov 2020 04:29:48 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Wed, 11 Nov 2020 04:29:13 GMTConnection: keep-aliveETag: "5fab6899-264"Accept-Ranges: bytes
[root@server3 core]# id nobodyuid=99(nobody) gid=99(nobody) 组=99(nobody)[root@server3 core]# cd /usr/local/nginx/conf/[root@server3 conf]# vi nginx.conf
[root@server3 ~]# service nginx stop[root@server3 ~]# service nginx start[root@server3 ~]# ps aux | grep nginxroot 48547 0.0 0.0 20500 608 ? Ss 13:53 0:00 nginx: master process /usr/local/nginx/sbin/nginxnginx 48548 0.0 0.0 23028 1384 ? S 13:53 0:00 nginx: worker processroot 48550 0.0 0.0 112676 984 pts/2 R+ 13:53 0:00 grep --color=auto nginx
设置方法
[root@localhost conf]# vim nginx.conf
末行添加
[root@client1 ~]# date -d "-1 day" "+%Y%m%d"20201111[root@client1 opt]# date -d "1 day"2020年 11月 13日 星期五 09:08:10 CST[root@client1 opt]# date -d "0 day"2020年 11月 12日 星期四 09:08:39 CST[root@client1 opt]# date -d "-1 day"2020年 11月 11日 星期三 09:08:43 CST
编写脚进行日志分割的思路
#!/bin/bash#Filename:test.sh#设置日期名称d=$(date -d "-1 day" "+%Y%m%d") #显示一天前的时间logs_path="/var/log/nginx"pid_path="/usr/local/nginx/logs/nginx.pid"#自动创建日志目录[ -d $logs_path ] || mkdir -p $logs_path#分隔日志mv /usr/local/nginx/logs/access.log ${ logs_path}/tase.cpm-access.log-$d#生成新日志kill -USR1 $(cat $pid_path)#删除30天前的日志find $logs_path -mtime +30 | xargs rm -rf
[root@localhost opt]# chmod +x shuai.sh [root@localhost nginx]# cd /opt/[root@localhost opt]# ./shuai.sh [root@localhost opt]# ls /var/log/nginx/tase.cpm-access.log-20201111
[root@client1 opt]# crontab -eno crontab for root - using an empty onecrontab: installing new crontab
超时参数为避免同-客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
Keepalive_timeout设置连接保持超时时间Client_header_timeout指定等待客户端发送请求头的超时时间Client_body_timeout设置请求体读超时时间
[root@localhost opt]# cd /usr/local/nginx/conf/[root@localhost conf]# lsfastcgi.conf nginx.conffastcgi.conf.default nginx.conf.defaultfastcgi_params scgi_paramsfastcgi_params.default scgi_params.defaultkoi-utf uwsgi_paramskoi-win uwsgi_params.defaultmime.types win-utfmime.types.default[root@localhost conf]# vim nginx.conf第一个是客户端的超时时间,第二个是服务端的超时时间,参数放在http中以上就是进行超时时间的设置
[root@localhost html]# service nginx stop[root@localhost html]# service nginx start
在高并发场景,需要启动更多的 Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
可以使用 ps auxi命令查看Ngnx运行进程的个数
更改进程数的配置方法
修改配置文件,修改进程配置参数
修改配置文件的 worker_ processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍
运行进程数多一些,响应访问请求时, Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度
使用 ps aux查看运行进程数的变化情况
默认情况, Nginx的多个进程可能跑在一个cPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU
在一台4核物理服务器,可进行以下配置,将进程进行分配
Worker_cpu_affinity 0001 0010 0100 1000 ‘//核心数的序列位置’
[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"8
[root@localhost conf]# pkill -9 nginx[root@localhost conf]# netstat -anpt | grep nginx[root@localhost conf]# nginx[root@localhost conf]# netstat -anpt | grep nginxtcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 41766/nginx: master [root@localhost conf]# ps aux | grep nginxroot 41766 0.0 0.0 20496 612 ? Ss 10:35 0:00 nginx: master process nginxnginx 41767 0.0 0.0 23024 1384 ? S 10:35 0:00 nginx: worker processnginx 41768 0.0 0.0 23024 1388 ? S 10:35 0:00 nginx: worker processnginx 41769 0.0 0.0 23024 1396 ? S 10:35 0:00 nginx: worker processnginx 41770 0.0 0.0 23024 1380 ? S 10:35 0:00 nginx: worker processroot 41774 0.0 0.0 112680 984 pts/0 S+ 10:36 0:00 grep --color=auto nginx
[root@localhost conf]# vi /usr/local/nginx/conf/nginx.conf#搜索gzip on 第一行取消注释gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json; gzip_disable "MSIE [1-6]\."; gzip_vary on;
[root@localhost conf]# service nginx stop[root@localhost conf]# service nginx start
防盗链概述
使用三台主机模拟防盗链
IP地址 | 域名 | 用途 | 系统 |
---|---|---|---|
20.0.0.12 | www.tom.com | 源主机 | CentOS7 |
20.0.0.13 | -------------- | 盗链主机 | CentOS7 |
[root@localhost ~]# vi /etc/hosts192.168.73.40 www.test.com
[root@server3 html]# vi index.htmlThis is Copy!
[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; } }
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 空闲进程数FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多
可以有20个进程存在[root@localhost ~]# vi /usr/local/php/etc/php-fpm.d/www.confpm = dynamicpm.max_children = 20pm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers = 8[root@localhost ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php-fpm.d/www.conf[root@localhost ~]# netstat -ntap | grep 9000tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2094/php-fpm: maste
转载地址:http://zfdaf.baihongyu.com/