文章目录
- Nginx 优化与防盗链
- 一、隐藏版本号
- 1.1 修改配置文件方式
- 1.1.1 操作步骤
- 1.2 修改源码方式
- 1.2.1 操作步骤
- 二、修改用户与组
- 2.1 操作步骤
- 三、缓存时间
- 3.1 操作步骤
- 四、日志切割
- 4.1 操作步骤
- 五、连接超时
- 5.1 操作步骤
- 六、更改进程数
- 6.1 操作步骤
- 七、配置网页压缩
- 7.1 操作步骤
- 八、配置防盗链
- 8.1 操作步骤
Nginx 优化与防盗链
在日常使用和管理 Nginx 服务器时,通过一些优化手段可以提高性能和安全性。本文将详细介绍几种常见的 Nginx 优化方法,包括隐藏版本号、修改用户与组、设置缓存时间、日志切割、配置连接超时、调整进程数、网页压缩以及防盗链配置。
一、隐藏版本号
Nginx 默认会在响应头中显示版本号,可能会为服务器带来安全隐患。通过以下两种方法可以隐藏或修改 Nginx 的版本号。
1.1 修改配置文件方式
此方法通过修改 Nginx 配置文件来隐藏版本号。
1.1.1 操作步骤
- 使用以下命令打开 Nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
- 在
http
块中添加server_tokens off;
来关闭版本号的显示:http {include mime.types;default_type application/octet-stream;server_tokens off; # 关闭版本号显示... }
- 重启 Nginx 服务:
systemctl restart nginx
- 通过命令检查版本号是否已隐藏:
curl -I http://192.168.10.23
1.2 修改源码方式
此方法通过直接修改 Nginx 的源码文件来改变版本号和服务器类型。
1.2.1 操作步骤
- 编辑 Nginx 源码中的头文件:
vim /opt/nginx-1.12.0/src/core/nginx.h
- 修改版本号和服务器类型:
#define NGINX_VERSION "1.1.1" # 修改版本号 #define NGINX_VER "IIS" NGINX_VERSION # 修改服务器类型
- 重新编译并安装 Nginx:
cd /opt/nginx-1.12.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module make && make install
- 修改配置文件中的
server_tokens
设置,并重启服务:vim /usr/local/nginx/conf/nginx.conf http {include mime.types;default_type application/octet-stream;server_tokens on;... } systemctl restart nginx
- 检查修改后的版本号:
curl -I http://192.168.10.23
二、修改用户与组
为了提升 Nginx 的安全性,可以将 Nginx 运行的用户与组从默认的 root
修改为 nginx
。
2.1 操作步骤
- 编辑 Nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
- 将用户和组设置为
nginx
:user nginx nginx; # 修改用户和组为 nginx
- 重启 Nginx 服务:
systemctl restart nginx
- 通过命令检查主进程和子进程的用户是否已修改:
ps aux | grep nginx
三、缓存时间
为提升网站的响应速度,可以通过设置缓存时间来减少重复请求,尤其适用于静态内容。
3.1 操作步骤
- 编辑 Nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
- 针对图片资源设置缓存时间:
http {...server {...location / {root html;index index.html index.htm;}location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {root html;expires 1d; # 设置缓存时间为1天}...} }
- 重启 Nginx 服务:
systemctl restart nginx
- 访问测试资源,并查看响应头信息确认缓存时间:
curl -I http://www.kgc.com/wangsicong.jpg
四、日志切割
随着时间推移,Nginx 的日志文件可能会变得非常庞大。通过定期切割日志,可以避免日志文件过大影响服务器性能。
4.1 操作步骤
- 创建日志切割脚本:
vim /opt/fenge.sh
- 在脚本中添加以下内容:
#!/bin/bash day=$(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}/kgc.com-access.log-$day # 移动并重命名日志文件 kill -USR1 $(cat $pid_path) # 重建日志文件 find $logs_path -mtime +30 -exec rm -rf {} \; # 删除30天前的日志
- 赋予脚本执行权限并执行脚本:
chmod +x /opt/fenge.sh /opt/fenge.sh
- 配置定时任务,定期执行日志切割:
crontab -e 0 1 * * * /opt/fenge.sh
五、连接超时
设置连接超时可以避免长时间占用服务器资源,从而提升 Nginx 的性能。
5.1 操作步骤
- 编辑 Nginx 配置文件:
vim /usr/local/nginx/conf/nginx.conf
- 设置连接超时参数:
http {...keepalive_timeout 65 180; # 设置三次握手的超时时间client_header_timeout 80; # 设置客户端请求头的超时时间client_body_timeout 80; # 设置客户端请求体的超时时间... }
- 重启 Nginx 服务:
systemctl restart nginx
六、更改进程数
在高并发场景下,通过增加 Nginx 进程数可以提高服务器的处理能力。
6.1 操作步骤
- 查看 CPU 核数:
cat /proc/cpuinfo | grep -c "physical id"
- 编辑 Nginx 配置文件,调整进程数:
vim /usr/local/nginx/conf/nginx.conf worker_processes 2; # 设置为CPU核数的2倍 worker_cpu_affinity 01 10; # 设置进程与CPU的对应关系
- 重启 Nginx 服务:
systemctl restart nginx
七、配置网页压缩
通过压缩网页内容,可以减少传输数据的大小,节省带宽并提高用户的访问体验。
7.1 操作步骤
- 编辑 Nginx 配置文件,开启 Gzip 压缩功能:
vim /usr/local/nginx/conf/nginx.conf
- 在
http
块中加入以下配置:http {...gzip on; # 开启 gzip 压缩gzip_min_length 1k; # 最小压缩文件大小gzip_buffers 4 64k; # 设置压缩缓冲区gzip_http_version 1.1; # 设置 HTTP 版本gzip_comp_level 6; # 压缩级别gzip_vary on; # 支持前端缓存服务器存储压缩页面gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json; # 压缩类型... }
- 重启 Nginx 服务:
systemctl restart nginx
- 在浏览器中测试压缩效果,检查响应头中的
Content-Encoding: gzip
。
八、配置防盗链
防盗链可以防止他人盗用服务器上的资源,保护带宽和服务器资源。
8.1 操作步骤
-
编辑 Nginx 配置文件,添加防盗链配置:
vim /usr/local/nginx/conf/nginx.conf
-
在
server
块中添加以下配置:server {...location ~* \.(jpg|gif|swf)$ {valid_referers none blocked *.kgc.com kgc.com;if ($invalid_referer) {rewrite ^/ http://www.kgc.com/error.png; # 或使用 return 403;}}...}
-
配置完成后,重启 Nginx 服务:
systemctl restart nginx
*.kgc.com kgc.com;
if ($invalid_referer) {
rewrite ^/ http://www.kgc.com/error.png; # 或使用 return 403;
}
}
…
}
3. 配置完成后,重启 Nginx 服务:
```bash
systemctl restart nginx
- 准备好网页和资源文件,测试防盗链效果。