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

相关文章

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.