Nginx优化部署

一、隐藏版本号

隐藏Nginx版本号,避免安全漏洞泄露
Nginx隐藏版本号的方法
未隐藏版本号前使用curl -I(大写的i)命令检测结果

[root@localhost ~]# curl -I http://20.0.0.10
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 16 Oct 2020 06:15:34 GMT
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://20.0.0.10
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 16 Oct 2020 06:25:37 GMT
[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          "jh/" NGINX_VERSION

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

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

二、修改Nginx用户和组

Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
Nginx默认使用nobody用户账号与组账号
修改的方法
编译安装时指定用户与组

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
 
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ps aux | grep nginx ###查看进程信息

三、配置Nginx网页缓存时间

当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
一般针对静态网页设置,对动态网页不设置缓存时间
设置方法
在主配置文件的location段加入expires参数

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.html index.htm;
            expires 1d; ###设置缓存时间为1天
        }

[root@localhost ~]# systemctl restart nginx

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

[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-20201015 ###运行之后生成昨天的日志

关于日期

###获取当天的的日期
[root@localhost ~]# date +%Y%m%d
20201016
[root@localhost ~]# date
2020年 10月 16日 星期五 15:40:12 CST

###昨天
[root@localhost ~]# date -d "-1 day"
2020年 10月 15日 星期四 15:41:09 CST

###明天
[root@localhost ~]# date -d "day"
2020年 10月 17日 星期六 15:41:51 CST

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

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

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

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

五、配置Nginx实现连接超时

为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)
指定每个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运行进程数

  • 在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
  • 修改配置文件的worker_processes参数一般设为CPU的个数或者核数在高并发情况下可设置为CPU个数或者核数的2倍
  • 增加进程数,可减少了系统的开销,提升了服务速度
  • 使用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实现网页压缩功能

修改配置文件

[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 ###插入图片
<html><body><h1>web 1</h1><img src=a.jpg / ></body></html>12 [root@localhost ~]# systemctl restart nginx

八、Nginx防盗链设置

盗链主机配置(IP:20.0.0.30)

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vi /etc/hosts
20.0.0.10 www.xuhao.com

盗链网站首页配置

[root@server1 ~]# vi /var/www/html/index.html
<html><body><h1>server 2</h1><img src=http://20.0.0.10/a.jpg / ></body></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模块进行参数优化

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.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、付费专栏及课程。

余额充值