Nginx 优化与防盗链

Nginx 优化与防盗链

在日常使用和管理 Nginx 服务器时,通过一些优化手段可以提高性能和安全性。本文将详细介绍几种常见的 Nginx 优化方法,包括隐藏版本号、修改用户与组、设置缓存时间、日志切割、配置连接超时、调整进程数、网页压缩以及防盗链配置。

一、隐藏版本号

Nginx 默认会在响应头中显示版本号,可能会为服务器带来安全隐患。通过以下两种方法可以隐藏或修改 Nginx 的版本号。

1.1 修改配置文件方式

此方法通过修改 Nginx 配置文件来隐藏版本号。

1.1.1 操作步骤

  1. 使用以下命令打开 Nginx 配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    
  2. http 块中添加 server_tokens off; 来关闭版本号的显示:
    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_tokens off;  # 关闭版本号显示
        ...
    }
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    
  4. 通过命令检查版本号是否已隐藏:
    curl -I http://192.168.10.23
    

1.2 修改源码方式

此方法通过直接修改 Nginx 的源码文件来改变版本号和服务器类型。

1.2.1 操作步骤

  1. 编辑 Nginx 源码中的头文件:
    vim /opt/nginx-1.12.0/src/core/nginx.h
    
  2. 修改版本号和服务器类型:
    #define NGINX_VERSION "1.1.1"  # 修改版本号
    #define NGINX_VER "IIS" NGINX_VERSION  # 修改服务器类型
    
  3. 重新编译并安装 Nginx:
    cd /opt/nginx-1.12.0/
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
    make && make install
    
  4. 修改配置文件中的 server_tokens 设置,并重启服务:
    vim /usr/local/nginx/conf/nginx.conf
    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_tokens on;
        ...
    }
    systemctl restart nginx
    
  5. 检查修改后的版本号:
    curl -I http://192.168.10.23
    

二、修改用户与组

为了提升 Nginx 的安全性,可以将 Nginx 运行的用户与组从默认的 root 修改为 nginx

2.1 操作步骤

  1. 编辑 Nginx 配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    
  2. 将用户和组设置为 nginx
    user nginx nginx;  # 修改用户和组为 nginx
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    
  4. 通过命令检查主进程和子进程的用户是否已修改:
    ps aux | grep nginx
    

三、缓存时间

为提升网站的响应速度,可以通过设置缓存时间来减少重复请求,尤其适用于静态内容。

3.1 操作步骤

  1. 编辑 Nginx 配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    
  2. 针对图片资源设置缓存时间:
    http {
        ...
        server {
            ...
            location / {
                root html;
                index index.html index.htm;
            }
    
            location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
                root html;
                expires 1d;  # 设置缓存时间为1天
            }
            ...
        }
    }
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    
  4. 访问测试资源,并查看响应头信息确认缓存时间:
    curl -I http://www.kgc.com/wangsicong.jpg
    

四、日志切割

随着时间推移,Nginx 的日志文件可能会变得非常庞大。通过定期切割日志,可以避免日志文件过大影响服务器性能。

4.1 操作步骤

  1. 创建日志切割脚本:
    vim /opt/fenge.sh
    
  2. 在脚本中添加以下内容:
    #!/bin/bash
    day=$(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}/kgc.com-access.log-$day  # 移动并重命名日志文件
    kill -USR1 $(cat $pid_path)  # 重建日志文件
    find $logs_path -mtime +30 -exec rm -rf {} \;  # 删除30天前的日志
    
  3. 赋予脚本执行权限并执行脚本:
    chmod +x /opt/fenge.sh
    /opt/fenge.sh
    
  4. 配置定时任务,定期执行日志切割:
    crontab -e
    0 1 * * * /opt/fenge.sh
    

五、连接超时

设置连接超时可以避免长时间占用服务器资源,从而提升 Nginx 的性能。

5.1 操作步骤

  1. 编辑 Nginx 配置文件:
    vim /usr/local/nginx/conf/nginx.conf
    
  2. 设置连接超时参数:
    http {
        ...
        keepalive_timeout 65 180;  # 设置三次握手的超时时间
        client_header_timeout 80;  # 设置客户端请求头的超时时间
        client_body_timeout 80;  # 设置客户端请求体的超时时间
        ...
    }
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    

六、更改进程数

在高并发场景下,通过增加 Nginx 进程数可以提高服务器的处理能力。

6.1 操作步骤

  1. 查看 CPU 核数:
    cat /proc/cpuinfo | grep -c "physical id"
    
  2. 编辑 Nginx 配置文件,调整进程数:
    vim /usr/local/nginx/conf/nginx.conf
    worker_processes  2;  # 设置为CPU核数的2倍
    worker_cpu_affinity 01 10;  # 设置进程与CPU的对应关系
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    

七、配置网页压缩

通过压缩网页内容,可以减少传输数据的大小,节省带宽并提高用户的访问体验。

7.1 操作步骤

  1. 编辑 Nginx 配置文件,开启 Gzip 压缩功能:
    vim /usr/local/nginx/conf/nginx.conf
    
  2. http 块中加入以下配置:
    http {
        ...
        gzip on;  # 开启 gzip 压缩
        gzip_min_length 1k;  # 最小压缩文件大小
        gzip_buffers 4 64k;  # 设置压缩缓冲区
        gzip_http_version 1.1;  # 设置 HTTP 版本
        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;  # 压缩类型
        ...
    }
    
  3. 重启 Nginx 服务:
    systemctl restart nginx
    
  4. 在浏览器中测试压缩效果,检查响应头中的 Content-Encoding: gzip

八、配置防盗链

防盗链可以防止他人盗用服务器上的资源,保护带宽和服务器资源。

8.1 操作步骤

  1. 编辑 Nginx 配置文件,添加防盗链配置:

    vim /usr/local/nginx/conf/nginx.conf
    
  2. server 块中添加以下配置:

    server {
           ...
           location ~* \
    
    .(jpg|gif|swf)$ {
               valid_referers none blocked *.kgc.com kgc.com;
               if ($invalid_referer) {
                   rewrite ^/ http://www.kgc.com/error.png;  # 或使用 return 403;
               }
           }
           ...
       }
    
  3. 配置完成后,重启 Nginx 服务:

    systemctl restart nginx
    

*.kgc.com kgc.com;
if ($invalid_referer) {
rewrite ^/ http://www.kgc.com/error.png; # 或使用 return 403;
}
}

}


3. 配置完成后,重启 Nginx 服务:
```bash
systemctl restart nginx
  1. 准备好网页和资源文件,测试防盗链效果。
  • 19
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值