一切为了安全和体验,Nginx多种优化

一、隐藏版本号

在生产环境中如果版本号泄露,在被人恶意攻击时,会根据版本号,查找该版本存在的漏洞,直接攻击服务器。

[root@localhost ~]# curl -I http://192.168.238.150

在这里插入图片描述

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -I http://192.168.238.150

可以看到版本号已隐藏
在这里插入图片描述

二、修改源码

修改源码:修改版本号和服务名,欺骗客户端的恶意访问

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在主配置文件中打开版本号设置
在这里插入图片描述

[root@localhost ~]# vim /opt/nginx-1.15.9/src/core/nginx.h

在这里插入图片描述

[root@localhost ~]# cd /opt/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install
root@localhost nginx-1.15.9]# systemctl restart nginx
[root@localhost nginx-1.15.9]# curl -I http://192.168.238.150

在这里插入图片描述

三、修改用户和组

1、Nginx 运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制。
2、Nginx 默认使用 nobody 用户账号与组账号,虽然nobody的权限给的很低,有比较高的安全性,但是也可以修改用户和组的账号,来提升安全性。

[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.15.9]# systemctl restart nginx
[root@localhost nginx-1.15.9]# ps aux | grep nginx

在这里插入图片描述

四、设置缓存时间

设置缓存时间是为了客户端在次访问相同内容的请求时,避免重复请求,可以加快访问速度。
一般给静态页面做缓存时间,动态页面不做缓存时间。

[root@localhost nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf

添加图片识别和缓存时间一天
在这里插入图片描述
上传图片

[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html
[root@localhost html]# rz #上传图片
[root@localhost html]# ls

在这里插入图片描述

[root@localhost html]# vim index.html #进入站点配置文件

在这里插入图片描述

[root@localhost html]# systemctl restart nginx

在这里插入图片描述
Cache-Control: max-age=86400 #缓存时间(单位:s)

五、日志分割

Apache自带日志分割工具。Nginx不具备日志分割功能,用用脚本根据时间戳(mtime)分割日志并对日志进行管理。

[root@localhost html]# cd /opt
[root@localhost opt]# vim rzfg.sh

在这里插入图片描述

[root@localhost opt]# chmod +x rzfg.sh
[root@localhost opt]# ./rzfg.sh

在这里插入图片描述

[root@localhost opt]# crontab -e #创建周期性任务,每天1点执行

在这里插入图片描述

六、连接超时

用来设置请求资源和服务器返回的时间,保证一个请求占用固定时间,超出后报504超时!这样可以保证一个请求占用过长时间。默认时间65s。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

keepalive_timeout  100 #客户端连接在服务器端保持多久后退出
client_header_timeout 80 #设置读取客户端请求头数据的超时时间,如果超过这个时间客户端还没有发送完整的数据,服务器端将返回408错误
client_body_timeout 80 #设置读取客户端请求主体的超时间

七、更改进程数

在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞。运行进程数多一些,响应访问请求时,Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度,使用ps aux可以查看运行进程数的变化情况。

[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical" #查看CPU核数
8
[root@localhost ~]# ps aux | grep nginx #查看Nginx的进程信息
root      95554  0.0  0.0  20560   628 ?        Ss   09:13   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     95555  0.0  0.0  23096  1644 ?        S    09:13   0:00 nginx: worker process
root      95831  0.0  0.0 112724   984 pts/1    S+   09:34   0:00 grep --color=auto nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

worker_processes  8
#工作进程。设置为内核数或内核数的两倍
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000
#表示开启八个进程。00000001表示第一个内核,以此类推。

八、网页压缩

nginx的压缩模块提供了对文件内容压缩的功能,允许nginx服务器将传输的内容发送到客户端之前进行压缩,以节约网站带宽,提升用户的访问体验。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx 

在这里插入图片描述

九、防盗链

对于图片来说,盗链网站,如果想使用服务端网站的图片,可以直接写上服务端网站图片的链接地址,或者将服务端网站的图片通过右键另存为的方式下载到本地,然后在页面上使用。如果服务端网站不想盗链网站这么干了,那么服务端网站可以采取防盗链的措施来干这个工作,结果就是,盗链网站想请求所需要的资源,通过url的方式,获取的可能不是原来的图片了,出现404或者别的图片替代了。如果通过浏览器直接打开图片url,那么仍然有可能显示404,这就是防盗链。

服务端
[root@localhost ~]# vim /etc/hosts #添加映射

在这里插入图片描述

盗链端
[root@localhost ~]# vim /etc/hosts #添加映射

在这里插入图片描述
客户端
1、进入C:\windows\System32\drivers\etc目录下
修改hosts文件的用户权限为完全控制
在这里插入图片描述
进入hosts文件添加映射
在这里插入图片描述
域名访问网页
在这里插入图片描述

一、盗链

盗链端

[root@localhost ~]# vim /usr/local/nginx/html/index.html

在这里插入图片描述

[root@localhost ~]# systemctl restart nginx

win10客户端访问盗链端地址
在这里插入图片描述

二、防盗链

服务端
上传文件名为dog的照片

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

 location ~*\.(jpg|gif|swf)$ {
            valid_referers none blocked *.dog.com dog.com;			
            if ( $invalid_referer ) {
               rewrite ^/ http://www.dog.com/error.png;			
            }
        }
valid_referers:设置信任的网站,即能引用相应图片的网站(白名单)
none:浏览器中 Referer为空的情况,就是直接在浏览器访问图片
blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或者https://开头
if语句:如果链接的来源域名不在 valid_referers所列出的列表中, $invalid_referer为1,则执行后面的操作,即进行重写或返回403页面

客户端访问盗链端ip
在这里插入图片描述

十、FPM优化(Nginx支持PHP)

Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整

Static的方式的参数

[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf
pm = static			#将产生固定数量的fpm进程
pm.max_children=20	#指定启动的进程数量

在这里插入图片描述

Dynamic方式的参数

pm = dynamic				#pm参数:将以动态的方式产生fpm进程
pm.max_children=20 			#指定启动的进程数量最大的数量
pm.start_servers = 5		#动态方式下初始的fpm进程数量
pm.min_spare_servers = 2	#动态方式下最小的fpm空闭进程数
pm.max_spare_servers = 8	#动态方式下最大的fpm空闭进程

在这里插入图片描述
小小总结:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

events {
    worker_connections  1024; #默认每个进程的最大连接数,最大可调为65535,但一般情况下调为30000.
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值