• Nginx的优化和防盗链


    Nginx的优化方案

    1、隐藏版本号

    如何查看版本号:

    方式一:

    [root@nginx1 ~]# curl -I 20.0.0.61

    方式二:

    浏览器访问主机地址,F12查看

    如何隐藏:

    方式一:修改配置文件

    1. [root@nginx1 ~]# cd /usr/local/nginx/conf/
    2. [root@nginx1 conf]# vim nginx.conf
    3. --http模块插入--
    4. server_tokens off;
    5. [root@nginx1 conf]# nginx -t
    6. [root@nginx1 conf]# systemctl restart nginx
    7. [root@nginx1 conf]# curl -I 20.0.0.61

    方式二:修改源码文件,重新编译安装

    (在源码包里修改配置文件,修改完之后要重新配置,编译和安装,然后将隐藏版本号打开)

    1. [root@nginx1 opt]# cd nginx-1.22.0/src/core/
    2. [root@nginx1 core]# vim nginx.h

    1. [root@nginx1 core]# cd /usr/local/nginx/conf/
    2. [root@nginx1 conf]# vim nginx.conf
    3. server_tokens on;

    1. [root@nginx1 conf]# nginx -t
    2. [root@nginx1 conf]# systemctl restart nginx

    2、nginx日志分割

    原因:nginx没有自带的日志分割功能,需要靠人工通过脚本实现日志分割

    1. [root@nginx1 opt]# vim nginxlog.sh
    2. #!/bin/bash
    3. d=$(date +%Y-%m-%d)
    4. dir="/usr/local/nginx/logs"
    5. logs_file='/usr/local/nginx/logs/access.log'
    6. logs_error='/usr/local/nginx/logs/error.log'
    7. pid_file='/usr/local/nginx/run/nginx.pid'
    8. if [ ! -d "$dir" ]
    9. then
    10. mkdir -p $dir
    11. fi
    12. mv ${logs_file} ${dir}/access_${d}.log
    13. mv ${logs_error} ${dir}/error_${d}.log
    14. kill -USR1 $(cat ${pid_file})
    15. find $dir -mtime +30 -exec rm -rf {} \;

    日志清理原则:
    业务日志一般保留30天。数据库日志,保留2年。用户信息加密,而且要永久保存。高可用。
    业务日志如果最近无重大事项,保留10天之内的也可以,但是要申请批准。

    3、网页压缩

    Nginx的ngx_http_gzip_module压缩模块可以对文件内容和图片进行压缩的功能,可以节约宽带,提升用户的访问速度

    1. [root@nginx1 conf]#vim nginx.conf
    2. http {
    3. ......
    4. gzip on; #取消注释,开启gzip压缩功能
    5. gzip_min_length 1k; #如果文件的大小是1k,不再进行压缩处理
    6. gzip_comp_level 6; #压缩比率为1-91是速度快但是压缩比最低,就是压缩速度慢但是压缩比高
    7. gzip_vary on; #前端的缓存也可以支持压缩
    8. gzip_types text/plain text/css text/xml image/jpg image/jpeg image/png image/gif;
    9. ......
    10. #所有支持压缩的格式: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;

    1. [root@nginx1 conf]# nginx -t
    2. [root@nginx1 conf]# systemctl restart nginx
    3. [root@nginx1 conf]# cd ..
    4. [root@nginx1 nginx]# cd html
    5. --传入图片--
    6. [root@nginx1 html]# ls
    7. 50x.html index.html naruto.PNG

    4、设置nginx的图片缓存时间

    可以在日后访问时不需要经常的向后台请求数据,加快访问速度,一般是针对静态页面,动态不设置缓存时间

    1. [root@nginx1 conf]# vim nginx.conf
    2. http {
    3. ......
    4. server {
    5. ......
    6. location ~* \.(gif|jpg|jepg|bmp|ico)$ {
    7. root html;
    8. expires 1d;
    9. }
    10. ......
    11. }
    12. }

    1. [root@nginx1 conf]# nginx -t
    2. [root@nginx1 conf]# systemctl restart nginx

    5、连接超时

    1. [root@nginx1 conf]# vim nginx.conf
    2. http {
    3. ......
    4. keepalive_timeout 60;
    5. client_header_timeout 10;
    6. client_body_timeout 10;
    7. ......
    8. }
    9. -----------------------------------------------------------------------------------------
    10. client_header_timeout
    11. 客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)。
    12. client_body_timeout
    13. 指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)。

    1. [root@nginx1 conf]# nginx -t
    2. [root@nginx1 conf]# systemctl restart nginx

    6、nginx并发设置

    在高并发的场景下,需要nginx启动更多的进程来保证响应速度
    根据cpu的核心数,可以调整nginx的工作进程

    查看cpu核数:

    1. [root@nginx1 conf]# cat /proc/cpuinfo | grep processor | wc -l
    2. 或者
    3. [root@nginx1 conf]# cat /proc/cpuinfo | grep -c processer
    1. [root@nginx1 conf]# vim nginx.conf
    2. --将worker_processes改为四个--
    3. [root@nginx1 conf]# nginx -t
    4. [root@nginx1 conf]# systemctl restart nginx

    为什么8个以上的cpu进程会降低性能?
    cpu频繁切换会浪费资源

    解决方法:
    将进程绑定在某一个cpu上,可以减少cpu之间的切换,提高效率

    1. [root@nginx1 conf]# vim nginx.conf
    2. worker_processes 4;
    3. worker_cpu_affinity 0001 0010 0100 1000;

    1. [root@nginx1 conf]# nginx -t
    2. [root@nginx1 conf]# systemctl restart nginx

    7、优化Time_WAIT

    • time_wait是tcp连接状态中的一种,不是报错,出现在四次挥手之后
    • time_wai在连接正常关闭之后,一段时间之后会自动消失,而且占用的资源很少,对服务器性能的影响有限
    • 在time_wait状态下,tcp处于连接等待状态,此等待会有一个持续时间,也就是http1.1版本的会话保持

    time_wait主要目的:
    1、确保可靠的关闭连接
    2、避免连接复用

    查看time_wait:

    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

    如何使其快速消失?
    改内核文件

    1. [root@nginx1 ~]# vim /etc/sysctl.conf
    2. --添加--
    3. net.ipv4.tcp_syncookies = 1
    4. net.ipv4.tcp_tw_reuse = 1
    5. net.ipv4.tcp_tw_recycle = 1
    6. net.ipv4.tcp_fin_timeour = 60
    7. -------------------------------------------------------------------------------------------
    8. net.ipv4.tcp_syncookies = 1:表示开启SYN cookies(当出现SYN等待队列溢出时,启用cookies缓存记录来处理SYN队列),默认是01表示开启
    9. net.ipv4.tcp_tw_reuse = 1:time_wait状态可以重用,一个连接就要占用一个端口,time_wait若是把所有的端口全部占满,新的连接请求也不会被拒绝,但是也不会被处理,只是不拒绝
    10. net.ipv4.tcp_tw_recycle = 1:让time_wait尽快回收
    11. net.ipv4.tcp_fin_timeour = 60:所有time_wait最大生命周期60s

    8、配置防盗链

    目的:防止其他网站盗用本站的图片

    61主机模拟主站
    62主机模拟副站,即盗用网站

    1. [root@nginx1 conf]# vim nginx.conf
    2. http {
    3. ......
    4. server {
    5. ......
    6. location ~* \.(jpg|gif)$ {
    7. valid_referers none blocked *.pup.com pup.com;
    8. if ( $invalid_referer ) {
    9. rewrite ^/ http://www.pup.com/error.png;
    10. }
    11. }
    12. ......
    13. }
    14. }
    15. -------------------------------------------------------------------------------------------
    16. 设置信任的网站为pup.com/任意开头的pup.com
    17. none:允许没有http_refer的请求访问资源,请求url里面可以不包含refer,不需要带URI
    18. blocked:请求网站时,前面可以不带协议
    19. if语句:如果连接资源不是来自上面valid_referers定义的信任列表,$valid_referers的值会变成true,执行重定向rewrite...

    1. Web源主机配置(20.0.0.61):
    2. [root@nginx1 conf]# cd /usr/local/nginx/html/
    3. --将naruto.PNG和errot.PNG传入html目录下--
    4. [root@nginx1 html]# vim index.html
    5. "naruto.PNG"/>
    6. [root@nginx1 html]# echo "20.0.0.61 www.pup.com" >> /etc/hosts
    7. [root@nginx1 html]# echo "20.0.0.62 www.benet.com" >> /etc/hosts
    8. 盗链网站主机配置(20.0.0.62):
    9. [root@nginx2 ~]# cd /usr/local/nginx/html/
    10. [root@nginx2 html]# vim index.html
    11. "http://www.pup.com/error.PNG"/>
    12. [root@nginx2 html]# echo "20.0.0.61 www.pup.com" >> /etc/hosts
    13. [root@nginx2 html]# echo "20.0.0.62 www.benet.com" >> /etc/hosts

    主站访问www.pup.com

    副站访问www.benet.com

    补充

    nginx的常用内置变量

    1、$remote_addr:客户端的ip地址

    1. [root@nginx1 conf]# vim nginx.conf
    2. location / {
    3. root html;
    4. index index.html index.htm;
    5. return 200 "ip:$remote_addr";
    6. }

    浏览器访问本机地址会下载一个文件,打开即是本机的ip地址

    如何不下载而是直接打印?

    1. location / {
    2. ...
    3. default_type text/plain;
    4. }

     2、$remote_port:显示客户端的端口号

    1. location / {
    2. root html;
    3. index index.html index.htm;
    4. return 200 "ip:$remote_addr\nport:$remote_port";
    5. default_type text/plain;
    6. }

    3、$uri:显示请求的uri

    1. location / {
    2. root html;
    3. index index.html index.htm;
    4. return 200 "ip:$remote_addr\nport:$remote_port\nlj:$uri";
    5. default_type text/plain;
    6. }

    4、$host:显示请求的主机名

    1. location / {
    2. root html;
    3. index index.html index.htm;
    4. return 200 "ip:$remote_addr\nport:$remote_port\nlj:$uri\nzj:$host";
    5. default_type text/plain;

    5、$request_method:显示请求的方法

    1. location / {
    2. root html;
    3. index index.html index.htm;
    4. return 200 "ip:$remote_addr\nport:$remote_port\nlj:$uri\nzj:$host\nff:$request_method";
    5. default_type text/plain;
    6. }

    重要的变量配置

    proxy_set_header X-Forwarded-for $remote_addr

    代理服务器设置此变量,因为用代理服务器访问网站时,要加上客户端的真实ip传给后端,不然会认为代理服务器隐藏地址或具有攻击性,可能将代理服务器拉入黑名单

    proxy_set_header X-Real-IP $remote_addr

    不通过代理服务器访问网站,也是客户端的真实ip,也要发送给后端,现在所有的网站都会要求客户端在请求时加上真实ip

  • 相关阅读:
    夯实c语言基础
    cpp primer plus笔记07-内存模型和命名空间
    格力报案称“高管遭自媒体侮辱诽谤”
    【牛客刷题】带你在牛客刷题第八弹(简单排序)
    用 Rust 编写 eBPF/XDP 负载均衡器
    【前端面试知识题】- 4.2 JavaScript
    CSS 布局案例: 2行、多行每行格数不定,最后一列对齐
    electron使用electron-builder macOS windows 打包 签名 更新 上架
    SpringAMQP
    WPF数据绑定(Binding的源与路径)(二)
  • 原文地址:https://blog.csdn.net/pupcarrot/article/details/133827130