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

相关文章

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Nginx中配置HTTP/2协议的详细指南

《Nginx中配置HTTP/2协议的详细指南》HTTP/2是HTTP协议的下一代版本,旨在提高性能、减少延迟并优化现代网络环境中的通信效率,本文将为大家介绍Nginx配置HTTP/2协议想详细步骤,需... 目录一、HTTP/2 协议概述1.HTTP/22. HTTP/2 的核心特性3. HTTP/2 的优

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各