nginx优化

1、隐藏版本
在生产环境中,需要隐藏Ngnx的版本号,以避免安全漏洞的泄漏
①配置文件的方法nginx.conf
②修改源码法

2、改用户和组操作
3、日志切割操作
4、进程数操作
5、网页压缩操作

首先我们查看一下
本地: curl -I http://192.168.10.21头部信息
nginx优化_第1张图片
浏览器:可以直接在浏览器(谷歌)一-》 开发者工具,选择network- 》重新加载页面一》选择请求一 >》 选择headlers–》查看版本。
vim /usr/local/nginx/conf/nginx.conf
http {
include mime. types;
default_type application/octet- stream;
server_ tokens off; ##添加,关闭版本号
}

systemctl restart nginx

然后就是查看curl -I http://192.168.10.21
nginx优化_第2张图片
nginx优化_第3张图片

二、修改源码

#src:
基本所有的配置看到src就是放源码的位置
vim /opt/nqinx-1.12.2/src/core/nginx.h
#define nginx_ version 1012000
#define NGINX VERSION “1.0.0” #将原始的1.15.9修改为1.0.0
#define NGINX VER “IIS” NGINX VERSION #将原始的Nginx修改为IIS
-》 wq
nginx优化_第4张图片
nginx优化_第5张图片

#重新编译安装
cd /opt/nginx-1.12.2
./configure
–prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_stub_status_module

make && make install

#将方法一中关闭的版本号重新打开
vim /usr/local/nginx/conf/nginx.conf
http {
include mime. types;
default_ type appl ication/octet- stream; I
server tokens on; #打开
nginx优化_第6张图片

#重启服务
systemctl restart nginx.service

#查看版本号是否隐藏
curl -I http://192.168.200.50/

########################修改用户和组#######################
若没有安装前创建用户,则在此服务中默认使用的是nobody

vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #将前面的#注释掉,然后修改用户与组为nginx (身份)
worker processes 1;
nginx优化_第7张图片

PS: 需要chown 给与属组属主

systemctl restart nginx.service
ps aux| grep nginx #查看用户与组是否修改成功

在这里插入图片描述

设置缓存时间
vim /usr/local/nginx/conf/nginx.conf
#修改主配置文件

http {
include mime.types;
default_ type application/ octet- stream;
server tokens on;

location / {
root html;
index index.html index. htm;
}

location~ \.(gif|jpg|jepg|bmp|ico)$ {		#添加图片识别
	root		html;
	expires 	1d;		#设置缓存时间为1天
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/c55de7676d174f899f7c540b4ff3c1b9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAU2xkeTA3MDE=,size_20,color_FFFFFF,t_70,g_se,x_16)

--》wq

#_上传rabbit. jpg图片、修改站点文件
cd /usr/ local/ nginx/html
#修改index. html
vim index.html
14 

Welcome to nginx!

15 15行插入识别图片 -》wq ![在这里插入图片描述](https://img-blog.csdnimg.cn/e1f77fe7eea342569d3f4c4570085402.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAU2xkeTA3MDE=,size_20,color_FFFFFF,t_70,g_se,x_16) #测试: 网页访问 or curl -I http://192.168.226.132/rabbit.jpg ![在这里插入图片描述](https://img-blog.csdnimg.cn/85bef4c22ee146d7b934ca94bbeb034b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAU2xkeTA3MDE=,size_20,color_FFFFFF,t_70,g_se,x_16)

你可能感兴趣的:(linux,运维)