当前位置:   article > 正文

Nginx优化与防盗链_nginix配置网络盗链

nginix配置网络盗链

目录

前言

一、隐藏版本号

      1、查看版本号方法

      2、Nginx(NG)隐藏版本号的方式:

二、修改用户和组

      1、修改属组属主

​      2、重启服务,查看服务状态

​ 三、设置缓存时间

      1、修改配置文件

      2、重启服务

      3、上传图片,修改站点文件

四、日志分割

     1、插入脚本

     2、测试脚本

五、连接超时

     1、超时参数

     2、修改配置文件

六、更改进程数

七、网页压缩

八、盗链与防盗链

     1、盗链

     2、防盗链

九、FPM模块参数优化

 总结


前言

        在企业信息化应用环境中。服务器的安全性和响应速度需要根据实际的情况进行相应的参数配置,达到最优的用户体验。默认的nginx安装参数只能提供最基本的服务,还需要调整如网页时间、连接超时、网页压缩等相应参数,才能发挥服务器的最大作用。

一、隐藏版本号

     1、查看版本号方法

        1-1、本地查看(查看头部信息)

         1-2、浏览器查看

                谷歌浏览器:更多工具→开发者工具→Network一刷新页面→点击ip地址→Headers→查看到版本

      2、Nginx(NG)隐藏版本号的方式:

         2-1、修改配置文件

  1. vim /usr/local/nginx/conf/nginx.conf
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. server_tokens off; ##添加命令,指的是关闭版本号
  9. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '


验证:

  1. systemctl restart nginx
  2. curl -i http://192.168.159.70

         2-2、修改源码

  1. vim /opt/nginx-1.12.2/src/core/nginx.h
  2. #ifndef _NGINX_H_INCLUDED_
  3. #define _NGINX_H_INCLUDED_
  4. #define nginx_version 1012002
  5. #define NGINX_VERSION "8.0.0" #修改版本号
  6. #define NGINX_VER "mysql/" NGINX_VERSION #修改服务器类型
  7. #ifdef NGX_BUILD


重新编译:

  1. cd /opt/nginx-1.12.2/
  2. ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
  3. make && make install


 

  1. vim /usr/local/nginx/conf/nginx.conf
  2. ......
  3. http {
  4. include mime.types;
  5. default_type application/octet-stream;
  6. server_tokens on;
  7. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  8. ......
  9. systemctl restart nginx
  10. curl -i http://192.168.159.70

 

二、修改用户和组

     1、修改属组属主

         若没有安装前创建用户,则在此服务中默认使用的是nobody

  1. vim /usr/local/nginx/conf/nginx.conf
  2. #user nobody;
  3. worker_processes 1;
  4. #默认是Nginx默认使用nobody用户账号与组账号
  5. #修改成如下内容
  6. user nginx nginx;
  7. worker_processes 1;


      2、重启服务,查看服务状态

  1. systemctl restart nginx
  2. ps aux | grep nginx



 三、设置缓存时间

     当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
     一般针对静态网页设置,对动态网页不设置缓存时间

     1、修改配置文件

  1. vim /usr/local/nginx/conf/nginx.conf
  2. server {
  3. listen 192.168.159.70:80;
  4. server_name www.accp.com;
  5. charset utf-8;
  6. access_log logs/accp.access.log;
  7. location / {
  8. root html;
  9. index index.html index.htm;
  10. } #输入下面4行内容
  11. location ~ \.(gif|jpg|png|bmp|ico)$ { ##加入新的location,以图片作为缓存对象
  12. root html;
  13. expires 1d; #指定缓存时间,1天
  14. }

 

      2、重启服务

  1. nginx -t
  2. systemctl restart nginx

      3、上传图片,修改站点文件

  1. [root@localhost ~]# cd /usr/local/nginx/html
  2. [root@localhost html]# rz -E
  3. rz waiting to receive.
  4. [root@localhost html]# cd /usr/local/nginx/html
  5. [root@localhost html]# rz -E
  6. rz waiting to receive.
  7. [root@localhost html]# ls
  8. 50x.html index.html qiche.jpg
  9. [root@localhost html]# vim index.html
  10. </style>
  11. </head>
  12. <body>
  13. <h1>Welcome to nginx!</h1>
  14. <img src="qiche.jpg"/> #插入识别图片
  15. systemctl restart nginx

 

四、日志分割

     1、插入脚本

  1. [root@www ~]# vim /opt/fenge.sh
  2. #!/bin/bash
  3. #Filename:fenge.sh
  4. d=$(date -d "-1 day" "+%Y%m%d") #显示前一天的时间
  5. logs_path="/var/log/nginx"
  6. pid_path=" /usr/local/nginx/logs/nginx.pid"
  7. [ -d $logs_path ] || mkdir -p $logs_path #创建日志文件目录
  8. mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d#移动并重命名日志文件
  9. kill -HUP $(cat $pid_path) #重建日志文件
  10. find $logs_path -mtime +30 | xargs rm -rf #删除30天前的日志文件
  11. 首先先定义一个前一天的时间戳的变量d,再指定两个变量,一个是pid文件位置,一个是保存分割之后的日志文件位置,然后判断我们定义的/var/log/nginx是否存在,如果不存在创建,根据时间戳,在每天1点把前一天的日志给移动到我们新创建的$logs_path下,并且以前一天的时间戳命名,然后再去发送一个信号让我们的nginx重载,也就是生成一个新的日志文件,以此再去进行揭露当天的日志并且到第二天凌晨一点重新做这个事,最后设置30天清一次。

      2、测试脚本

  1. [root@www ~]# chmod +x /opt/fenge.sh
  2. [root@www ~]# crontab -e
  3. crontab: no changes made to crontab
  4. [root@www ~]# crontab -l
  5. 0 1 * * * /opt/fenge.sh
  6. [root@www ~]# systemctl restart nginx
  7. [root@www ~]# netstat -natp | grep nginx
  8. tcp 0 0 192.168.159.100:80 0.0.0.0:* LISTEN 4094/nginx: master
  9. tcp 0 0 192.168.159.70:80 0.0.0.0:* LISTEN 4094/nginx: master
  10. [root@www ~]# sh -x /opt/fenge.sh
  11. ++ date -d '-1 day' +%Y%m%d
  12. + d=20211010
  13. + logs_path=/var/log/nginx
  14. + pid_path=' /usr/local/nginx/logs/nginx.pid'
  15. + '[' -d /var/log/nginx ']'
  16. + mkdir -p /var/log/nginx
  17. + mv /usr/local/nginx/logs/access.log /var/log/nginx/test.com-access.log-20211010
  18. ++ cat /usr/local/nginx/logs/nginx.pid
  19. + kill -HUP 4094
  20. + find /var/log/nginx -mtime +30
  21. + xargs rm -rf
  22. [root@www ~]# ls /var/log/nginx
  23. test.com-access.log-20211010
  24. [root@www ~]# date -s 20211010
  25. 20211010日 星期日 00:00:00 CST

五、连接超时

     为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间

    1、超时参数

       Keepalive_timeout

           设置连接保持超时时间

       Client_header timeout

            指定等待客户端发送请求头的超时时间

       Client_body _timeout

             设置请求体读超时时间

     2、修改配置文件

  1. [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
  2. #keepalive_timeout 0;
  3. keepalive_timeout 65;
  4. client_header_timeout 80; #等待客户端发送请求头的超时时间,超时会发送408错误
  5. client_body_timeout 80; #等待客户端发送请求体的超时时间
  6. [root@www ~]# nginx -t
  7. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  8. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

六、更改进程数

  1. [root@www ~]# cat /proc/cpuinfo | grep -c "physical" #核心数
  2. 12
  3. [root@www ~]# ps aux | grep nginx #—一个主进程包含一个子进程
  4. root 4094 0.0 0.0 20644 1452 ? Ss 11:25 0:00 nginx: master process /usr/local/ngi
  5. nx/sbin/nginxnginx 4123 0.0 0.0 22956 1444 ? S 11:27 0:00 nginx: worker process
  6. root 4889 0.0 0.0 112680 980 pts/1 S+ 12:03 0:00 grep --color=auto nginx
  7. [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
  8. #user nobody;
  9. worker_processes 2; #修改与cPU核数相同或2倍( cgroup)
  10. worker_cpu_affinity 01 10; #设置每个进程由不同的cpu处理、进程数配为2时,为0001 0010 0100 1000
  11. [root@www ~]# systemctl restart nginx
  12. PS:
  13. 01表示启用第一个cPu内核,10表示启用第二个cPu内核
  14. worker cpu affinity 0110;表示开启两个进程,第一个进程对应着第一个cP u内核,第二个进程对应着第二个cPt内核。###2核cpu,开启4个进程
  15. worker processes 4 ;
  16. worker cpu affinity 01 10 01 10;
  17. PS:开启了四个进程,它们分别对应着开启2个cPU内核###4个cpu,开启4个进程
  18. worker processes 4 ;
  19. worker cpu affinity 0001 0010 0100 1000 ;
  20. Ps:0001表示启用第一个cPu内核,0010表示启用第二个cPu内核,依此类推

七、网页压缩

  1. [root@www ~]# vim /usr/local/nginx/conf/nginx.conf
  2. #gzip on;
  3. gzip on; #取消注释,开启gzip压缩功能
  4. gzip_min_length 1k; #最小压缩文件大小
  5. gzip_buffers 4 16k; #压缩缓冲区,大小为4个16k缓冲区
  6. gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
  7. gzip_comp_level 6; #压缩比率
  8. gzip_vary on; #支持前端缓存服务器存储压缩页面
  9. 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;
  10. #支持前端缓存服务器存储压缩页面
  11. [root@www html]# vim index.html
  12. ......
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <img src="qiche.jpg"/> #网页中插入图片
  17. [root@www html]# systemctl restart nginx

在Linux系统中,打开火狐浏览器,右击点查看元素

选择 网络 ---> 选择 HTML、WS、其他

访问 http://192.168.184.20 ,双击200响应消息查看响应头中包含 Content-Encoding: gzip

八、盗链与防盗链

    1、盗链

       服务端:192.168.159.80

      盗链端:192.168.159.70

       1-1、服务端配置

  1. [root@localhost ~]# cd /usr/local/nginx/html
  2. [root@localhost html]# rz -E
  3. rz waiting to receive.
  4. [root@localhost html]# ls
  5. 50x.html dog.jpg index.html
  6. [root@localhost html]# vim index.html
  7. <h1>Welcome to nginx!</h1>
  8. <img src="dog.jpg"/>
  9. [root@localhost html]# vim /etc/hosts
  10. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  11. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  12. 192.168.159.70 www.dog.com

        1-2、盗链端

  1. [root@www html]# cd /usr/local/nginx/html
  2. [root@www html]# ls
  3. 50x.html index.html qiche.jpg
  4. [root@www html]# vim index.html
  5. <h1>Welcome to nginx!</h1>
  6. <img src="http://www.dog.com/dog.jpg"/
  7. [root@www html]# vim /etc/hosts
  8. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  9. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  10. 192.168.159.80 www.dog.com

     2、防盗链

        2-1、服务端

  1. [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
  2. ......
  3. #gzip on
  4. ......
  5. location / {
  6. root html;
  7. index index.html index.htm;
  8. }
  9. location ~*\.(jpg|gif|swf)$ {
  10. valid_referers *.dog.com dog.com; #设置信任的访问来源
  11. if ( $invalid_referer ) {
  12. rewrite ^/ http://www.dog.com/error.png;
  13. #return 403;
  14. }
  15. }
  16. #error_page 404 /404.html;
  17. [root@localhost ~]# nginx -t
  18. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  19. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  20. [root@localhost ~]# cd /usr/local/nginx/html
  21. [root@localhost html]# rz -E
  22. rz waiting to receive.
  23. [root@localhost html]# ls
  24. 50x.html dog.jpg error.png index.html
  25. [root@localhost html]# systemctl restart nginx
  26. 防盗链设置参数详细说明:
  27. valid referers:设置信任的网站,即能引用相应图片的网站none:浏览器中Referer为空的情况,就是直接在浏览器访问图片
  28. blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以以http:/l或者https://开头后面的网址或者域名:referer中包含相关字符串的网址
  29. if语句:如果链接的来源域名不在talid referers所列出的列表中,sinvalid referer为1,则执行后面的操作,即进行重写或返回403页面
  30. ##上传error.png文件至/usr/ local/ nginx/ html

 

 

九、FPM模块参数优化

  1. cd /usr/ local/php/etc/
  2. cp php-fpm.conf.default php-fpm.conf
  3. vim php-fpm.conf
  4. pid = run/php-fpm.pid
  5. vim /usr/local/php/etc/php-fpm.d/ www .conf
  6. #96行
  7. pm = dynamic #fpm进程启动方式,动态的
  8. #107行
  9. pm.max_children=20 #fpm进程启动的最大进程数
  10. #112行
  11. pm.start_servers = 5 #动态方式下启动时默认开启的进程数,在最小和最大之间
  12. #117行
  13. pm.min_spare_servers = 2 #动态方式下最小空闲进程数
  14. #122行
  15. pm. max_spare_servers = 8 #动态方式下最大空闲进程数
  16. #启动php-fpm,不可用于重启
  17. /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
  18. #执行第一个命令后,就可以使用下面这条命令查看pid号重启php-fpm
  19. kill -USR2 `cat /usr/ local/php/var/run/php-fpm.pid`
  20. netstat -anpt l grep 9000

 总结

         Nginx可以作为正反向代理服务器,也可以作为负载均衡使用,而且是一个轻量级且高性能的服务器,所以肯定会出现高并发高流量的场景,而且安全问题一直处于被动状态,各种安全漏洞依然存在,为了服务器的正常运行,优化是非常重要的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/378153
推荐阅读
相关标签
  

闽ICP备14008679号