Nginx优化服务和防盗链

文章详细介绍了如何对Nginx进行优化,包括设置长连接,安装和使用第三方echo模块,搭建虚拟主机以实现多站点管理,配置防盗链保护资源,以及启用网页压缩和日志分割功能。同时,还提到了如何修改Nginx运行的用户和组,确保服务安全。
摘要由CSDN通过智能技术生成

一、长连接

长连接是指在一个连接上可以连续发送多个数据包,在连接保持期间,如果没有数据包发送,需 …
短连接是指通讯双方有数据交互时,就建立一个连接,数据发送完成后,则断开此连接,即每次连接只完成一项业务的发送。

1、修改主配置文件

[root@www ~]#vim /apps/nginx/conf/nginx.conf
http {
    include       mime.types;
    include       /apps/nginx/conf.d/*.conf;   ##添加这行内容
    default_type  application/octet-stream;
[root@www ~]#cd /apps/nginx/      ##切换到/apps/nginx/目录
[root@www nginx]#mkdir conf.d   ##创建conf.d文件
[root@www nginx]#cd conf.d/     ##切换到conf.d目录里
[root@www conf.d]#vim pc.conf   ##编辑pc。conf文件
server{
    listen 80;
    server_name  www.zhantai.com;
    root  /data/nginx/pc/;

[root@www conf.d]#mkdir -pv /data/nginx/pc/   ##递归创建文件夹
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/nginx"
mkdir: 已创建目录 "/data/nginx/pc/"

[root@www conf.d]#echo pc > /data/nginx/pc/index.html
[root@www conf.d]#tree /data/
/data/
└── nginx
    └── pc
        └── index.html

2 directories, 1 file
[root@www conf.d]#systemctl restart nginx  ##重启服务

在这里插入图片描述
在这里插入图片描述

2、测试

在另一台机子
[root@localhost ~]# vi /etc/hosts   ##将前面一台机子的IP地址和域名需要写在hosts文件里面
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.130 www.zhantai.com
[root@localhost ~]# curl www.zhantai.com   ##验证出现pc就OK了
pc

在这里插入图片描述

3、在主配置文件添加

[root@www conf.d]#vi pc.conf

server{
    listen 80;
    server_name  www.zhantai.com;
    root  /data/nginx/pc/;
    keepalive_requests 3;
    keepalive_timeout 60 65;
}
[root@www conf.d]#systemctl restart nginx  ##重启服务
[root@www conf.d]#nginx -s reload    ##重新加载配置

在这里插入图片描述

4、验证

在另一台机子验证
[root@localhost ~]# curl www.zhantai.com -I
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 21 Feb 2023 14:26:36 GMT
Content-Type: text/html
Content-Length: 3
Last-Modified: Tue, 21 Feb 2023 13:50:18 GMT
Connection: keep-alive
Keep-Alive: timeout=65      ###超时时间
ETag: "63f4cc1a-3"
Accept-Ranges: bytes

在这里插入图片描述

二、Nginx第三方模块

1、开源的echo模块

现将代码包上传到/opt目录下,解压
[root@www opt]#cd /opt/   
[root@www opt]#unzip echo-nginx-module-master.zip     #解压
[root@www opt]#cd nginx-1.18.0/
[root@www nginx-1.18.0]#systemctl stop nginx  #编译前先停掉服务,在编译

./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/echo-nginx-module-master   #第三方模块

make  &&  make install    #重新编译之后就有第三方模块,编译时就可以使用
此代码包下载需要翻墙,方便期间,可以直接到网盘下载:
链接:https://pan.baidu.com/s/1ULiMf3Kc3MWjmrozZu6fQA 
提取码:q94p

在这里插入图片描述

2、查看是否成功

在这里插入图片描述

3、加echo模块步骤

[root@www nginx-1.18.0]#systemctl start nginx
[root@www nginx-1.18.0]#cd /apps/nginx/conf.d/
[root@www conf.d]#vim pc.conf    #进入配置文件添加一下内容

 location  /hello {
     default_type   text/html;
     echo "hello world ";
   }
}
[root@www conf.d]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@www conf.d]#nginx -s reload

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、网页测试验证

在这里插入图片描述

三、搭建虚拟主机

1、编译安装好nginx后,对主配置文件进行修改

[root@localhost]# cd /apps/nginx/conf/
[root@www conf]#vim nginx.conf  ##在http模块添加这个内容,在这只能写http子模块,要在主配置文件
include /apps/nginx/conf.d/*.conf;
[root@www nginx]#mkdir conf.d

在这里插入图片描述

2、创建文件

[root@www nginx]#cd conf.d/
[root@www conf.d]#vi pc.conf
server{
     listen  80;
     server_name   www.pc.com;
     root  /data/nginx/pc/;

}
[root@www conf.d]#vi m.conf
server{
     listen  80;
     server_name   www.m.com;
     root  /data/nginx/m/;

}

在这里插入图片描述
在这里插入图片描述

3、验证

在第二台机子
[root@localhost ~]# vi /etc/hosts
192.168.10.130  www.pc.com   www.m.com
[root@localhost ~]# curl www.pc.com
pc
[root@localhost ~]# curl www.m.com
mobile
[root@localhost ~]#

在这里插入图片描述

四、防盗链

1、先yum安装nginx,两台机子都要装

[root@localhost ~]#yum install -y epel-release    #yum安装nginx
[root@localhost ~]#yum info nginx
[root@localhost ~]#yum install -y nginx
[root@localhost ~]#systemctl restart nginx.service    #重启服务
[root@localhost ~]#cd /usr/share/nginx/html/
[root@localhost html]#vim index.html

<html>
<body>
<h1>ZZT is boy</h1>
<img src="a.jpg"/>
</body>
</html>
[root@localhost html]#systemctl restart nginx.service 

在这里插入图片描述

2、网页准备

Web源主机配置:192.168.10.130(www.zztshiboy.com)

1.将a.jpg b.png文件传到/usr/local/nginx/html目录下,注意:编译安装html文件位置就在/usr/local/nginx/html,要是yum安装的话就在/usr/share/nginx/html
[root@localhost ~]# cd /apps/nginx/html/
[root@localhost html]# ls
50x.html  a.jpg  index.html
[root@localhost html]#

2.配置首页文件
vim index.html
<html>
 <body>
<h1>ZZT is boy</h1>
<img src="a.jpg"/>
  </body>
</html>

3.添加IP和域名的映射关系
[root@localhost html]#echo "192.168.10.130 www.zztshiboy.com" >> /etc/hosts
[root@localhost html]#echo "192.168.10.132 www.zzthenshuai.com" >> /etc/hosts

在这里插入图片描述
在这里插入图片描述
盗链网站主机配置:192.168.10.132(www.zzthenshuai.com)

1.切换到站点目录
[root@bogon ~]# cd /usr/share/nginx/html/

2.配置首页文件,图片盗用Web源主机中的图片资源
vim index.html
 <html>
 <body>
 <title>test</title>
  <img src="http://www.zztshiboy.com/a.jpg"/>
 </body>
 </html>
 
3.添加IP和域名的映射关系
[root@bogon html]# echo "192.168.10.130 www.zztshiboy.com" >> /etc/hosts
[root@bogon html]# echo "192.168.10.132 www.zzthenshuai.com" >> /etc/hosts

4.在盗图网站主机上进行浏览器查看
http://www.zztshiboy.com      #Web源主机
http://www.zzthenshuai.com    #盗链网站

在这里插入图片描述

3、Web源主机配置防盗链

当不是由web主机的域名请求图片资源时,统一将图片重写到b.png。

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

 location ~* \.(jpg|gif|swf)$ {
             root html;
             expires 1d;
             valid_referers none blocked *.zztshiboy.com zztshiboy.com;
               if ( $invalid_referer ) {
             rewrite ^/ http://www.zztshiboy.com/b.png;
         }

       }
[root@localhost ~]#systemctl restart nginx

##################注释#########################

~* .(jpg|gif|swf)$ :这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf 结尾的文件;
 
 valid_referers :设置信任的网站,可以正常使用图片;
 
 none:允许没有http_refer的请求访问资源(根据Referer的定义,它的作用是指示一个请求是从哪里链接过来的,如果直接在浏览器的地址栏中输入一个资源的URL地址,那么这种请求是不会包含 Referer 字段的),如 http://www.zzthenshuai.com/a.jpg。
 我们使用 http://www.zzthenshuai.com 访问显示的图片,可以理解成 http://www.zzthenshuai.com/a.jpg 这个请求是从 http://www.zzthenshuai.com 这个链接过来的。
 
 blocked:允许不是http://开头的,不带协议的请求访问资源; 
 
 *.zzthenshuai.com:只允许来自指定域名的请求访问资源,如 http://www.zzthenshuai.com
 
 if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为true,则执行后面的操作,即进行重写或返回 403 页面。

在这里插入图片描述
在这里插入图片描述

五、配置网页压缩

Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。

允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装。

可在配置文件中加入相应的压缩功能参数对压缩性能进行优化。

 #1、修改配置文件
 vim /usr/local/nginx/conf/nginx.conf
 http {
 ...... 
    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;
 ...... 
 }
 
 #2、重启服务
 systemctl restart nginx
 
 #3、浏览器访问测试
 在Linux系统中,打开火狐浏览器,右击点查看元素
 选择 网络 ---> 选择 HTML、WS、其他 
 访问 http://www.zztshiboy.com ,双击响应消息查看响应头中包含 Content-Encoding: gzip

六、日志分割

随着Nginx运行时间的增加,产生的日志也会逐渐增加,为了方便掌握Nginx的运行状态,需要时刻关注Nginx日志文件。太大的日志文件对监控是一个大灾难,不便于分析排查,需要定期的进行日志文件的切割。

操作步骤

(1)、创建旧日志存放目录

(2)、通过mv命令将原有日志移动到日志目录中

(3)、kill -USR1 < PID> 重新生成日志文件

(4)、删除30天之前的日志文件

 1. #写脚本
 [root@localhost ~]# vim /opt/log.sh
 #!/bin/bash
 # Filename: log.sh
 # nginx日志分割,按时间分割
 
 #显示前一天的时间
 day=$(date -d "-1 day" "+%Y%m%d")
 #旧日志文件目录
 logs_path="/var/log/nginx"
 #nginx进程的PID
 pid_path="/usr/local/nginx/logs/nginx.pid"
 
 #如果旧日志目录不存在,则创建日志文件目录
 [ -d $logs_path ] || mkdir -p $logs_path
 #将日志移动到旧日志目录,并重命名日志文件
 mv /usr/local/nginx/logs/access.log ${logs_path}/zztshiboy.com-access.log-$day
 #重建新日志文件
 kill -USR1 $(cat $pid_path) 
 #删除30天之前的日志文件
 find $logs_path -mtime +30 -exec rm -rf {} ;           
 
 2. #赋予执行权限,执行脚本。查看日志文件目录。
 [root@localhost ~]# chmod +x /usr/local/nginx/nginx_log.sh 
 [root@localhost ~]# /opt/log.sh
 [root@localhost ~]# ls /var/log/nginx/            //旧日志文件已被移动到设置好的目录
 zztshiboy.com-access.log-202200608
 [root@localhost ~]# ls /usr/local/nginx/logs/     //已重建新日志文件
 access.log  error.log  nginx.pid
 
 3. #编写计划任务,每天定点执行
 [root@localhost nginx]#crontab -e
 0 1 * * * /opt/fenge.sh

七、修改Nginx运行的用户和组

方法一:在编译安装时,指定运行用户和组(该方法仅在编译安装时使用)

 [root@localhost opt]# cd nginx-1.18.0/
 [root@localhost nginx-1.18.0]# ./configure \
  --prefix=/usr/local/nginx \       #指定nginx的安装路径
  --user=nginx \                    #指定用户名(运行用户)
  --group=nginx \                   #指定组名
  --with-http_stub_status_module    #启用http_stub_status_module模块以支持状态统计

方法二:修改配置文件nginx.conf

 #修改配置文件文件
 vim /usr/local/nginx/conf/nginx.conf
 user nginx nginx;       #取消注释,修改用户为nginx,组为nginx
 
 #重启服务
 systemctl restart nginx
 
 #查看是否修改成功。可以看到主进程由root创建,子进程由nginx创建
 ps aux | grep nginx

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值