Nginx的优化与防盗链,无名宵小速速退让!

2024-02-13 11:59

本文主要是介绍Nginx的优化与防盗链,无名宵小速速退让!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 1. Nginx隐藏版本号
    • 1.1 修改配置文件
    • 1.2 修改源码
  • 2. 修改用户和组
  • 3. 设置缓存时间
  • 4. 日志分割
  • 5. 实现连接超时
  • 6. 更改进程数
  • 7. 网页压缩
  • 8. 盗链与防盗链
    • 8.1 盗链
    • 8.2 防盗链
  • 9. FPM模块参数优化

1. Nginx隐藏版本号

1.1 修改配置文件

[root@client ~]# curl 192.168.152.129
[root@client ~]# curl -I http://192.168.152.129
HTTP/1.1 200 OK
Server: nginx/1.15.9
Date: Thu, 24 Jun 2021 01:51:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 16:39:04 GMT
Connection: keep-alive
ETag: "60d21228-264"
Accept-Ranges: bytes
[root@client ~]# vim /usr/local/nginx/conf/nginx.confhttp {include       mime.types;default_type  application/octet-stream;server_tokens off;#关闭版本号[root@client ~]# systemctl restart nginx
[root@client ~]# curl -I http://192.168.152.129
HTTP/1.1 200 OK
Server: nginx
#这里可以看到是隐藏了版本号,看不到准确的数字
Date: Thu, 24 Jun 2021 03:30:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 16:39:04 GMT
Connection: keep-alive
ETag: "60d21228-264"
Accept-Ranges: bytes

1.2 修改源码

[root@client ~]# vim /opt/nginx-1.15.9/src/core/nginx.h#define nginx_version      1015009
#define NGINX_VERSION      "1.15.9"
#define NGINX_VER          "nginx/" NGINX_VERSION将上面的原有内容稍加更改,即可
#define nginx_version      1015009
#define NGINX_VERSION      "1.0.0"
#define NGINX_VER          "apache/" NGINX_VERSION进行重新编译
[root@client ~]# cd /opt/nginx-1.15.9/
[root@client nginx-1.15.9]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module[root@client nginx-1.15.9]# make && make install
[root@client nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.confhttp {include       mime.types;default_type  application/octet-stream;server_tokens on;#打开版本号[root@client nginx-1.15.9]# systemctl restart nginx
[root@client nginx-1.15.9]# curl -I http://192.168.152.129
HTTP/1.1 200 OK
Server: apache/1.0.0
#这里就可以很明确的看到版本号的名称+版本数字
#不是之前的nginx
Date: Thu, 24 Jun 2021 03:40:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 22 Jun 2021 16:39:04 GMT
Connection: keep-alive
ETag: "60d21228-264"
Accept-Ranges: bytes

2. 修改用户和组

Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制

[root@client nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf#user  nobody;
worker_processes  1;
#默认是Nginx默认使用nobody用户账号与组账号
#修改成如下内容user  nginx nginx;
worker_processes  1;
#修改属组属主,重启服务,查看服务状态
[root@client nginx-1.15.9]# systemctl restart nginx
[root@client nginx-1.15.9]# ps aux | grep nginx
root     109662  0.0  0.0  20560   624 ?        Ss   12:10   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    109663  0.0  0.0  23096  1392 ?        S    12:10   0:00 nginx: worker process
root     109667  0.0  0.0 112724   988 pts/4    S+   12:10   0:00 grep --color=auto nginx

3. 设置缓存时间

[root@client nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf45         location / {46             root   html;47             index  index.html index.htm;48         }49         50         location ~ \.(gif|jpg|jepg|bmp|ico)$ {#添加图片识别     51          root   html;      52          expires 1d; #设置缓存时间为1天      53      }  [root@client nginx-1.15.9]# cd /usr/local/nginx/html
[root@client html]# vim index.html 14 <h1>Welcome to nginx!</h1>
15 <img src="1.jpg"</h1>#事先要记得将图片先导入目录下面
[root@client html]# ls
1.jpg  2.jpg  3.jpg  50x.html  index.html[root@client html]# systemctl restart nginx

效果如下:

4. 日志分割

日志储存为一个文件会越来越大不方便查看,下面我们设置成按天分割日志,即第二天会产生新的日志文件,记录的是今天的日志

第一步:编写日志分割的功能脚本

[root@client html]# vim /opt/fenge.sh#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d") 
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path 
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -HUP $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf[root@client html]# chmod +x /opt/fenge.sh[root@client html]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@client html]# crontab -l
0 1 * * * /opt/fenge.sh[root@client html]# systemctl restart nginx
[root@client html]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      111019/nginx: maste 
[root@client opt]# bash -x fenge.sh 
++ date -d '-1 day' +%Y%m%d
+ d=20210623
+ logs_path=/var/log/nginx
+ pid_path=/usr/local/nginx/logs/nginx.pid
+ '[' -d /var/log/nginx ']'
+ mv /usr/local/nginx/logs/access.log /var/log/nginx/test.com-access.log-20210623
++ cat /usr/local/nginx/logs/nginx.pid
+ kill -HUP 111019
+ find /var/log/nginx -mtime +30
+ xargs rm -rf
[root@client opt]# ls /var/log/nginx/
test.com-access.log-20210623
#可以看到前一天的日志
[root@client opt]# date -s 20210625
2021年 06月 25日 星期五 00:00:00 CST
[root@client opt]# ./fenge.sh 
[root@client opt]# ls /var/log/nginx/
test.com-access.log-20210623  test.com-access.log-20210624
[root@client opt]# date
2021年 06月 25日 星期五 00:00:15 CST

5. 实现连接超时

避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间

[root@client opt]# vim /usr/local/nginx/conf/nginx.conf32     #keepalive_timeout  0;
33     keepalive_timeout  100;
34     client_header_timeout 80;
35     client_body_timeout 80;
[root@client opt]# 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

6. 更改进程数

[root@client opt]# cat /proc/cpuinfo | grep -c "physical"
8
[root@client opt]# ps aux | grep nginx
root     111019  0.0  0.0  20600  1496 ?        Ss   6月24   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    113972  0.0  0.0  23112  1428 ?        S    01:00   0:00 nginx: worker process
root     114265  0.0  0.0 112724   988 pts/1    S+   01:24   0:00 grep --color=auto nginx[root@client ~]# vim /usr/local/nginx/conf/nginx.conf2 #user  nobody;
3 worker_processes  2;
4 worker_cpu_affinity 01 10;

7. 网页压缩

[root@client ~]# vim /usr/local/nginx/conf/nginx.conf37     #gzip  on;38     gzip  on; 39     gzip_min_length 1k;40     gzip_buffers 4 16k;41     #gzip_http_version 1.1;  42     gzip_comp_level 6;    43     gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg     image/png image/gif application/xml text/javascript application/x-httpd-php appli    cation/javascript application/json;44     gzip_disable "MSIE [1-6]\.";45     gzip_vary on;       [root@client ~]# cd /usr/local/nginx/html
[root@client html]# vim index.html <h1>Welcome to nginx!</h1>
<img src="3.jpg"</h1>[root@client html]# systemctl stop nginx
[root@client html]# systemctl start nginx



8. 盗链与防盗链

盗链:盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。

防盗链:WEB应用防火墙通过实现URL级别的访问控制,对客户端请求进行检测,如果发现图片、文件等资源信息的HTTP请求来自于其它网站,则阻止盗链请求,节省因盗用资源链接而消耗的带宽和性能。

8.1 盗链

配置环境:
服务端:192.168.152.129
盗链端:192.168.152.130
Win10:192.168.152.1

服务端添加映射:
[root@client html]# vim /etc/hosts
192.168.152.129 www.1.com盗链端添加映射:
[root@server ~]# vim /etc/hosts
192.168.152.130 daolian
192.168.152.129 www.1.com
192.168.152.152 www.benet.com添加图片:
[root@server ~]# vim /usr/local/nginx/html/index.html14 <h1>Welcome to nginx!</h1>15 <img src="http://www.1.com/1.jpg"/>




win10测试一下访问服务端网页地址

win10测试一下访问盗链端网页地址

8.2 防盗链

服务端:

[root@client ~]# cd /usr/local/nginx/html
[root@client html]# ls
1.jpg  2.jpg  3.jpg  50x.html  index.html
[root@client html]# cp 2.jpg 2.png
[root@client html]# ls
1.jpg  2.jpg  2.png  3.jpg  50x.html  index.html
#这边需要进行复制一下,图片格式不一样,为了防盗链不同的体现[root@client ~]# vim /usr/local/nginx/conf/nginx.conf56         location / {57             root   html;58             index  index.html index.htm;59         }60      location ~*\.(jpg|gif|swf)$ {61             valid_referers none blocked *.1.com 1.com;                  #   62             if ( $invalid_referer ) {63                rewrite ^/ http://www.1.com/2.png;64             }65         }

盗链端不需要做操作。

使用win10访问域名+盗链端地址:

9. FPM模块参数优化

对FPM模块进行参数优化

Nginx的PHP解析功能实现如果是交由FPM(fastcgi 进程管理器)处理的,为了提高PHP的处理速度。可以对FPM模块进行参数跳转

Ngingx是通过FPM调用的PHP

FPM优化参数:

pm  #使用哪种方法启动fpm进程,可以说是static和dynamic。前者将产生固定数量的fpm进程,后者将以动态的方式产生fpm进程。

pm.max_children  #static方式下开启的fpm进程数

pm.start_server  #动态方式下初始的fpm进程数量

pm.min_spare_servers  #动态方式下最小的fpm空闲进程数

pm.max_spare_servers  #动态方式下最大的fpm空闲进程数

vim php-fpm.conf pid = run/php-fpm.pidpm = dynamicpm.max_children=20     ##static模式下空闲进程数上限,大于下面的值pm.start_servers = 5   ##动态方式下默认开启的进程数,在最小和最大之间pm.min_spare_servers = 2  ##动态方式下最少空闲进程数pm.max_spare_servers = 8  ##动态方式下最大空闲进程数

这篇关于Nginx的优化与防盗链,无名宵小速速退让!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/705476

相关文章

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

服务器雪崩的应对策略之----SQL优化

SQL语句的优化是数据库性能优化的重要方面,特别是在处理大规模数据或高频访问时。作为一个C++程序员,理解SQL优化不仅有助于编写高效的数据库操作代码,还能增强对系统性能瓶颈的整体把握。以下是详细的SQL语句优化技巧和策略: SQL优化 1. 选择合适的数据类型2. 使用索引3. 优化查询4. 范式化和反范式化5. 查询重写6. 使用缓存7. 优化数据库设计8. 分析和监控9. 调整配置1、

Java中如何优化数据库查询性能?

Java中如何优化数据库查询性能? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨在Java中如何优化数据库查询性能,这是提升应用程序响应速度和用户体验的关键技术。 优化数据库查询性能的重要性 在现代应用开发中,数据库查询是最常见的操作之一。随着数据量的增加和业务复杂度的提升,数据库查询的性能优化显得尤为重

设置Nginx缓存策略

详细信息 Nginx服务器的缓存策略设置方法有两种:add_header或者expires。 1. add_header 1)语法:add_header name value。 2)默认值:none。 3)使用范围:http、server、location。 配置示例如下: add_header cache-control "max-age=86400";#设置缓存时间为1天。add

打包体积分析和优化

webpack分析工具:webpack-bundle-analyzer 1. 通过<script src="./vue.js"></script>方式引入vue、vuex、vue-router等包(CDN) // webpack.config.jsif(process.env.NODE_ENV==='production') {module.exports = {devtool: 'none

Docker Compose--安装Nginx--方法/实例

原文网址:Docker Compose--安装Nginx--方法/实例_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍Docker Compose如何安装Nginx。 目录结构 ├── config│   ├── cert│   │   ├── xxx_bundle.pem│   │   └── xxx.key│   ├── conf.d│   └── nginx.co

nginx 8051#0: *4 recv() failed (104: Connection reset by peer) while reading response header from u

环境    php7   nginx1.8.0    nginx   报错  500  GATWAY网关错误 2017/08/28 10:45:42 [error] 7975#0: *333 recv() failed (104: Connection reset by peer) while reading response header from upstream, clien

nginx 504 Gateway Time-out

环境:PHP7.1,NGINX,Mysql 问题描述: 本地写了一个需要执行比较长时间的脚本,放到了php-fpm里面跑。用一个链接调用起这个脚本。发现第一次调用的时候,需要等比较久的时间,但是如果在执行期间再次请求这个链接。第二个请求的链接会返回504。甚至,直接在脚本最开始的地方中断都还是报 504. 但是如果请求其他链接,可以正常请求。 nginx 返回码、、 504 Gateway

linux匹配Nginx日志,某个字符开头和结尾的字符串

匹配 os=1 开头, &ip结尾的字符串 cat 2018-06-07.log | egrep -o ‘os=1.*.&ip’ 存入日志。然后使用submit 前面和后面的值去掉,剩下就是需要的字符串。 cat 2018-06-07.log | egrep -o ‘os=1.*.&ip’ >log.log

Clickhouse 的性能优化实践总结

文章目录 前言性能优化的原则数据结构优化内存优化磁盘优化网络优化CPU优化查询优化数据迁移优化 前言 ClickHouse是一个性能很强的OLAP数据库,性能强是建立在专业运维之上的,需要专业运维人员依据不同的业务需求对ClickHouse进行有针对性的优化。同一批数据,在不同的业务下,查询性能可能出现两极分化。 性能优化的原则 在进行ClickHouse性能优化时,有几条