Linux入门教程:Nginx配置文件-3,运维日志


设置黑白名单:

语法:
allow
deny

作用位置:
http, server, location, limit_except

具体实现:
server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;

    location /test {
        root /www/html;
        deny 172.20.23.23;
        allow 172.20.23.33;
        deny all;
    }

    location /test1 {
        alias /mydata/html;
    }

}

测试访问:
[root@www21:17:48~]#curl http://www.a.com/test/ 
<h1>test location page for nginx</h1>

更改配置:
allow 172.20.23.33; --->换成deny
测试:
[root@www21:21:16~]#curl http://www.a.com/test/ 
<html>
<head><title>403 Forbidden</title></head>

基于用户的认证:

关键语法:
auth_basic "提醒语句"
官网说明:
Syntax: auth_basic string | off;
Default:    
auth_basic off;
Context:http, server, location, limit_except

auth_basic_user_file
官网说明:
Syntax: auth_basic_user_file file;
Default:    —
Context:http, server, location, limit_except

具体使用:
server {
        location /test1 {
        alias /mydata/html;
        auth_basic "test nginx";
        auth_basic_user_file /etc/nginx/conf.d/.npasswd;
    }
}

设置用户密码:
[root@www21:28:47conf.d]#htpasswd -c -m /etc/nginx/conf.d/.npasswd tom
New password: 
Re-type new password: 
Adding password for user tom

测试:
[root@www21:32:56~]#curl -u tom:123456 http://www.a.com/test1/
<h1>test alias for nginx</h1>

查看状态信息:

stub_status  on|off

具体使用:
server {
    location /status {
        stub_status on;
        allow 172.20.23.33;
        deny all;
     }

}

具体信息解析:
Active connections:当前状态,活动状态的连接数 
accepts:统计总值,已经接受的客户端请求的总数 
handled:统计总值,已经处理完成的客户端请求的总数 
requests:统计总值,客户端发来的总的请求数 
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数 
Writing:当前状态,正在向客户端发送响应报文过程中的连接数 
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

设置访问日志格式:

设置格式:
log_format [log_name] '$1 $2 $3';
设置在http段中
引用日志格式:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

具体使用:
自定义日志格式:
log_format test '$remote_addr:$remote_port $request_method $remote_user $status $http_user_agent $bytes_sent $gzip_ratio';  
使用自定义格式:
server {
    server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;
    access_log /web/a.com/a.com.log test;
}

http核心模块的内置变量:
 $uri:当前请求的uri,不带参数
 $host:http请求报文中host首部,如果请求中没有host首部,则以处理此请求的虚拟主机名
  代替!
 $request_uri:请求的uri,带完整参数
 $hostname:nginx服务运行在的主机的主机名
 $remote_addr:客户端ip
 $remote_port:客户端端口
 $remote_user:使用用户认证时客户端用户输入的用户名
 $request_filename:用户请求中的RUI经过本地root或alias转换后映射的本地的文件或路径
 $request_method:请求方法
 $server_addr:服务器地址
 $server_name:服务器名称
 $server_port:服务器端口
 $server_protocol:服务器向客户端发送响应时的协议 如http/1.1
 $scheme:在请求中使用的scheme,如https http,哪个协议
 $http_HEADER:匹配请求报文中指定的HEADER  $http_host匹配请求报文中的host首部
 $sent_http_HEADER:匹配响应报文中指定的HEADER---要小写
  例子:$http_content_type匹配响应报文中的content-type首部
 $document_root:当前请求映射到的root配置项
日志格式变量: 
 $status:用来引用状态码
 $bytes_sent:发送的字节数
 $http_referer:从哪个页面跳转过来的
 $http_user_agent:浏览器的类型
 $gzip_ratio:压缩比例,配合压缩功能

设置日志缓存:

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];  

open_log_file_cache off;  
缓存各日志文件相关的元数据信息   
max:缓存的最大文件描述符数量  
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项  
inactive:非活动时长  
valid:验证缓存中各缓存项是否为活动项的时间间隔 

压缩功能:

关键设置:
gzip on|off
官网说明:
Syntax: gzip on | off;
Default:    
gzip off;
Context:http, server, location, if in location
基本设置项:
gzip_buffers [n]:压缩时缓冲大小
gzip_comp_level[0--9]:压缩等级
gzip_distable [对哪种浏览器,文件不压缩]msie6
gzip_min_length [值]:内容大于这个值才会压缩
gzip_http_version:压缩后构建响应报文,使用那个版本 1.0/1.1
gzip_types:只对那些类型的网页文件做压缩默认包含有text/html
gzip_buffers number size; 支持实现压缩功能时缓冲区数量及每个缓存区的大小  
默认:32 4k 或 16 8k
gzip_vary on | off;如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding

具体使用:
server {
        location /test3/ {
        gzip on;
        gzip_comp_level 6;
        gzip_min_length 100;
        gzip_types text/xml text/css text/plain application/javascript;
    }
}

测试:
172.20.23.33:44018 GET - 200 curl/7.29.0  200 436946 "-" -->压缩前
172.20.23.33:44020 GET - 200 curl/7.29.0  200 3252 "134.86"-->压缩后

相关内容