nginx 优化和安装防盗链以及实验举例

目录

nginx编译安装常用模块

生产中建议设置

nginx 内核限制文件优化

先将 nginx编译安装直至 systemctl命令使用 nginx服务

安全优化

隐藏 nginx版本号

查看版本号

隐藏版本信息

修改用户与组

限制单个 IP的访问频率和连接数

防盗链相关设置

在源主机里配置防盗链

nginx 日志分割脚本

nginx 内核优化

性能优化,每次更改完要记得重启服务哟

页面缓存时间

设置工作进程数

工作进程静态绑核

设置并发

连接保持超时

网页压缩


nginx编译安装常用模块

http_gzip_module              #网页压缩模块
http_stub_status_module       #状态统计模块
http_auth_basic_module        #网页用户认证模块
http_fastcgi_module           #fastcgi转发 php-fpm的模块
http_rewrite_module           #URL重写模块
http_ssl_module               #https安全加密模块
http_limit_conn_module        #限制最大连接数模块
http_limit_req_module         #限制最大访问频率模块
http_proxy_module             #请求转发模块
http_image_filter_module      #图片处理模块
http_upstream_*_module        #负载均衡服务器列表模块
stream_*_module               #四层代理转发模块

生产中建议设置

*   soft    noproc          65535         打开的进程数
*   hard    noproc          65535
*   soft    nofile          65535         打开的文件数
*   hard    nofile          65535
*   soft    memlock         unlimited     不做内存锁定
*   hard    memlock         unlimited

nginx 内核限制文件优化

/etc/security/limits.conf          #内核限制文件路径

先将 nginx编译安装直至 systemctl命令使用 nginx服务

安全优化

隐藏 nginx版本号
查看版本号
#方法一:使用 curl命令访问主机IP
[root@localhost ~]# curl -I http://192.168.88.110
HTTP/1.1 200 OK
Server: nginx/1.25.3 #版本号
Date: Tue, 12 Dec 2023 01:45:11 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 12 Dec 2023 01:23:56 GMT
Connection: keep-alive
ETag: "6577b62c-267"
Accept-Ranges: bytes
​
#方法二
#cd到指定目录
[root@localhost ~]# cd /usr/local/nginx/html/
​
#将一个图片传输到虚拟机中
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
50x.html  index.html  下载.jpg
​
#如果是中文需要改为自定义英文
[root@localhost html]# mv 下载.jpg  xia.jpg
[root@localhost html]# ls
50x.html  index.html  xia.jpg

—— 进入虚拟机进行操作 ——

隐藏版本信息

方法一

#进入配置文件修改
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

#检查 nginx语法是否正确
[root@localhost html]# 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#重启 nginx服务
[root@localhost html]# systemctl restart nginx.service#验证版本号是否隐藏
[root@localhost html]# curl -I http://192.168.88.110
HTTP/1.1 200 OK
Server: nginx #已经隐藏
Date: Tue, 12 Dec 2023 03:12:49 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 12 Dec 2023 01:23:56 GMT
Connection: keep-alive
ETag: "6577b62c-267"
Accept-Ranges: bytes

方法二

#cd到安装文件位置
[root@localhost html]# cd /opt#停止 nginx服务
[root@localhost opt]# systemctl stop nginx.service #cd到指定目录
[root@localhost opt]# cd nginx-1.25.3/#cd到内核文件所在目录
[root@localhost nginx-1.25.3]# cd src/core/#进入配置文件
[root@localhost core]# vim nginx.h

#查看之前的配置
[root@localhost core]# nginx -V
nginx version: nginx/1.25.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module#cd到指定目录进行编译安装
[root@localhost core]# cd /opt/nginx-1.25.3/
[root@localhost nginx-1.25.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.25.3]# make && make install#进入配置文件进行更改
[root@localhost nginx-1.25.3]# vim /usr/local//nginx/conf/nginx.conf

#重启 nginx服务
[root@localhost nginx-1.25.3]# systemctl restart nginx#验证
[root@localhost nginx-1.25.3]# curl -I http://192.168.88.110 
HTTP/1.1 200 OK
Server: mhy/123 #显示为自定义内容
Date: Tue, 12 Dec 2023 03:45:37 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 12 Dec 2023 01:23:56 GMT
Connection: keep-alive
ETag: "6577b62c-267"
Accept-Ranges: bytes

修改用户与组
#创建一个自定义用户
[root@localhost nginx-1.25.3]# useradd -s /sbin/nologin mhy
​
#进入配置文件进行更改
[root@localhost nginx-1.25.3]# vim /usr/local//nginx/conf/nginx.conf

#重启 nginx服务
[root@localhost nginx-1.25.3]# systemctl restart nginx.service #查看是否修改成功
[root@localhost nginx-1.25.3]# ps aux |grep mhy
mhy       56039  0.0  0.0  23068  1380 ?        S    12:05   0:00 nginx: worker process
root      56041  0.0  0.0 112676   980 pts/1    R+   12:05   0:00 grep --color=auto mhy

限制单个 IP的访问频率和连接数
#进入配置文件并进行添加修改
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=20r/s; limit_conn_zone $binary_remote_addr zone=addr:10m;
...
limit_req zone=req_zone burst=5 nodelay;
limit_conn addr 5;
...


防盗链相关设置
  • 在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失,也避免了不必要的带宽浪费

  • Nginx 的防盗链功能也非常强大,在默认情况下,只需要进行很简单的配置,即可实现防盗链处理

准备两台虚拟机,编译安装 nginx,源主机:192.168.88.110

盗链主机:192.168.88.60

—— 配置源主机

#cd到指定目录,下载图片
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html  R-C.jpg#进入配置文件,添加配置
[root@localhost html]# vim /etc/hosts

#进入配置文件,添加配置
[root@localhost html]# vim index.html

显示结果;源主机上操作

—— 配置盗链主机

#yum安装 httpd服务
[root@localhost ~]# yum install -y httpd#cd到指定目录
[root@localhost ~]# cd /var/www/html/#进入配置文件
[root@localhost html]# vim index.html
<html>
<body>
<h1>盗链</h1>
<img src="http://www.wx.com/R-C.jpg"/>
</body>
</html>

#进入配置文件
[root@localhost html]# vim /etc/hosts

#重启 http服务
[root@localhost html]# systemctl restart httpd

在盗链主机上访问本机网址;访问源主机网址

在源主机里配置防盗链
#在指定目录下 /usr/local/nginx/html/添加防盗图片
[root@localhost html]# ls
16212205693486036.jpg  50x.html  index.html  R-C.jpg#将图片名更改为 error.png
[root@localhost html]# mv  16212205693486036.jpg  error.png
[root@localhost html]# ls
50x.html  error.png  index.html  R-C.jpg#进入配置文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conflocation ~* \.(jpg|gif|swf)$ {valid_referers none blocked *.wx.com wx.com;if ( $invalid_referer ) { rewrite ^/ http://www.wx.com/error.png;}   }  

#重启 nginx服务
[root@localhost html]# systemctl restart nginx#检查 nginx配置是否有语法错误
[root@localhost html]# 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

实验结果

在源主机上访问源主机网址 www.wx.com

在盗链主机上访问源主机网址 www.wx.com以及访问盗链主机自己的网址 www.hi.com,都会显示源主机设定的防盗图片,隐藏真实图片


nginx 日志分割脚本
#cd到指定目录,日志分割脚本要在此目录下
[root@localhost ~]# cd /usr/local/nginx#编写脚本,自定义脚本名
[root@localhost nginx]# vim nginx_log.sh
#!/bin/bash
#this is used for cutting nginx logs 
##定义日志收集的目录
newlogpath=/var/log/nginx
##定义区分日志的标识,采用时间标记,计划每天一次日志分割
lastday=`date -d "-1 day" +%Y%m%d`
##找到应用程序生成日志的源路径
oldlogpath=/usr/local/nginx/logs
##定义应用程序的pid号 也就是nginx的master进程号
pid=`cat ${oldlogpath}/nginx.pid`##先判断收集日志的目录是否存在,没有则创建,有则进行下一步
[ -d $newlogpath ]||mkdir $newlogpath##使用mv命令进行日志分割,将生成的访问日志和错误日志都移动到收集日志的目录下,并添加时间标记
mv $oldlogpath/access.log $newlogpath/access.log.$lastday
mv $oldlogpath/error.log $newlogpath/error.log.$lastday##重新生成新的日志,便于nginx记录后续访问事务
kill -USR1 $pid##删除大于30天的日志,释放磁盘空间
find $newlogpath -mtime +30 -exec rm -rf {} \;
#给脚本文件添加运行权限
[root@localhost nginx]# chmod +x /usr/local/nginx/nginx_log.sh#编辑定时任务,每天执行一次
[root@localhost nginx]#crontab -e* * */1 * *     /usr/local/nginx/nginx_log.sh

nginx 内核优化
#进入配置文件,添加配置
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 8192
net.core.somaxconn = 10000
net.ipv4.tcp_keepalive_time = 1200


性能优化,每次更改完要记得重启服务哟

页面缓存时间
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

#cd到指定目录,并传输一张图片 ha.jpg
[root@localhost ~]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  error.png  ha.jpg  index.html  R-C.jpg#重启 nginx服务
[root@localhost html]# systemctl restart nginx

实验结果;搜索成功后,电脑键盘按F12,才出现以下右边的界面


设置工作进程数
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf


工作进程静态绑核
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

注释:根据虚拟机的处理器个数决定,例:虚拟机有四个处理器,那就是 0001 0010 0100 1000;虚拟机有三个处理器,那就是 001 010 100



设置并发
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf


连接保持超时
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf


网页压缩
#进入配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf   gzip on;							gzip_min_length 1k;      		gzip_buffers 4 64k;      		gzip_http_version 1.1;   		gzip_comp_level 6;       		gzip_vary on;					gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
#注释gzip on;							#取消注释,开启gzip压缩功能gzip_min_length 1k;      		#最小压缩文件大小gzip_buffers 4 64k;      		#压缩缓冲区,大小为4个64k缓冲区gzip_http_version 1.1;   		#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)gzip_comp_level 6;       		#压缩比率gzip_vary on;					#支持前端缓存服务器存储压缩页面gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;		#压缩类型,表示哪些网页文档启用压缩功能

#重启服务
[root@localhost ~]# systemctl restart nginx

实验结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/219227.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++11】右值引用与移动语义

一.左值与右值 左值&#xff1a;可以取地址的表示数据的表达式&#xff0c;左值可以出现在赋值符号左边 右值&#xff1a;不能取地址的表示数据的表达式&#xff0c;右值不能出现在赋值符号左边 int fun() {return 0; } int main() {int a 0;//a->左值const int b 1;//b-&…

【lesson14】MySQL表的基本查询(1)

文章目录 表的基本操作介绍retrieveselect列建表基本测试 where子句建表基本测试 表的基本操作介绍 CRUD : Create(创建), Retrieve(读取)&#xff0c;Update(更新)&#xff0c;Delete&#xff08;删除&#xff09; retrieve select列 建表 基本测试 插入数据 全列查询 …

利用断路器状态统计sentinel熔断次数

最近项目需要sentinel熔断时记录熔断的次数&#xff0c;在经过一阵搜索后决定利用断路器的状态变化来实现此功能 然而&#xff0c;遇到了这样的一个情况&#xff0c;断路器的状态在第一次熔断时正常从close–>open&#xff0c;但在后续&#xff08;熔断时间内blocked或者熔断…

RocketMQ的延迟消息是如何实现的❓

RocketMQ 作为一款强大的分布式消息中间件&#xff0c;提供了丰富的功能&#xff0c;其中之一就是延迟消息。在本篇博客中&#xff0c;我们将深入探讨 RocketMQ 延迟消息的实现机制&#xff0c;了解消息的定时投递和消费流程。 1. 定时消息的发送 RocketMQ 实现延迟消息的第一…

linux应用软件下载站收集

一、这是一个别人问题帖&#xff0c;里面有很多下载站点。 谁知道可以自由下载Linux软件的论坛或者平台&#xff1f;类似52破解论坛。国内国外都可以&#xff0c;我在搜索引擎找不到&#xff1f; - 知乎

2023年度影响力出海品牌传音移动互联:开放合作 赋能更多中国企业高效出海

伴随着全球化的脚步&#xff0c;出海成为许多中国企业的“必选项”&#xff0c;与之配套的出海服务相关业务也得到了极大的发展。近日&#xff0c;第五届鲸鸣奖颁奖典礼上&#xff0c;传音移动互联凭借为企业提供高效优质的出海解决方案&#xff0c;荣获鲸鸣奖“2023年度影响力…

SpringBoot 引入nacos 【最新 | 可运行】

SpringBoot 引入nacos 首先要了解在 Springboot 中只支持那些 Springboot 的版本&#xff08;我真的被这个搞死了&#xff09;,可以如下图参考&#xff1a; 下面我们就开始吧 下载 Nacos nacos 下载地址&#xff0c;这里可以选择你要下载的版本&#xff0c;我选择下载了2.2.…

<JavaEE> 文件IO -- 数据流和文件内容操作(Reader 和 Writer 、InputStream 和 OutputStream)

目录 一、数据流概述 二、流的关闭 2.1 使用 close() 方法 2.2 使用 try-finally 2.3 使用 try-with-resources 三、字符流的读写 3.1 Reader 类 3.2 Writer 类 四、字节流的读写 4.1 InputStream 类 4.2 OutputStream 类 一、数据流概述 1&#xff09;在 Java 中&…

[c]零钱兑换

题目比较简单&#xff0c;看答案就能看懂什么意思 #include<stdio.h> int main() {int count 0;int n;scanf("%d", &n);for (int i 0; i < n; i){for (int k 0; k <n/2; k){for (int j 0; j < n/5 ; j){if (i 2 * k 5 * j n){count;}}}}p…

【Python基础】迭代器

文章目录 [toc]什么是迭代可迭代对象判断数据类型是否是可迭代类型 迭代器对可迭代对象进行迭代的本质获取可迭代对象的迭代器通过迭代器获取数据StopIteration异常 自定义迭代器__iter__()方法__next__()方法判断数据类型是否是可迭代类型自定义迭代器案例分离模式整合模式 fo…

这套软件测试技巧|软测经典面试题真的有用,今天面试大部分都遇到了!!!

祝同学们都能够顺利找到心仪的工作拿高薪&#xff0c;废话不多说&#xff0c;下面上题了~ 46、您以往是否曾经从事过性能测试工作&#xff1f;如果有&#xff0c;请尽可能的详细描述您以往的性能测试工作的完整过程。 &#xff08;以自己最熟悉的性能测试项目为例&#xff09; …

【ARM Trace32(劳特巴赫) 高级篇 20 -- SNOOPer 使用介绍】

请阅读【Trace32 ARM 专栏导读】 文章目录 Trace32 SNOOPer 介绍SNOOPer 主要功能:SNOOPer 使用场景SNOOPer.ERRORSTOPSNOOPer.ModeSNOOPer.PCSNOOPer.RateSNOOPer.SELectSNOOPer.SIZESNOOPer.TDelaySNOOPer.TOutSNOOPer.TValueSNOOPer PC 采样Trace32 SNOOPer 介绍 在 Laut…

什么是面向切片编程?

面向切片&#xff08;Aspect-oriented Programming&#xff0c;AOP&#xff09;是一种软件开发的方法论&#xff0c;它旨在通过将横切关注点从主要业务逻辑中分离出来&#xff0c;提供更高层次的模块化和可维护性。它通过将跨越多个对象、类或组件的共同功能&#xff08;称为横…

可惜+悲伤+唉=emmo...

拟合曲线&#xff1a; 参考论文&#xff1a;黄河清.NURBS曲面逆向造型关键算法的研究与应用 [D].西北工业大学,2004 三次NURBS曲线控制点的计算 首先给出拟合曲线的具体步骤&#xff1a; 1、节点矢量的求解方法为&#xff1a; 采用积累弦长参数化法&#xff0c;即&#xff1…

应用ICP-MS实验PFA烧杯耐腐蚀带刻度反应杯的特点分析

聚四氟&#xff08;PFA&#xff09;烧杯可用于痕量分析、同位素分析等实验&#xff0c;ICP-MS实验室适用。半导体、多晶硅、光伏电子 锂电池行业均适用。杯体刻度清晰&#xff0c;方便观察&#xff0c;尖嘴方便倾倒溶液。 可溶性聚四氟乙烯烧杯特性&#xff1a; 1、透明&…

如何定制专属app:定制app教程

《如何定制专属app&#xff1a;探索app定制的秘密》 什么是app定制 在当今数字化时代&#xff0c;应用程序&#xff08;app&#xff09;已经渗透到我们生活的方方面面&#xff0c;从社交娱乐到工作学习&#xff0c;app无处不在&#xff0c;尽管市场上有着众多的app供我们选择…

(企业 / 公司项目)微服务项目解决跨域问题:

前后端分离项目中前端出现了跨域的问题 在网关模块配置文件中添加 配置 application.properties # 允许请求来源&#xff08;老版本叫allowedOrigin&#xff09; spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedOriginPatterns* # 允许携带的头信息 spri…

经典基本电路

USB电路 USB差分走线的阻抗为90欧:差分对10mil宽的走线以及5mil的间距,两边包地15/20mil以上厚度(SI9000计算阻抗) USB2.0接口电路&#xff1a; USB3.0接口电路&#xff1a; USB HUB电路: HDMI电路 HDMI差分走线的阻抗为100欧:差分对6mil宽的走线以及5mil的间距,两边包地15/20…

PADS的使用

目录 一、PADS常规操作1.1 常用快捷键1.2 Logic相关设置 二、Logic库操作2.1 逻辑库2.2 封装库2.3 元件库 一、PADS常规操作 PADS安装好了之后&#xff0c;主要使用下面三个软件&#xff1a; Logic&#xff1a;画原理图文件&#xff0c;也可以使用OrCAD导入Router&#xff1a;…

解决maven报错 ‘parent.relativePath‘ of POM

错误提示 parent.relativePath of POM io.renren:renren-fast:3.0.0 (D:\wzyProjets\gulimail\renren-fast\pom.xml) points at com.wzy.gulimail:gulimail instead of org.springframework.boot:spring-boot-starter-parent, please verify your project structure错误分析 子…