目录
Nginx 优化
1. 隐藏版本号
(1) 原因
(2) 版本号查看
(3) 隐藏方法
2. 修改用户与组
3. 缓存时间
4. 日志分割
5. 连接超时
6. 配置网页压缩
Nginx 优化
1. 隐藏版本号
(1) 原因
1. 隐藏版本号
(1) 原因
为了安全,如果暴露版本信息,黑客可以通过版本信息得知该版本存在的漏洞,进而对服务器进行攻击。隐藏版本信息可以避免黑客有的放矢的进行破坏。
(2) 版本号查看
① nginx -v (仅限 web 浏览器)
[root@c7-1 ~]#nginx -v nginx version: nginx/1.12.2
② curl -I
[root@c7-1 ~]#curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Fri, 08 Oct 2021 12:29:08 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 04 Oct 2021 12:54:14 GMT Connection: keep-alive ETag: "615af976-264" Accept-Ranges: bytes
(3) 隐藏方法
① 修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
......
server_tokens off; #添加此行内容,关闭版本号的显示
......
[root@localhost ~]# systemctl restart nginx
② 修改源码文件,重新编译
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
......
#server_tokens off; #注释此行内容,开启版本号的显示
......
[root@localhost ~]# vim /opt/nginx-1.12.2/src/core/nginx.h
##修改版本号和名称,可伪装成其他服务(例如 apache、mysql 等)
#define NGINX_VERSION "5.7.20"
#define NGINX_VER "mysql/" NGINX_VERSION
#重新编译
cd /opt/nginx-1.12.2/
./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-http_stub_status_module
make -j 4 && make install
systemctl restart nginx
2. 修改用户与组
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #第二行,取消注释,修改用户为nginx,组为nginx
[root@localhost nginx-1.12.0]# systemctl restart nginx
[root@localhost nginx-1.12.0]# ps aux | grep nginx
#主进程由root创建,子进程由nginx创建
root 42095 0.0 0.0 20500 628 ? Ss 23:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 42096 0.0 0.0 22948 1404 ? S 23:29 0:00 nginx: worker process
root 42103 0.0 0.0 112676 976 pts/0 R+ 23:29 0:00 grep --color=auto nginx
3. 缓存时间
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
......
server {
......
location ~ .(gif|jpg|jpeg|png|bmp|ico)$ { #新建location,以图片作为缓存对象
root html;
expires 1d; #指定缓存时间为1天,一天半为1d12h
}
......
}
......
[root@localhost nginx-1.12.0]# systemctl restart nginx
[root@c7-1 /usr/local/nginx/html]#ls
50x.html index.html index.html.bak test.jpg
[root@c7-1 /usr/local/nginx/html]#vim index.html
#加入图片
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
......
server {
......
location ~ .(gif|jpg|jpeg|png|bmp|ico)$ { #新建location,以图片作为缓存对象
root html;
expires 1d; #指定缓存时间为1天,一天半为1d12h
}
......
}
......
[root@localhost nginx-1.12.0]# systemctl restart nginx
[root@c7-1 /usr/local/nginx/html]#ls
50x.html index.html index.html.bak test.jpg
[root@c7-1 /usr/local/nginx/html]#vim index.html
#加入图片


