Nginx optimization and anti-theft chain (hide version number, configure anti-theft chain, modify users and groups, cache time, log cutting, configure web page compression, connection timeout, change process)

Hide version number

You can use the Fiddler tool to grab data packets and check the Nginx version,
or you can use the command curl -I http://192.168.80.10 in Centos to display the header information of the response message.
Insert picture description here

curl -I http://192.168.238.20

Insert picture description here
Insert picture description here

方法一(隐藏版本):修改配置文件方式

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
include  mime.types;
default_type application/octet-stream;
server_tokens off;         添加,关闭版本号
,.....
systemctl restart nginx.service
curl -I http://192.168.238.20

Insert picture description here
Insert picture description here

方法二(伪装版本号):修改源码文件,重新编译安装

vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1"         #修改版本号
#define NGINX_VER "IIS" NGINX_VERSION       #修改服务器类型

cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make && make install

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
include  mime.types;
default_type application/octet-stream;
server_tokens on;      打开

systemctl restart nginx
curl -I http://192.168.238.20

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Modify users and groups

vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;          #取消注释,修改用户为nginx,组为nginx

systemctl restart nginx

ps aux | grep nginx
主进程由root创建,子进程由nginx创建

Insert picture description here
Insert picture description here
Insert picture description here

Cache time

vim /usr/local/nginx/conf/nginx.conf

http {
    
    
     server {
    
    
            location / {
    
    
                 root html;
                 index index.html index.htm;
            }
            location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
    
           
            加入新的location,以图片作为缓存对象
                  root html;
                  expires 1d;                     #指定缓存时间, 1天
            }
       }
}

systemctl restart nginx
http://192.168.238.20/gou.jpg

在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络--->选择HTML、ws、其他
访问http://192.168.238.20
双击200响应消息查看响应头中包含'Cahce-Control:max-age=86400'表示缓存时间是86400秒。
也就是缓存一天的时间,一天之内浏览器访问这个页面,都是用缓存中的数据
而不需要向Nginx服务器重新发出请求,减少了服务器的使用带宽。

Insert picture description here

Log cutting

vi /opt/fenge.sh
#!/bin/bash
#Filename:fenge.sh
day=$(date -d "-1 day" "+%Y%m%d""+%F")      #显示前一天的时间
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}/xyw.com-access.log_$day    #移动并重命名日志文件
kill -USR1 $(cat $pid_path)                           #重建新日志文件
find $logs_path -mtime +30 -exec rm -rf {
    
    } \;        #删除30天之前的日志文件
#find $logs_path -mtime +30 | xargs rm -rf



chmod +x /opt/fenge.sh
/opt/fenge.sh
ls /var/log/nginx
ls /usr/local/nginx/logs/access.log
crontab -e
0 1 * * * /opt/fenge.sh

Insert picture description here
Insert picture description hereInsert picture description here
Insert picture description here

ctime, atime, mtime

In the linux operating system, each file has a lot of time parameters, of which three are more important, namely ctime, atime, mtime

ctime (status time):
当修改文件的权限或者属性的时候,就会更新这个时间,ctime并不是create time,更像是change time,只有当更新文件的属性或者权限的时候才会更新这个时间,但是更改内容的话是不会更新这个时间。

atime (accesstime):
当使用这个文件的时候就会更新这个时间。

mtime (modification time):
当修改文件的内容数据的时候,就会更新这个时间,而更改权限或者属性,mtime不会改变,这就是和ctime的区别。

Connection timed out

HTTP has a KeepAlive mode, which tells the web server to keep the TCP connection open after processing a request. If it receives other requests from the client, the server will use this unclosed connection without establishing another connection.
KeepAlive remains open for a period of time, and they will occupy resources during this period. Excessive use will affect performance.

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
}

systemctl restart nginx

Insert picture description here

keepalive_timeout
指定KeepAlive的超时时间(timeout) 。指定每个TCP连接最多可以保持多长时间,服务器将会在这个时间后关闭连接。
Nginx的默认值是65秒,有些浏览器最多只保持60秒,所以可以设定为60秒。若将它设置为0,就禁止了keepalive连接。

第二个参数(可选的)指定了在响应头Keep-Alive:timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,Nginx不会发送Keep-Alive响应头。

client_header_timeout
客户端向服务端发送一个完整的request header的超时时间。如果客户端在指定时间内没有发送一个完整的request header,Nginx返回HTTP 408 (Request Timed out)

client_body_timeout
指定客户端与服务端建立连接后发送request body的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx返回HTTP408 (Request Timed Out)。

Change process

cat /proc/cpuinfo | grep -c "physical id"     #查看cpu核数
ps aux | grep nginx      #查看nginx主进程中包含几个子进程

vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;  #修改为核数相同或者2倍
worker_cpu_affinity 01 10;  #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000

systemctl restart nginx

Insert picture description here
Insert picture description here

Configure web page compression

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+rssimage/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;  #压缩类型,表示哪些网页文档启用压缩功能
}

cd /usr/local/nginx/html先将gou.jpg文件传到/usr/local/nginx/html目录下
vim index.html
....
<img src="gou.jpg"/>     #网页中插入图片
</body></html>

Insert picture description here

Insert picture description here

在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络-->选择HTML、WS、其他
访问http://192.168.238.20,双击200响应消息查看响应头中包含"Content-Encoding: gzip"

Insert picture description here

Configure anti-leech

盗链机

yum install -y httpd

vim /var/www/html/index.html
<html><body><h1>IT WORKS!</h1>
<img src="http://192.168.238.20/gou.jpg"/>
</body></html>

echo "192.168.238.20 www.xyw.com" >> /etc/hosts
echo "192.168.238.40 www.abc.com" >> /etc/hosts

systemctl restart httpd.service

Insert picture description here

源主机

cd /var/www/html/xyw
[root@www xyw]#ls
gou.jpg  index.html

vim index.html 

</body></html>
<html><body>
<h1>www.xyw.com<h1>
<img src="gou.jpg"/>
</body></html>

systemctl restart nginx.service

可以访问www.xyw.com查看图片
也可访问http://192.168.238.20进行查看

Insert picture description here

Insert picture description here
此时盗链主机已经可以盗链

访问www.abc.com进行盗链

Insert picture description here
源主机配置防盗链

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
    server {
    
    
        location ~* \.(jpg|gif|swf)$ {
    
    
            valid_referers *.kgc.com kgc.com;
            if ($invalid_referer) {
    
    
	        rewrite ^/ http://www.kgc.com/error.png;
	        #return 403;
            }
        }
    }
}

cd /var/www/html/xyw
[root@www xyw]#ls
gou.jpg  index.html 
[root@www xyw]#rz -E             将防盗链图片拖进来
rz waiting to receive.
[root@www xyw]#ls
error.jpg  gou.jpg  index.html
[root@www xyw]#mv error.jpg error.png     因为原图片和盗链图篇格式不能一致,修改盗链图片格式,记得别人忘记文件里面的也要修改
[root@www xyw]#ls
error.png  gou.jpg  index.html

systemctl restart nginx.service

~*. (jpg|gif|swf)$: This regular expression indicates that it matches case-insensitive files that end with .jpg or .gif or .swf;
valid_referers: set trusted websites, pictures can be used normally; URL or domain name: Referer contains a related string URL
if statement: If the source domain of the link is not in the list listed in valid_referers, and sinvalid referer is 1, then the following operation is performed, that is, rewriting or returning to the 403 page.

Insert picture description here
Insert picture description here
此时盗链机已经无法盗链

可以访问www.abc.com验证

Insert picture description here

Guess you like

Origin blog.csdn.net/IvyXYW/article/details/112561722