- 一、Nginx 优化
- 1. 隐藏版本号
- (1) 隐藏版本号的原因
- (2) 版本号查看
- ① nginx -v (仅限 web 浏览器)
- ② curl -I
- ③ 浏览器查看
- (3) 隐藏方法
- ① 修改配置文件
- ② 修改源码文件,重新编译
- 2. 修改用户与组
- 3. 缓存时间
- 4. 日志分割
- 5. 连接超时
- 6. 更改进程数
- 7. 配置网页压缩
- 二、盗链与防盗链
- 1. 盗链
- 1.1 nginx 服务端配置
- 1.2 盗链主机配置
- 1.3 windows 测试机配置
- 1.4 windows 访问测试
- 2. 防盗链
- 2.1 nginx 服务端配置
- 2.2 windows 访问测试
- 三、FPM 参数优化
为了安全,如果暴露版本信息,黑客可以通过版本信息得知该版本存在的漏洞,进而对服务器进行攻击。隐藏版本信息可以避免黑客有的放矢的进行破坏。
(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
#隐藏成功 [root@c7-1 ~]#nginx -v nginx version: mysql/5.7.202. 修改用户与组
[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 nginx3. 缓存时间
当 nginx 将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度。一般针对静态网页进行设置,对动态网页不设置缓存时间。
[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 #加入图片



