访问状态
ststus 指令
此参数属于ngx_ http_ stub_ status_ module 模块,这个模块的主要功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态,例如连接数等信息。要使用状态模块,在编译Nginx时必须增加http stub_ status module模块来支持。配置方法:
vim conf.d/state.conf
server {
listen 80;
server_name state.axb.com;
stub_status;
}
访问日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_x_real_ip"';
access_log /var/log/nginx/access.log main;
参数解释
log_format可定义多种日志格式,取不同名称即可access_log用来指定日志文件的路径及使用那种日志格式
"log_format"指令只能配置在"http块"中。 "access_log"指令可以配置在以下区块中: http, server, location, if in location, limit_except
日志变量说明
$remote_addr记录访问网站的客户端地址$http_x_forwarded_for当前端有代理服务器时,设置Web节点记录客户端地址的配置$remote_user远程客户端用户名称$time_local记录访问时间与时区$request用户的http请求起始行信息$statushttp状态码,记录请求返回的状态,如 200,404等$body_bytes_sents服务器发送给客户端的相应body字节数$http_referer记录此次请求是从哪个链接访问过来的,$http_user_agent记录客户端访问信息,如:浏览器,手机等…
错误日志
属于ngx_core_module模块的参数,参数名为error_log, 可以在Main区块中全局配置,也可放置在不同的虚拟主机中单独记录.
关键字日志文件错误级别
error_log/var/log/nginx/error.logwarn
生产场景使用warn|error|crit之一
错误级别
[debug|info|notice|warn|error|crit|alert|emerg]
可放置区域 main, http, server, locatio
日志切割
脚本
#!/bin/bash
log_name
="access.log"
log_dir
="/var/log/nginx"
dir
=$(/usr/bin/ls -l /var/log/nginx/access.log|awk '{print $5}')
Date
=$(date +%F-%T)
if [ $dir -gt 300000
];
then
mv $log_dir/
$log_name /backup/
${log_name}_
$Date
nginx -s reopen
else
exit
fi
0 */3 * * * /bin/sh /server/scripts/access_cut.sh
&>/dev/null
logrotate
主配置文件:vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly #切割日志时间周期 daily hourly monthly
# keep 4 weeks worth of backlogs
rotate 4 #保留多少数据备份,4份
# create new (empty) log files after rotating old ones
create #轮换旧文件后创建新的(空)日志文件
# use date as a suffix of the rotated file
dateext #使用日期作为轮询文件的后缀
# uncomment this if you want your log files compressed
#compress #确认切割后日志是否打包压缩
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #RPM软件包将日志轮询配置文件放入此目录中
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { #指定特定日志文件做特殊切割处理
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
索引(共享)功能
autoindex 启动或关闭目录功能charset 页面设置字符集
server {
listen 80;
server_name www.test.cn test.cn;
autoindex on;
charset utf-8; #设置字符集,避免乱码
root /html/www; #将资源上传至此目录
#index index.html index.htm;
error_page 500 502 503 504 /50x.html;
}
需要将首页文件去掉,否则会优先显示首页文件的内容若想下载资源可将将mime.types文件中的某些静态资源注释掉.不识别的会自动下载
访问控制
allowdeny
deny的优先级高于allow
server {
listen 80;
server_name www.axb.com axb.com;
root /html/www;
index index.html index.htm;
location /ss {
deny 10.0.0.0/24;
}
location /mm {
deny 172.16.1.0/24;
}
}
登陆认证
auth_basic 开启认证功能auth_basic_user_file 启用密码文件
配置文件
server {
listen 80;
server_name bbs.axb.com;
root /html/bbs;
autoindex on;
index index.html index.htm;
charset utf-8;
error_log /var/log/nginx/error_bbs.log warn;
location /ss_dir {
auth_basic "sa env auth";
auth_basic_user_file conf/htpasswd;
}
}
生成密码文件
yum install -y httpd-tools
用法一: 创建密码文件并添加用户信息
htpasswd -bc /etc/nginx/conf/htpasswd sa sa123
用法二: 添加新的认证用户
htpasswd -b /etc/nginx/conf/htpasswd sa01 sa123
用法三: 删除指定认证
htpasswd -D /etc/nginx/conf/htpasswd sa01