博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学会企业级网站优化一篇就够了(LNMP之全优化)!!!
阅读量:2026 次
发布时间:2019-04-28

本文共 9674 字,大约阅读时间需要 32 分钟。

文章目录

一:Nginx优化与防盗链实验

前言

最近整理了一下Nginx的优化,详细介绍,更改用户与组、配置网页缓存、日志切割、设置连接超时等;

在这里插入图片描述

1.1:方法一:配置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
  • 客户机访问

在这里插入图片描述

  • 使用curl -l命令检测
    -i参数可以显示 http response 的头信息,连同网页代码一起。-I 参数则只显示 http response 的头信息。
[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

1.2:方法二:修改源码

  • Nginx源码文件/usr/src/ nginx-1.12.2/src/ core/nginx. h包含了版本信息,可以随意设置
  • 重新编译安装,隐藏版本信息
  • define NGINX_VERSION “1.12.2″,修改版本号为1.2.2
[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

二:修改Nginx用户与组

  • 新建用户账号,如 nginx
  • 修改主配置文件user选项,指定用户账号
  • 重启 nginx服务,使配置生效
  • 使用 ps aux命令查看nginx的进程信息,验证运行用户账号改变效果
[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

三:配置Nginx网页缓存时间

  • 当Nginx将网页数据返回给客户端后,可设置缓存时,以方便在日后进行相同的内容请求时直接放回,避免重复请求,加快了访问速度
  • 一般针对静态网页设置,对动态网页不设置缓存时间

设置方法

  • 修改Nginx的配置文件,在location段加入expires参数
[root@localhost conf]# vim nginx.conf

末行添加在这里插入图片描述

在这里插入图片描述

四:实现Nginx的日志切割

  • 随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注Nginx日志文件
  • 太大的日志文件对监控是一一个大灾难定期进行日志文件的切割
  • Nginx自身不具备日志分割处理的功能,但可以通过
  • Nginx信号控制功能的脚本实现日志的自动切割
  • 通过Linux的计划任务周期性地进行日志切割

4.1:日志分隔思路

  • 设置时间变量
  • 设置保存日志路径
  • 将目前的日志文件进行重命名
  • 重建新日志文件
  • 删除时间过长的日志文件
  • 设置cron任务,定期执行脚本自动进行日志分割
  • date -d:设置时间格式
[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

五:配置Nginx实现连接超时

超时参数为避免同-客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间

  • 超时参数
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中以上就是进行超时时间的设置

3.3:重启服务

[root@localhost html]# service nginx stop[root@localhost html]# service nginx start

3.4:访问http://20.0.0.41,在用Fidder抓包工具

在这里插入图片描述

六:nginx运行进程数

在高并发场景,需要启动更多的 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

七:配置Nginx实现网页压缩功能

  • Nginx的ngx_htto_gzip_ module压缩模块提供对文件内容压缩的功能
  • 允许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

在这里插入图片描述

八:防盗链优化

防盗链概述

  • 在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失
  • Nginx防盗链功能也非常强大。默认情况下,只需要进行简单的配置,即可实现防盗链处理

使用三台主机模拟防盗链

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.html

This 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;             }    }

在这里插入图片描述

  • 测试

在这里插入图片描述

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

  • Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对FPM 模块进行参数跳转。
  • 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 空闲进程数

  • 优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
  • 优化参数调整

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/

你可能感兴趣的文章
LeetCode166. Fraction to Recurring Decimal(思路及python解法)
查看>>
LeetCode454. 4Sum II(思路及python解法)
查看>>
LeetCode395. Longest Substring with At Least K Repeating Characters(思路及python解法)
查看>>
LeetCode268. Missing Number
查看>>
iOS组件化开发一远端私有库建立(二)
查看>>
我们应当怎样做需求分析
查看>>
问题账户需求分析
查看>>
《uml大战需求分析》阅读笔记05
查看>>
远程办公之:向日葵X 使用教程
查看>>
Varnish4.x配置文件详解
查看>>
Roundcube Webmail 安装配置图文详情
查看>>
Mysql innodb_flush_log_trx_commit 简单调优
查看>>
Piranha web 界面LVS DR 模式配置图文详解
查看>>
Piranha LVS DR 模式 HA 集群配置
查看>>
Apache RewriteCond RewriteRule 跳转故障解决
查看>>
UII自动化之Appium
查看>>
Html
查看>>
JavaScript(含DOM编程)
查看>>
第五讲、Jmeter性能测试实践—HTTP接口
查看>>
第十一讲、jmeter性能测试实战-web程序
查看>>