连接状态模块
###stub_status_module#访问默认站点的状态(注意:错误验证)http://ip/nginx_status#配置模块状态(在哪一个网站下配置,就只能看哪一个网站的状态)server { location /nginx_status { stub_status; allow ip; #允许那个ip访问 deny all; #拒接那个ip }}#配置完成后重启服务器并打开模块页面nginx -t #检测配置文件书写nginx -s reload #重新加载配置文件http://ip/nginx_status#解析Active connections: 2 #活跃连接数server accepts handled requests 2001 2001 1881 #发起连接数,成功连接数,请求书Reading: 0 Writing: 1 Waiting: 1 #Reading: 0 读取客户端Header的信息数 请求头 #Writing: 1 返回给客户端的header的信息数 响应头 #Waiting: 1 #等待的请求数,开起来keepalive
随机主页
# random_index_module1,创建主页目录mkdir /app2,创建多个主页(在不同主页下写入不同内容)touch /app/{blue.html,green.thml,red.html,yellow.html}echo 1 > /app/blue.html ......3,启动随机主页(案例)vim /etc/nginx/conf.d/default.conflocation / { # root /usr/share/nginx/html; # index index.html index.htm; root /app; random_index on; }#重启nginxnginx -tnginx -s reload
替换模块
# sub_module# 如果我们用模板生成网站的时候,因为疏漏或者别的原因造成代码不如意,但是此时因为文件数量巨大,不方便全部重新生成,那么这个时候我们就可以用此类模块来暂时实现纠错,另一方面,我们也可以利用这个实现服务器端文字过滤的效果。启动替换1vim /etc/nginx/conf.d/default.conf server {sub_filter nginx "jiangzilong"; #把nginx替换为jiangzilongsub_filter_once off; #关闭默认只替换一行,全替换 location / { root /usr/share/nginx/html; index index.html index.htm; }}#重启nginx,自行测试nginx -tnginx -s reload
文件读取,nginx内核优化,可以直接打开几个配置
ngx_http_core_module sendfile #发送文件速度加快,直接发给接口,默认开启tcp_nopush #使用tpc_nopush网络传输效率提升,当包累计到一定大小后再发送tcp_nodelay #启动TCP_NODELAY,就意味着禁用了Nagle算法,允许小包的发送http{ sendfile on; tcp_nopush on; tcp_nodelay on;}
文件压缩
原理介绍启动该模块,使文件传输前进行压缩,提升传输效率。ngx_http_gzip_module#模块开启关闭syntax: gzip on | off;Default: gzip off;Context: http,server,location,if in location#设置gzip压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大syntax: gizp_comp_level level;Default: gizp_comp_level 1;(1-9)Context: http,server,location#用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项。syntax: gzip_http_version 1.0 | 1.1;Default: gzip_http_version 1.1;Context: http,server,location#设置用于处理请求压缩的缓冲区数量和大小。比如32 4K表示按照内存页(one memory page)大小以4K为单位(即一个系统中内存页为4K),申请32倍的内存空间。建议此项不设置,使用默认值。Syntax: gzip_buffers number size;Default: gzip_buffers 32 4k|16 8k;Context: http, server, location#压缩类型gzip_types text/plain application/json text/javascript application/x-javascript text/css application/xml;#静态文件压缩开启gizp_static on;
页面缓存
# 模块 expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求要配置expires。ngx_http_headers_modulesyntax: expires [modified] time; expires epoch | max |off;Detault: expires off;context: http,server,location,if in location location / { expires 24h; root /usr/share/nginx/html; index index.html index.htm; }
防盗链
日志不正常,日志莫名其妙的产生了,观察referer字段,发现被盗链了。#针对所有内容location / { root /a.com; index index.html index.htm; valid_referers none blocked *.a.com; if($invalid_referer){ return 4 }}#server_name 白名单location / { root /a.com; index index.html index.htm; valid_referers none blocked *.a.com server_name ~\.goole\. ~192.168.8; if($invalid_referer){ return 4 }}