Linux-Nginx优化与防盗链
一、隐藏版本号
1. 查看版本号
curl -I http://192.168.117.30
2. 修改配置文件
2.1 方法一
vim /usr/local/nginx/conf/nginx.conf
server_tokens off; #添加,关闭版本号
systemctl restart nginx
curl -I http://192.168.117.30
2.2 方法二
vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "8.8.8" #修改版本号
#define NGINX_VER "KKK" NGINX_VERSION #修改服务器类型
cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
vim /usr/local/nginx/conf/nginx.conf
server_tokens on;
systemctl restart nginx
curl -I http://192.168.117.30
二、修改用户与组
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #取消注释,修改用户为nginx ,组为nginx
systemctl restart nginx
ps aux | grep nginx
主进程由root创建,子进程由nginx创建
vim /usr/local/nginx/conf/nginx.conf
user root root;
systemctl restart nginx
ps aux | grep nginx
三、缓存时间
vim /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jpg|jepg|png|bmp|ico)$ { #加入新的 location,以图片作为缓存对象
root html;
expires 1d; #指定缓存时间,1天
http://192.168.117.30/kasumi.jpg
四、日志切割
vim /fenge.sh
#!/bin/bash
d=$(date -d "-1 day" "+%Y%m%d") #显示前一天的时间
logs_path="/var/log/nginx"
pid_path=`cat /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-{$d}
#重建日志文件
kill -USR1 $pid_path
#删除30天前的日志文件
find $logs_path -mtime +30 -exec rm -rf {} \;
#find $logs_path -mtime +30 |xargs rm -rf
crontab -e
0 1 * * * /root/fenge.sh
五、连接超时
vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
systemctl restart nginx
六、更改进程数
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; #修改为核数相同或者2倍
systemctl restart nginx
cat /proc/cpuinfo | grep -c "physical id" #查看cpu核数
ps aux | grep nginx #查看nginx主进程中包含几个子进程
七、配置网页压缩
vim /usr/local/nginx/conf/nginx.conf
http {
......
gzip on; #取消注释,开启gzip压缩功能
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区,大小为4个16k缓冲区
gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
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; #压缩类型,表示哪些网页文档启用压缩功能
......
}
在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络 ---> 选择 HTML、WS、其他
访问 http://192.168.117.30,双击200响应消息查看响应头中包含 Content-Encoding: gzip
八、配置防盗链
vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
valid_referers *.lic.com lic.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.lic.com/error.png;
}
}
网页准备:
Web源主机(192.168.117.30)
cd /usr/local/nginx/html
将kiki.jpg、error.png文件传到/usr/local/nginx/html目录下
vim index.html
<img src="kasumi.jpg"/>
</body>
</html>
echo "192.168.117.40 www.daotu.com" >> /etc/hosts
盗链网站主机(192.168.117.60)
cd /usr/local/nginx/html
vim index.html
......
<img src="http://www.ng.com/kasumi.jpg"/>
</body>
</html>
echo "192.168.117.30 www.ng.com" >> /etc/hosts
echo "192.168.117.60 www.daotu.com" >> /etc/hosts