您的位置:首页 > 运维架构 > Nginx

nginx防盗链

2019-11-18 13:24 1101 查看

Nginx实现连接超时

  • 在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
  • 使用fiddler工具查看connection参数

超时参数

Keepalive_timeout
设置连接保持超时时间,一般可只设置该参数,默认为65秒,可根据网站的情况设置,或者关闭,可在http段,server段,或者location段设置
Client_header_timeout
指定等待客户端发送请求头的超时时间
Client_body_timeout
设置请求体读超时时间

修改配置文件

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf    ##修改配置文件

#keepalive_timeout  0;
keepalive_timeout  65 180;     ##服务端和客户端的超时时间
client_header_timeout 80;       ##请求头部超时时间
client_body_timeout 80;          ##请求体超时时间

[root@localhost conf]# service nginx stop  ##关闭开启服务
[root@localhost conf]# service nginx start

Nginx运行进程数

  • 在高并发场景,需要启动更多的Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞
  • 可以使用ps aux命令查看Nginx运行进程的个数
  • 更改进程数的配置方法
    修改配置文件,修改进程配置参数
  • 修改配置文件的worker_process参数
    一般设为CPU的个数或者核数
    在高并发情况下可设置为CPU个数或者核数的2倍
  • 运行进程数多一些,响应访问请求时,Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度
  • 使用ps aux查看运行进程数的变化情况
  • 默认情况,Nginx的多个进程可能跑在一个CPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU
  • 在一台4核物理服务器,可进行以下配置,将进程进行分配
    worker_cpu_affinity 0001 0010 0100 1000

1,查看当前的工作进程信息

[root@localhost ~]# ps aux | grep nginx  ##查看进程信息
root       7231  0.0  0.0  20548   620 ?        Ss   01:57   0:00 nginx: master process  ##主进程
nginx      7232  0.0  0.0  23076  1400 ?        S    01:57   0:00 nginx: worker process ##1个工作进程
root       7372  0.0  0.0 112728   972 pts/2    S+   02:07   0:00 grep --color=auto nginx

2,为虚拟机添加CPU

3,修改配置文件

[root@localhost ~]# cd /proc/
[root@localhost proc]# cat cpuinfo   ##查看cpu核心数
processor       : 0   ##第一个
...
processor       : 1   ##第二个
...
[root@localhost proc]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf   ##修改配置文件

#user  nobody;
worker_processes  2;    ##核心数为2
worker_cpu_affinity 01 10;   ##进程分配

4,查看工作进程

[root@localhost conf]# ps aux | grep nginx   ##查看Nginx进程信息
nginx  2329  0.0  0.0  23076  1392 ?    S  20:12   0:00 nginx: worker process
##此时是两个工作进程
nginx   2330  0.0  0.0  23076  1384 ?   S  20:12   0:00 nginx: worker process

Nginx实现网页压缩功能

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

压缩功能参数

  • gzip on:开启gzip压缩输出
  • gzip_min_length 1k:用于设置允许压缩的页面最小字节数
  • gzip_buffers 4 16k:表示申请4个单元为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果
  • zip_http_version 1.0:用于设置识别http协议版本,默认是1.1,目前大部分浏览器已经支持gzip解压,但处理最慢,也比较消耗服务器CPU资源
  • gzip_comp_level 2:用来指定gzip压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理速度最慢,使用默认即可
  • gzip_types text/plain:压缩类型,是就对那些网页文档启用压缩功能
  • gzip_vary on:选项可以让前端的缓存服务器缓存经过gzip压缩的页面

实验环境

Linux服务器(192.168.13.142)
一台win10测试机

1,修改配置文件添加压缩模块内容

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf   ##修改配置文件
#gzip  on;   ##开启gzip压缩功能(暂时不开启)
gzip_min_length 1k;      ##压缩阀值
gzip_buffers 4 16k;        ##buffer 大小为4个16k缓冲区大小
gzip_http_version 1.1;   ##压缩版本
gzip_comp_level 6;       ##压缩比率,5,6适中
gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg
image/png image/gif application/xml text/javascript application/x-httpd-php app
lication/javascript application/json;
##支持的类型格式
gzip_disable "MSIE [1-6]\.";   ##禁用gzip条件,支持正则,表示ie6以下不启用gzip
gzip_vary on;     ##让前端的缓存服务器缓存经过gzip压缩的页面

2,在网页站点中插入图片

[root@localhost html]# mount.cifs //192.168.100.3/LAMP-C7 /mnt   ##挂载共享
Password for root@//192.168.100.3/LAMP-C7:
[root@localhost html]# ls /mnt
apr-1.6.2.tar.gz                  Discuz_X2.5_SC_UTF8.zip  LAMP-php5.6.txt
apr-util-1.6.0.tar.gz             error.png                mysql-5.6.26.tar.gz
awstats-7.6.tar.gz                httpd-2.4.29.tar.bz2     nginx-1.12.0.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm  kali.jpg                 php-5.6.11.tar.bz2
[root@localhost html]# cp /mnt/11.png ./    ##将图片复制到站点中
[root@localhost html]# vim index.html       ##将图片放到网页中

<h1>Welcome to nginx!</h1>
<img src="11.jpg"/>   ##添加图片

[root@localhost html]# service nginx stop   ##关闭开启Nginx服务
[root@localhost html]# service nginx start
[root@localhost html]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost html]# setenforce 0

3,查看网页信息,并用fiddler抓包


4,开启gzip压缩功能

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf  ##修改配置文件
gzip  on;   ##开启压缩功能
[root@localhost html]# service nginx stop  ##关闭和开启Nginx服务
[root@localhost html]# service nginx start

5,查看网页,并用fiddler抓包查看压缩情况

Nginx实现防盗链

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

实验环境

一台Linux服务器(192.168.13.142)
一台win7盗链网站(192.168.13.135)
一台win10测试机

一,在win7盗链网站中创建一个盗链的网页

1,用文本创建一个网页内容,修改后缀为index.html

<html>
<head>
<title>云计算</title>
</head>
<body>
<h1>this is test web</h1>
<img src="http://www.kgc.com/11.jpg"/>    ##盗链的图片
</body>
</html>

2,搭建web网站,指定DNS服务器地址(192.168.13.142)




3,安装dns服务器

[root@localhost html]# yum install bind -y  ##安装dns
[root@localhost html]# vim /etc/named.conf
options {
listen-on port 53 { any; };          ##监听所有
listen-on-v6 port 53 { ::1; };
directory       "/var/named";
dump-file       "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file  "/var/named/data/named.recursing";
secroots-file   "/var/named/data/named.secroots";
allow-query     { any; };           ##允许所有
[root@localhost html]# vim /etc/named.rfc1912.zones    ##配置区域配置文件
zone "kgc.com" IN {
type master;
file "kgc.com.zone";                ##kgc区域数据配置文件
allow-update { none; };
};
[root@localhost html]# cd /var/named/
[root@localhost named]# cp -p named.localhost kgc.com.zone    ##复制模板
[root@localhost named]# vim kgc.com.zone    ##修改区域配置文件

$TTL 1D
@       IN SOA  @ rname.invalid. (
0       ; serial
1D      ; refresh
1H      ; retry
1W      ; expire
3H )    ; minimum
NS      @
A       127.0.0.1
www IN  A       192.168.13.142     ##本机地址
[root@localhost named]# systemctl start named      ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service    ##关闭防火墙
[root@localhost named]# setenforce 0

4,用测试机访问盗链网站,原网站


5,修改配置文件,开启防盗链功能

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
##在server段插入防盗链配置
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

location ~*\.(jpg|gif|swf)$ {     ##支持格式
valid_referers none blocked *.kgc.com kgc.com;   ##允许kgc.com为后缀访问
if ( $invalid_referer ) {
rewrite ^/ http://www.kgc.com/error.png;  ##不是则发送错误图片
}
}
[root@localhost named]# cp /mnt/error.png /usr/local/nginx/html/   ##将防盗链图片放到站点中
[root@localhost named]# service nginx stop   ##关闭开启Nginx服务
[root@localhost named]# service nginx start

6,利用测试机访问盗链网站

对FPM模块进行参数优化

  • Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的出来速度,可对FPM模块进行参数的调整
  • FPM模块参数调整,要根据服务器的内存与服务器负载进行调整
  • 启动fpm进程方式
    static:将产生固定数量的fpm进程
    dynamic:将以动态的方式产生fpm进程
    通过pm参数指定

FPM优化参数

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

FPM优化参数调整

优化原因:服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
优化参数调整:FPM启动时有5个进程,最小空闲2个进程,最大空闲8个进程,最多可以有20个进程存在

谢谢阅读!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: