目录
一 .nginx 优化设置
1.1 nginx版本隐藏
方法一:在配置文件的 http{} 里添加server_tokens off;
###省略###
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
###省略###
方法二:适合安装前修改
1)修改nginx.h文件
vim /opt/nginx-1.15.9/src/core//nginx.h
###省略###
#define nginx_version 1015009
#define NGINX_VERSION "1.15.9"
#define NGINX_VER "nginx/" NGINX_VERSION
###省略###
修改成:
#define nginx_version 1015009
#define NGINX_VERSION "1.1.1" //修改1.15.9
#define NGINX_VER "IIS/" NGINX_VERSION //修改nginx
2) 重新编译安装
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
3)版本验证验证
curl -I http://20.0.0.24
1.2 设置超时时间
设置网站的超时时间,防止网站的长时间被占用
keepalive_timeout第一个参数指定了与client的keep-alive连接超时时间,服务器将会在这个时间后关闭连接。可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。
没有这个参数,nginx不会发送Keep-Alive响应头。
访问网址抓取数据报文,响应头中显示了超时时间是180秒。一般只设置Keepalive_timeout参数即可。
同时在优化后可能还会增加其它超时参数,比如Client_header_timeout参数,指定等待客户端发送请求头的超时时间,Client_body_timeout指定请求体读超时时间
修改配置文件里的http{}里面的模块
#keepalive_timeout 0;
keepalive_timeout 65 180; // 60 是服务器的断开时间,180是客户端的断开连接的时间
client_header_timeout 80;
client_body_timeout 80;
1.3 设置进程管理
1)nginx 的工作进程(s aux | grep nginx)
[root@localhost nginx-1.15.9]# ps aux | grep nginx
root 17488 0.0 0.0 20556 636 ? Ss 01:44 0:00 nginx: master process /usr/local/nginx/sbinnginx
nginx 17489 0.0 0.0 23012 1404 ? S 01:44 0:00 nginx: worker process
#有一个主进程和一个工作进程
注意:master 是不负责用户的连接请求的,真正负责用户的用户请求的是worker process
2)修改配置文件里的全局配置和 events {}
###修改全局配置
#user nobody;
worker_processes 2; // 修改工作进程数
worker_cpu_affinity 01 10; // 设置负载,使连接分均在两个进程里
###修改events {}里的配置
events {
worker_connections 1024;
} // 每个核心处理的连接数,能处理的并发请求
[root@localhost nginx]# ps aux |grep nginx
root 10565 0.0 0.0 20556 632 ? Ss 02:03 0:00 nginx: master process /usr/local/nginx/sbinnginx
nginx 10566 0.0 0.0 23012 1400 ? S 02:03 0:00 nginx: worker process
nginx 10567 0.0 0.0 23012 1400 ? S 02:03 0:00 nginx: worker process
## 两个进程同时工作
注意:在我们实际工作过程中,实际设置的工作进程为60-80个,而且不止一台服务器
1.4 nginx的压缩设置
代码解释:
gzip on; #开启gzip压缩功能
gzip_buffers 4 64k; #buffer 大小为4个16K缓冲区大小
gzip_http_version 1.1; #压缩版本
gzip_comp_level 2; #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快
gzip_min_length 1k; #压缩阈值
gzip_vary on; #选择支持vary header可以让前端的缓服务器缓存经过gzip压缩页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;
修改配置文件,在http{} 段内修改
####省略
gzip on;
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 1k;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript /text/css text/xml application/xml application/xml+rss;
####省略
抓包可以发现压缩过程,可自行上传超过1K的文件在首页,抓包可以发现压缩
1.5 nginx防盗链
1.51 win10下搭建一个盗链网站
1)先写一个网站测试首页
记事本上
###用记事本的方式写一个测试网页
<html>
<head>
<title>this is test web</title>
</head>
<boby>
<img src="www.lph.com./diaoni.jpg"/>
</boby>
</html>
把记事本的txt后缀名修改成html
修改后缀名后的网站首页
利用win10自带的网站服务器搭建网站
开启IIS功能
打开IIS页面
把首页网页复制到站点
盗链网站搭建完成
1.52 防盗链网站的搭建
防盗链网站的配置文件修改
##省略##
location = /50x.html {
root html;
}
location ~*\.(jpg|gif|swf)$ {
valid_referers none blocked *.lph.com lph.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.lph.com/error.png;
}
}
##省略
1.6 nginx缓存
1、以图片作为缓存对象,上传51xit.jpg图片到/usr/local/nginx/html的工作目录,访问
http://20.0.0.24/51xit.jpg,用wireshark工具进行抓包,查看响应报文,没有图片的缓存信息
查看报文没有缓存信息
2、修改Nginx的配置文件,在新location段加入expires 参数,指定缓存的时间,1d一天
[root@localhost~]# vi /usr/local/nginx/conf/nginx.conf
…..#####省略内容
location / {
root html;
index index.html index.htm;
}
location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
####配置解释##
expires 1d; ###指定缓存时间1天
3、重启Nginx服务,访问wireshark抓包
[root@localhost conf]# service nginx restart
找到20.0.0.24回应20.0.0.1图片报文
last-modified
expires
cache-control :max-age=86400
其中的Cahce-Control:max-age=86400表示缓存时间是86400秒,也就是缓存一天的
时间,一天之内浏览器访问这个页面,都是用缓存中的数据,而不需要向Nginx服务器重
新发出请求,减少了服务器的使用带宽。
1.7 nginx日志切割
随着Nginx运行时间的增加,产生的日志也会逐渐增加,为了方便掌握 Nginx的运行状态,需要时刻关注Nginx日志文件。太大的日志文件对监控是一个大灾难,不便于分析排查,需要定期的进行日志文件的切割。
##编写脚本/opt/fenge.sh##
把Nginx的日志文件/usr/local/nginx/logs/access.log移动到,目录/var/log/nginx下面,以当前时间做为日志文件的名称,然后用kill -USR1创建新的日志文件/usr/local/nginx/logs/access.log,最后删除30天之前的日志文件。
[root@www logs]# vi /opt/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}/51xit.top-access.log-$d ###移动并重命名日志文件
kill -USR1 $(cat $pid_path) ###重建新日志文件
find $logs_path -mtime +30 | xargs rm -rf
####执行/opt/fenge.sh,测试日志文件是否被切割###
[root@www logs] chmod +x /opt/fenge.sh ####添加权限
[root@www logs]# /opt/fenge.sh ###执行分割脚本
[root@localhost nginx]# ls /var/log/nginx
51xit.top-access.log-20180523 ###按日期分割了日志文件
[root@www~]# cat /usr/local/nginx/logs/access.log ###原来的日志文件没有了 重新创建了
###设置crontab任务,定期执行脚本自动进行日志分割。###
[root@localhost opt]# crontab -e
30 1 * * * /opt/fenge.sh