Nginx优化与防盗链

隐藏版本号

1. 修改配置文件法

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

[root@localhost ~]# curl -I http://192.168.200.40
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 01 Dec 2020 11:28:09 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 612
Last-Modified: Tue, 01 Dec 2020 08:15:24 GMT
Connection: keep-alive
ETag: "5fc5fb9c-264"
Accept-Ranges: bytes

修改配置文件

[root@localhost ~]# 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.200.40
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 01 Dec 2020 11:33:23 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 612
Last-Modified: Tue, 01 Dec 2020 08:15:24 GMT
Connection: keep-alive
ETag: "5fc5fb9c-264"
Accept-Ranges: bytes


在这里插入图片描述

2. 修改源码并重新编译安装

[root@localhost ~]# vi /etc/nginx.conf 

server_tokens on;  ##将off修改为on


[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.200.40
HTTP/1.1 200 OK
Server: IIS/1.1.1
Date: Tue, 01 Dec 2020 11:40:58 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 612
Last-Modified: Tue, 01 Dec 2020 08:15:24 GMT
Connection: keep-alive
ETag: "5fc5fb9c-264"
Accept-Ranges: bytes

在这里插入图片描述

3.修改Nginx用户和组

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

①编译安装时指定用户与组

[root@server1 ~]# cd nginx-1.12.2/
[root@server1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 

②修改配置文件法指定

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx;

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# ps aux | grep nginx ###查看进程信息

Nginx网页缓存时间

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

设置方法

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls -lh
[root@localhost html]# vi index.html 

  <img src="1.jpg" />    ##添加图片


[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日志切割

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 123.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}/123.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 ~]# ./123.sh
[root@localhost ~]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 44
-rw-r--r--. 1 root root 44866 1201 20:11 123.com-access.log-20201130 ###运行之后生成昨天的日志

关于日期命令:

###获取当天的的日期 date +%Y%m%d 或 date

昨天: date -d “-1 day”

明天: date -d “day”

前一天的日期: date -d “-1 day” +%d

前一小时: date -d “-1 hour” +%H

前一分钟: date -d “-1 min” +%M

前一秒钟: date -d “-1 second” +%S

Nginx实现连接超时

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

[root@localhost ~]# vi /etc/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   20:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1031  0.0  0.0  22948  1408 ?        S    20:15   0:00 nginx: worker process
nginx      1032  0.0  0.0  22948  1408 ?        S    20:15   0:00 nginx: worker process
nginx      1033  0.0  0.0  22948  1408 ?        S    20:15   0:00 nginx: worker process
nginx      1034  0.0  0.0  22948  1408 ?        S    20:16   0:00 nginx: worker process
root       1731  0.0  0.0 112680   980 pts/0    S+   20:16   0:00 grep --color=auto nginx

Nginx实现网页压缩功能

修改配置文件

[root@localhost ~]# vi /etc/nginx.conf 

 gzip  on; ###开启gzip压缩功能
    gzip_min_length 1k; ###压缩阈值(超过1k的文件进行压缩)
    gzip_buffers 4 16k; ###buffer(缓冲)大小为416k缓冲区大小
    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=1.jpg / >
</body>
</html>

[root@localhost ~]# systemctl restart nginx

Nginx防盗链

源主机配置
[root@localhost ~]# vi /etc/hosts
192.168.200.40  www.123.com


[root@localhost ~]# cd /usr/local/nginx/html   #到/usr/local/nginx/html/下,
[root@localhost html]# rm-rf index.html  #删除原index.html文件,

添加新index.html文件,到此目录下。



添加配置
 location ~*\.(gif|jpg|swf)$ {
             valid_referers none blocked *.123.com 123.com 192.168.200.40;
             if ($invalid_referer) {
             rewrite ^/ http://192.168.200.40/error.png;
             }
    }

在这里插入图片描述

在这里插入图片描述

在另一台主机上进行盗链操作

[root@localhost ~]# yum -y install httpd

源主机防盗链配置
[root@localhost ~]# vi 
/var/www/html/index.html

<html><body><h1>en???</h1><img src=http://192.168.200.40/1.jpg / ></body></html>

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# vi /etc/hosts

末尾添加映射
192.168.200.40   www.123.com

在浏览器上测试http://192.168.200.40
在这里插入图片描述

FPM模块参数优化

1、Nginx 的 PHP 解析功能实现如果是交由 FPM 处理的,为了提高 PHP 的处理速度,可对
FPM 模块进行参数跳转。

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

3、优化参数调整
FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程存在

4、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 空闲进程数

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值