标签:nginx 优化 server1 Nginx 0.0 日志 root 防盗链
目录
一、Nginx优化
1.1配置Nginx隐藏版本号
- 隐藏Nginx版本号,避免安全漏洞泄露
- Nginx隐藏版本号的方法
未隐藏版本号前使用curl -I 网址
命令检测结果
[root@server1 ~]# curl -I 10.0.0.10
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 24 Nov 2020 17:01:39 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5
Last-Modified: Tue, 24 Nov 2020 15:55:42 GMT
Connection: keep-alive
ETag: "5fbd2cfe-5"
Accept-Ranges: bytes
1.1.1修改配置文件法
[root@server1 ~]# vim /etc/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; ##关闭版本号
[root@server1 ~]# systemctl restart nginx
[root@server1 ~]# curl -I 10.0.0.10
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Nov 2020 17:04:26 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5
Last-Modified: Tue, 24 Nov 2020 15:55:42 GMT
Connection: keep-alive
ETag: "5fbd2cfe-5"
Accept-Ranges: bytes
1.1.2修改源码法(修改源码法要在编译之前)
[root@server1 ~]# vi /opt/nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "8.8.8"
#define NGINX_VER "nginx/" NGINX_VERSION
[root@server1 nginx-1.12.2]# make && make install
[root@server1 nginx-1.12.2]# systemctl restart nginx
[root@server1 nginx-1.12.2]# curl -I 10.0.0.10
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 24 Nov 2020 17:08:33 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 5
Last-Modified: Tue, 24 Nov 2020 15:55:42 GMT
Connection: keep-alive
ETag: "5fbd2cfe-5"
Accept-Ranges: bytes
1.2修改Nginx用户与组
- Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制
- Nginx默认使用nobody用户账号与组账号
- 修改的方法
编译安装时指定用户与组
1.创建用户账号与组账号,如nginx
[root@server1 ~]# useradd -M -s /sbin/nologin nginx
2.编译安装--user与--group指定Nginx服务的运行用户与组账户
[root@server1 nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
修改主配置文件指定用户与组
[root@server1 ~]# vi /etc/nginx
# user nobody修改成nginx nginx
user nginx nginx
[root@server1 ~]# ps aux | grep nginx
root 4068 0.0 0.0 20500 612 ? Ss 01:15 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4069 0.0 0.0 23028 1388 ? S 01:15 0:00 nginx: worker process
root 4085 0.0 0.0 112676 984 pts/0 S+ 01:16 0:00 grep --color=auto nginx
1.3配置Nginx网页缓存时间
- 当nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。
- 一般针对静态网页设置,对动态网页不设置缓存时间
- 设置方法
在主配置文件的location段加入expires参数
[root@server1 ~]# vi /etc/nginx
...省略内容
location / {
root html;
index index.html index.htm;
expires 1d; ###设置缓存时间为1天
}
1.4实现Nginx的日志切割
- 随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注Nginx日志文件
- 太大的日志文件对监控是一个大灾难,所以需要定期进行日志文件的切割
- Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Linux的计划任务周期性地进行日志切割
- 编写脚本进行日志切割的思路
1.设置时间变量
2.设置保存日志路径;
3.将目前的日志文件进行重命名
4.删除时间过长的日志文件
5.设置cron任务,定期执行脚本自动进行日志分割
[root@server1 ~]# vi /etc/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@server1 ~]# vim /opt/fenge.sh
#!/bin/bash
#设置时间和日志名称的变量
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
#通过重载生成新日志(平滑的读取新配置文件,不必重启nginx)
kill -HUP $(cat $pid_path)
#删除30天前的日志(echo '--help' | xargs cat 等价于 cat --help)
find $logs_path -mtime +30 | xargs rm -rf
[root@server1 ~]# chmod +x fenge.sh
[root@server1 ~]# ./fenge.sh
[root@server1 ~]# crontab -e
0 1 * * * /opt/fenge.sh
- 日期相关命令
[root@server1 ~]# date -s 2019-12-25 #可以把当前的时间改为2019.12.25
[root@server1 ~]# date +%Y%m%d # 获取当天的的日期
201124
[root@server1 ~]# date
2020年 11月 24日 星期二 23:30:51 CST
[root@server1 ~]# date -d "-1 day" # 昨天
2020年 11月 23日 星期一 23:32:02 CST
[root@server1 ~]# date -d "day" # 明天
2020年 11月 25日 星期三 23:32:31 CST
[root@server1 ~]# date -d "-1 day" +%d # 前一天的日期
23
[root@server1 ~]# date -d "-h hour" +%H # 前一小时
22
[root@server1 ~]# date -d "-1 min" +%M # 前一分钟
34
[root@server1 ~]# date -d "-1 second" +%S # 前一秒钟
33
1.5实现连接超时
- 为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
- Nginx使用keepalive_timeout来指定KeepAlive的超时时间(timeout)
- 指定每个TCP连接最多可以保持多长时间。Nginx的默认值是65秒,有些浏览器最多只保持60秒,
若将它设置为0,就禁止了keepalive连接。
[root@server1 ~]# vi /etc/nginx
http {
.....省略内容
#keepalive_timeout 0;
keepalive_timeout 65 200; #以后者为准,可以在http\server\location设置
client_header_timeout 80; #等待客户端发送请求头的超时时间超时会发送408错误
client_body_timeout 80; #设置客户端发送请求体超时时间
}
[root@server1 ~]# systemctl restart nginx
1.6更改Nginx运行进程数
- 在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。
- 修改配置文件的worker_processes参数
一般设为CPU的个数或者核数
在高并发情况下可设置为CPU个数或者核数的2倍 - 增加进程数,可减少了系统的开销,提升了服务速度
- 使用ps aux查看运行进程数的变化情况
[root@server1 ~]# cat /proc/cpuinfo | grep -c "physical id" ##查看物理CPU的个数
4
[root@server1 ~]# vim /etc/nginx.conf
worker_processes 4 ##将工作进程数设为4
[root@server1 ~]# systemctl restart nginx
[root@server1 ~]# ps aux | grep nginx
root 11121 0.0 0.0 20500 608 ? Ss 23:53 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 11122 0.0 0.0 23028 1384 ? S 23:53 0:00 nginx: worker process
nginx 11123 0.0 0.0 23028 1384 ? S 23:53 0:00 nginx: worker process
nginx 11124 0.0 0.0 23028 1384 ? S 23:53 0:00 nginx: worker process
nginx 11125 0.0 0.0 23028 1384 ? S 23:53 0:00 nginx: worker process
root 11129 0.0 0.0 112676 980 pts/0 S+ 23:54 0:00 grep --color=auto nginx
1.7Nginx网页压缩
1.主配置文件中找到gzip on添加如下参数
[root@server1 ~]# vim /etc/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/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\."; ##配置禁用gzip条件,支持正则,表示ie6以下不启用
gzip_vary on; ##选择支持vary header可以让前端的缓存服务器缓存经过gzip压缩的页面
1.8FPM 参数优化
Nginx 的PHP解析功能实现如果是交给FPM 处理的,为了提高PHP的处理速度,可对FPM 模块进行参数的调整。
1、首先安装带有 FPM模块的PHP 环境,保证PHP 可以正常运行;
2、FPM 进程有两种启动方式,由pm 参数指定,分别是 static 和dynamic,前者将产生固定的 fpm进程,后者将以动态的方式产生 fpm进程。
3、FPM优化参数讲解:
- Static的方式的参数
pm.max_children: 指定启动的进程数量 - Dynamic方式的参数
pm.max_children:指定启动的进程数量最大的数量
pm.start_servers:动态方式下初始的fpm进程数量
pm.min_spare_servers: 动态方式下最小的fpm空闭进程数
pm.max_spare_servers:动态方式下最大的fpm空闭进程数
[root@server1 ~]# vi /usr/local/php/etc/php-fpm.d/www.conf
pid = run/php-fpm.pid
pm = dynamic
pm.max_children=20 #static模式下空闲进程数上限,大于下面的值;
pm.start_servers = 5 #动态方式默认开启的进程数,在最小和最大之间
pm.min_spare_servers = 2 #动态方式下最少空闲进程数
pm.max_spare_servers = 8 #动态方式下最大空闲进程数
二、Nginx防盗链配置
和Apache一样,在企业网站服务当中,一般都要配置防盗链,以避免网站内容被非法盗用,造成经济损失和带宽浪费。
Nginx 的防盗链功能十分强大,在默认的情况下,进行简单的配置,即可实现防盗链。
1.源主机配置
源主机的域名:www.test.com
做www.test.com–》10.0.0.20的域名解析
首页站点中放入一张image.jpg图片
2.盗链网站首页配置
[root@server1 ~]# vim /usr/local/nginx/html/index.html
...省略内容
<img src="http://www.test.com/image.jpg"/>
3.win10客户端访问盗链网站服务器的地址进行测试
4.源主机防盗链配置
[root@server1 html]# vim /etc/nginx.conf
...省略内容
[root@localhost ~]# vi /etc/nginx
location ~*\.(gif|jpg|swf)$ { # 这段正则表达式,表示不区分大小写,以jpg、gif、swf结尾的文件;
valid_referers none blocked *.test.com test.com; # 信任以test.com域名为结尾的网站访问
if ($invalid_referer) {
rewrite ^/ http://www.test.com/error.png; # 如果链接的来源域名不在valid_referers 的所列中,则执行后面的操作,即把error.png放到源主机工作目录下;
}
}
[root@localhost ~]# systemctl restart nginx
# 在Windows里的hosts里添加
10.0.0.20 www.test.com
5.win10客户端再次访问盗链网站服务器的地址进行测试
访问域名www.test.com
标签:nginx,优化,server1,Nginx,0.0,日志,root,防盗链 来源: https://blog.csdn.net/huhaiyangFIVE/article/details/110087359
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。