Nginx优化与防盗链

一、隐藏版本号

1.普通版

[root@localhost ~]#vim  /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;      (隐藏版本号)

查看信息:
方法一
[root@localhost conf]# curl -i 192.168.10.101
方法二

2.高级版 

修改源代码文件

[root@localhost ~]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# vim sre/core/nginx.h
#define nginx_version      1012000
#define NGINX_VERSION      "1.12.0"
#define NGINX_VER          "iis" NGINX_VERSION

重新安装初始化
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_stub_status_module && make && make install

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
 #server_tokens off;        (把隐藏版本号注释掉)

[root@localhost nginx-1.12.0]# nginx -s stop
[root@localhost nginx-1.12.0]# nginx 
[root@localhost nginx-1.12.0]# curl -i 192.168.10.101
HTTP/1.1 200 OK
Server: iis1.12.0

二、配置网页缓存

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

keepalive_timeout  65 180;            (65s服务器关闭连接;180s客户端断开连接)

备注:
keepalive_timeout
第一个参数指定了与客户端的 keep-alive 连接超时时间,服务器将会在这个时间后关闭连接。
第二个参数(可选)指定了在响应头 Keep-Alive: timeout=time 中的 time 值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,Nginx 不会发送 Keep-Alive 响应头。

server {      

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~ \.(gif|jpg|png|bmp)$  {
                root html;
                expires 1d;             (保存一天)
        }

备注:
\ 转义符
~:表示执行一个正则匹配,区分大小写
d:天
M:月
m:分钟
s:秒
h:小时

<利用图片进行测试>
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  error.png  index.html  logo.jpg

[root@localhost html]# vim index.html 
<html>
<body>
<h1>my picture <h1>
<img src="logo.jpg" />
</body>
</html>

测试

 三、日志切割

[root@localhost logs]# date -d "-1 day" "+%y%m%d"
240622
[root@localhost logs]# mv access.log $(date -d "-1 day" "+%y%m%d")
[root@localhost logs]# ls
240622  error.log  nginx.pid

[root@localhost logs]# systemctl restart nginx
[root@localhost logs]# ls
240622  access.log  error.log  nginx.pid

重启
[root@localhost logs]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12489/nginx: master 
[root@localhost logs]# kill -USR1 12489
     
备注:
       kill -USR1 :停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改。

四、网页压缩


[root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf
http {
     gzip  on;         #去掉gzip  on;前面的注释,增加其他的参数
    gzip_buffers 4 64k;
    gzip_http_version 1.1;

    gzip_comp_level 2;
    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/jpeg image/gif image/png;

[root@www ~]# systemctl restart nginx

备注:

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

测试


 

  • 23
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值