nginx高性能调优及防盗链

前言

为了适应企业需求,就需要考虑如何提升Nginx的性能与稳定性,这就是Nginx优化的内容,本次博客主要讲述Nginx的优化以及防盗链的部署。
Nginx的详细编译安装步骤:https://blog.csdn.net/empty_csx/article/details/115308327

一、隐藏版本号

  • 在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄漏
  • 查看方法
    • 使用fiddler工具在Windows客户端查看Nginx版本号
    • 在CentOS系统中使用"curl -I 网址”命令查看
  • Nginx隐藏版本号的方法
    • 修改配置文件法
    • 修改源码法
[root@localhost ~]# curl -I http://192.168.235.10
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 14 Oct 2019 09:15:23 GMT

1.1 修改配置文件方法

vi /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;

    server_tokens off; ###关闭版本号
[root@localhost ~]# systemctl restart nginx

[root@localhost ~]# curl -I http://192.168.235.10
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 14 Oct 2019 09:15:23 GMT

1.2 修改源码并重新编译安装

[root@localhost ~]# vi /root/nginx-1.12.2/src/core/nginx.h
#define nginx_version      1012002
#define NGINX_VERSION      "1.1.1" ###修改版本号
#define NGINX_VER          "nginx/" NGINX_VERSION

[root@localhost nginx-1.12.2]# make && make install

[root@localhost ~]# curl -I http://192.168.235.10
HTTP/1.1 200 OK
Server: nginx/1.1.1

二、修改Nginx用户和组

  • 1、Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
  • 2、Nginx默认使用nobody用户账号与组账号
  • 3、修改的方法
    编译安装时指定用户与组
[root@localhost ~]# vi /etc/nginx
# user nobody修改成nginx nginx
user nginx nginx

[root@localhost ~]# ps aux | grep nginx
root       1108  0.0  0.0  20496   612 ?        Ss   11:53   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1109  0.0  0.1  20940  1336 ?        S    11:53   0:00 nginx: worker process
root       1111  0.0  0.0 112660   972 pts/0    R+   11:53   0:00 grep --color=auto nginx

三、配置Nginx网页缓存时间

  • 1、当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
  • 2、一般针对静态网页设置,对动态网页不设置缓存时间
  • 3、设置方法
    • 在主配置文件的location段加入expires参数
[root@localhost ~]# vi /etc/nginx
location / {
            root   html;
            index  index.html index.htm;
            expires 1d; ###设置缓存时间为1天
        }

四、配置日志分割

  • 1、随着Nginx运行时间增加,日志也会增加。太大的日志文件对监控是一个大灾难。所以需要定期进行日志文件的切割
  • 2、Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割(Kill -HUP cat /xxx/log/nginx.pid #平滑重启nginx,类似reload)
    -QUIT :结束进程;-USR1:日志分割;-USR2:平滑升级
  • 3、通过Linux的计划任务周期性地进行日志切割
  • 4、编写脚本进行日志切割示例
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
error_log  logs/error.log  info;

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main; ###去除前面#号

[root@localhost ~]# 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@localhost ~]# vim fenge.sh
#!/bin/bash
#Filename:fenge.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}/test.com-access.log-$d ###分割新的日志
kill -HUP $(cat $pid_path) ###生成新的日志
find $logs_path -mtime +30 | xargs rm -rf ###删除30天前的日志(xargs用来传递参数)

[root@localhost ~]# chmod +x fenge.sh
[root@localhost ~]# ./fenge.sh
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 44
-rw-r--r--. 1 root root 44866 10月 16 15:11 test.com-access.log-20191115 ###运行之后生成昨天的日志

  • 关于日期
# 获取当天的的日期
[root@localhost ~]# date +%Y%m%d
20191115
[root@localhost ~]# date
Tue Nov 17 15:26:16 CST 2019

# 昨天
[root@localhost ~]# date -d "-1 day"
Mon Nov 16 15:26:35 CST 2019

# 明天
[root@localhost ~]# date -d "day"
Wed Nov 18 15:26:50 CST 2019

# 前一天的日期
[root@localhost ~]# date -d "-1 day" +%d
16

# 前一小时
[root@localhost ~]# date -d "-h hour" +%H
08

# 前一分钟
[root@localhost ~]# date -d "-1 min" +%M
27

# 前一秒钟
[root@localhost ~]# date -d "-1 second" +%S
32

配置Nginx实现链接超时

  • 1、为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
  • 2、Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)
  • 3、指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,
    若将它设置为0,就禁止了keepalive连接。

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
   #keepalive_timeout  0;
    keepalive_timeout  180;
    client_header_timeout 80;    ##等待客户端发送请求头的超时时间 超时会发送408错误
    client_body_timeout 80;    ##设置请求体的读超时时间

[root@localhost ~]# systemctl restart nginx

六、更改Nginx运行进程数

  • 1、在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
  • 2、修改配置文件的worker_processes参数
    一般设为CPU的个数或者核数
    在高并发情况下可设置为CPU个数或者核数的2倍
  • 3、增加进程数,可减少了系统的开销,提升了服务速度
  • 4、使用ps aux查看运行进程数的变化情况
[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical id" ###查看物理CPU的个数
4
[root@localhost ~]# ps aux | grep nginx ###查看运行行程数
root       1025  0.0  0.0  20500   632 ?        Ss   16:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1030  0.0  0.0  22948  1656 ?        S    16:06   0:00 nginx: worker process
root       1885  0.0  0.0 112680   984 pts/0    S+   16:13   0:00 grep --color=auto nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
worker_processes  4; ###将进程数改为4

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ps aux | grep nginx ###查看运行进程数变化
root       1030  0.0  0.0  20500   632 ?        Ss   16:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1031  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
nginx      1032  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
nginx      1033  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
nginx      1034  0.0  0.0  22948  1408 ?        S    16:17   0:00 nginx: worker process
root       1731  0.0  0.0 112680   980 pts/0    S+   16:18   0:00 grep --color=auto nginx

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

1.修改配置文件

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
 gzip  on; ###开启gzip压缩功能
    gzip_min_length 1k; ###压缩阈值(超过1k的文件进行压缩)
    gzip_buffers 4 16k; ###buffer(缓冲)大小为4个16k缓冲区大小
    gzip_http_version 1.1; ###压缩版本
    gzip_comp_level 6; ###压缩比率,最小为1,处理速度快,传输速度慢;最大为9,处理速度慢,传输速度快
    gzip_types text/plain application/x-javascript text/css image/jpg image/png image/gif application/xml text/javascript application/x-http-php application/javascript application/json; 
    gzip_vary on; ###选择支持vary header可以让前端的缓存服务器缓存经过gzip压缩的页面
    
[root@localhost ~]# vi /usr/local/nginx/html/index.html ###插入图片
<img src=a.jpg / >
</body>
</html>

[root@localhost ~]# systemctl restart nginx

八、Nginx防盗链设置

  • 1.源主机配置
[root@localhost ~]# vi /etc/hosts
192.168.235.10 www.test.com
  • 2.盗链主页配置
[root@server1 ~]# vi /var/www/html/index.html
<html><body><h1>This is Copy!</h1><img src=http://192.168.235.10/a.jpg / ></body></html>

3、源主机防盗链配置

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

4.测试
在这里插入图片描述
在这里插入图片描述

九、对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个进程存在
[root@localhost ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 2
pm.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 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2094/php-fpm: maste
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值