NGINX 优化与防盗链

操作步骤

1.安装nginx

[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# vim nginx
#!/bin/bash
#chkconfig: 35 99 20
#descripton:Nginx Service Control Script
cmd="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$cmd
;;

stop)
kill -3 `cat $pid`
;;

restart)
$0 stop
$0 start
;;

reload)
kill -1 `cat $pid`
;;

*)
echo "please input start,stop,reload,restart:"
exit 0
;;
esac
exit 1

[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx

2.隐藏版本号

[root@localhost init.d]# service nginx start
[root@localhost init.d]# curl -I 192.168.133.100
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Wed, 10 Nov 2021 06:41:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 10 Nov 2021 06:17:31 GMT
Connection: keep-alive
ETag: "618b63fb-264"
Accept-Ranges: bytes

在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

[root@localhost init.d]# service nginx restart
[root@localhost init.d]# curl -I 192.168.133.100
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Nov 2021 07:22:36 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 10 Nov 2021 06:17:31 GMT
Connection: keep-alive
ETag: "618b63fb-264"
Accept-Ranges: bytes

在这里插入图片描述

[root@localhost init.d]# vim /opt/nginx-1.12.0/src/core/nginx.h

在这里插入图片描述
改成
在这里插入图片描述

[root@localhost init.d]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install

在这里插入图片描述
更改为
在这里插入图片描述

[root@localhost nginx-1.12.0]# service nginx restart
[root@localhost nginx-1.12.0]# curl -I http://192.168.133.100

在这里插入图片描述

3.修改用户与组

[root@localhost init.d]# service nginx start
[root@localhost init.d]#  ps aux |grep nginx

在这里插入图片描述

[root@localhost conf]# useradd -s /sbin/nologin zhy
[root@localhost init.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

在这里插入图片描述

第二行改成
在这里插入图片描述

[root@localhost conf]# service nginx restart
[root@localhost conf]# ps aux |grep zhy
zhy       47188  0.0  0.0  23028  1384 ?        S    16:02   0:00 nginx: worker process
root      47190  0.0  0.0 112660   972 pts/0    S+   16:02   0:00 grep --color=auto zhy


4.修改缓存时间

当nginx 将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度一般针对静态网页设置,对动态网页不设置缓存时间。

[root@localhost conf]# vim nginx.conf

在此处加入这一段,1d=1天=86400秒
在这里插入图片描述

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# service nginx restart

网页上查看已经更改成功
在这里插入图片描述
其中的 Cahce-Control:max-age=86400 表示缓存时间是 86400 秒,也就是缓存一天的
时间,一天之内浏览器访问这个页面,都是用缓存中的数据,而不需要向 Nginx 服务器重
新发出请求,减少了服务器的使用带宽。

5.日志切割

随着 Nginx 运行时间的增加,产生的日志也会逐渐增加,为了方便掌握 Nginx 的运行
状态,需要时刻关注 Nginx 日志文件。太大的日志文件对监控是一个大灾难,不便于分析
排查,需要定期的进行日志文件的切割。
需要写脚本

[root@localhost nginx]# cd logs
[root@localhost logs]# ls
access.log  error.log  nginx.pid
[root@localhost logs]# vim log.sh
#!/bin/bash
d=`date +%F -d -1day`
path="/var/log/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
[ -d $path ] || mkdir -p $path
mv /usr/local/nginx/logs/access.log ${path}/192.168.133.100-$d
kill -USR1 $(cat $pid)
find $path -mtime +30 -delete
[root@localhost logs]# chmod +x log.sh 
[root@localhost logs]# bash log.sh 
[root@localhost nginx]# ls
192.168.133.100-  192.168.133.100-2021-11-09

6.连接超时设置

HTTP服务有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自同一客户端的其他请求,服务端会利用这个未被关闭的连接,而不需要再次建立一个连接
KeepAlive在一段时间内保持打开状态,它们会在这段时间内占用资源,占用过多就会影响服务器的性能
在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连
接超时参数,实现控制连接访问时间。可以修改配置文件 nginx.conf,设置 keepalive_timeout
超时时间。

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

在这里插入图片描述
更改并添加
在这里插入图片描述

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart

在这里插入图片描述

指定KeepAlive的超时时间(timeout)〉。指定每个tcP连接最多可以保持多长时间,服务器将会在这个时间后关闭连接。
Nginx的默认值是65秒,有些浏览器最多只保持60秒,所以可以设定为60秒。若将它设置为0,就禁止了keepalive连接。
第二个参数(可选的)指定了在响应头Keep-Alive:timeout=time中的time值3这个头能够让一些浏览器主动关闭连接,这样服务签就不必去关闭连接了。没有这个参数,Nginx不会发送 Keep-Alive响应头。

语法 client_header_timeout time
默认值 60s
上下文 http server(指可以放在http块和server块)
说明 指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,
才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

语法 client_body_timeout time
默认值 60s
上下文 http server location
说明 该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,
就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

7.更改请求进程数

在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞

[root@localhost conf]# lscpu

在这里插入图片描述

[root@localhost conf]# ps aux|grep nginx

在这里插入图片描述

[root@localhost conf]# !vim                                                                    运行上次vim的程序
vim nginx.conf

在这里插入图片描述
设置成你机器CPU的数量
在这里插入图片描述

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart
[root@localhost conf]# ps aux|grep nginx

在这里插入图片描述
修改 Nginx 的配置文件的 worker_processes 参数,一般设为 CPU 的个数或者核数,
在高并发的情况下可设置为 CPU 个数或者核数的 2 倍,可以查看 CPU 的核数以确定参数

root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 01 10 0100 1000;
#设置每个进程由不同cpu处理,进程数配为4时,0001 0010 0100 1000

8.网页压缩

Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能

允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装可在配置文件中加入相应的压缩功能参数对压缩性能进行优化

gzip on:开启 gzip 压缩输出;
gzip_min_length 1k:用于设置允许压缩的页面最小字节数;
gzip_buffers 4 16k:表示申请 4 个单位为 16k 的内存作为压缩结果流缓存,默认值
是申请与原始数据大小相同的内存空间来存储 gzip 压缩结果;
Zip_http_version 1.0:用于设置识别 http 协议版本,默认是 1.1,目前大部分浏览
器已经支持 gzip 解压,但处理最慢,也比较消耗服务器 CPU 资源;
Gzip_comp_level 2:用来指定 gzip 压缩比,1 压缩比最小,处理速度最快;9 压缩
比最大,传输速度快,但处理速度最慢,使用默认即可;
Gzip_vary on:选项可以让前端的缓存服务器缓存经过 gzip 压缩的页面
Gzip_types text/plain:压缩类型,是对哪些网页文档启用压缩功能;
[root@localhost conf]# vim 、usr/local/nginx/conf/nginx.conf

在这里插入图片描述
取消注释并添加
在这里插入图片描述

[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# service nginx restart

在这里插入图片描述

9.防盗链

在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济
损失,也避免了不必要的带宽浪费。Nginx 的防盗链功能也非常强大,在默认情况下,只需
要进行很简单的配置,即可实现防盗链处理。
~** .(jpg|gif|swf)$:这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的
文件
Valid_referers:设置信任的网站,可以正常使用图片。
None :浏览器中 referer 为空的情况,就是直接在浏览器访问图片。
Blocked :referer 不为空的情况 ,但是值被代理或防火墙删除了,这些值不以 http://或
https://开头。
后面的网址或者域名:referer 中包含相关字符串的网址。
If 语句:如果链接的来源域名不在 valid_referers 所列出的列表中,$invalid_referer 为*
1,则执行后面的操作,即进行重写或返回 403 页面。

找台新机器

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y httpd

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html

在这里插入图片描述

注意这里的图片是你nginx里HTML文件夹里正确图片的类型,因为盗用的是你正常显示的图片

[root@localhost html]# vim /etc/hosts

在这里插入图片描述

[root@localhost html]# systemctl start httpd

在这里插入图片描述
主机上输入

[root@localhost html]# vim index.html 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
增加以下一段
在这里插入图片描述

在网页文件夹里添加一个错误的图

[root@localhost html]# ls
123.jpg  50x.html  error.png  index.html

这样别人就再也无法盗用你网页上的图片了
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值