Nginx优化与防盗链

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.254.10
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Wed, 08 Dec 2021 06:08:18 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 08 Dec 2021 06:03:54 GMT
Connection: keep-alive
ETag: "61b04aca-264"
Accept-Ranges: bytes

image-20211208140943169

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

image-20211208141138720

server_tokens off;

image-20211208141244607

[root@localhost init.d]# service nginx restart
[root@localhost init.d]# curl -I 192.168.254.10
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 08 Dec 2021 06:15:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 08 Dec 2021 06:03:54 GMT
Connection: keep-alive
ETag: "61b04aca-264"
Accept-Ranges: bytes

image-20211208141606811

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

#更改这一段

image-20211208141719915

#define NGINX_VERSION      "xxx"
#define NGINX_VER          "???/" NGINX_VERSION

image-20211208141844518

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
#将之前的off改为on

image-20211208142209605

[root@localhost nginx-1.12.0]# service nginx restart
[root@localhost nginx-1.12.0]# curl -I 192.168.254.10

image-20211208142828412

3.修改用户和组

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

image-20211208143104204

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

image-20211208143215205

第二行改成

image-20211208143520860

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

4.修改缓存时间

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

[root@localhost conf]# vim nginx.conf


在此处加入这一段,1d=1天=86400秒

location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
            root   html;
            expires 1d;
        }

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.254.10-$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在一段时间内保持打开状态,它们会在这段时间内占用资源,占用过多就会影响服务器的性能
  • 在企业网站中,为了s避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连
    接超时参数,实现控制连接访问时间。可以修改配置文件 nginx.conf,设置 keepalive_timeout
    超时时间。
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf


image-20211208171251341

更改并添加

image-20211208171510863

[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
客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx返回HTTP 408 (Request Timed out) 。

client_body timeout
指定客户端与服务端建立连接后发送request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTT408 ( Request Timed out ) 。

语法 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进程以保证快速响应,以处理用户的请求,避免造成阻塞

image-20211208172102807

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

image-20211208172144686

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


image-20211208172321709

设置成你机器CPU的数量

image-20211208172414060


image-20211208172549091

修改 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

image-20211208172921974

取消注释并添加

gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/png;

9.防盗链

在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济
损失,也避免了不必要的带宽浪费。Nginx 的防盗链功能也非常强大,在默认情况下,只需
要进行很简单的配置,即可实现防盗链处理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值